Tensorflow2 -- MNIST

Tensorflow2.X和1.X有多了很多差別和使用方式, 今天用tf2來實作MNIST分類問題 MNIST MNIST是一個很標準的手寫數字分類問題, 數據集下載有很多方式,這次直接使用tf API提供的 28 * 28 且只有黑白的數據 開發 在local 起 jupyter lab 先看看GPU是否啟用 %matplotlib widget import matplotlib.pyplot as plt import tensorflow as tf import numpy as np # check gpu tf.config.list_physical_devices('GPU') tf.test.is_built_with_cuda() # output True 方法一 繼承 tf.keras.model class MLP(tf.keras.Model): def __init__(self): super().__init__() self.flatten = tf.keras.layers.Flatten() self.dense1 = tf.keras.layers.Dense(units=100, activation=tf.nn.relu) self.dense2 = tf.keras.layers.Dense(units=20, activation=tf.nn.leaky_relu) self.dense3 = tf.keras.layers.Dense(units=10) @tf.function def call(self, inputs): # [batch_size, 28, 28, 1] flat1 = self.flatten(inputs) # [batch_size, 784] dens1 = self.dense1(flat1) # [batch_size, 100] dens2 = self.dense2(dens1) # [batch_size, 20] dens3 = self.dense3(dens2) # [batch_size, 10] output = tf.nn.softmax(dens3) return output 使用tf.GradientTape訓練 # @tf.function def one_batch_step(X, y, **kwargs): with tf.GradientTape() as tape: y_pred = model(X) loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred) loss = tf.reduce_mean(loss) tf.print(f"{batch_index} loss {loss}", [loss]) with summary_writer.as_default(): tf.summary.scalar("loss", loss, step=batch_index) grads = tape.gradient(loss, model.variables) optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables)) for epoch_index in range(num_epochs): for batch_index in range(num_batches): X, y = data_loader.get_batch(batch_size) one_batch_step(X, y, batch_index=batch_index) with summary_writer.as_default(): tf.summary.trace_export(name="model_trace", step=0, profiler_outdir=log_dir) tf.saved_model.save(model, f"saved/{model_name}") 方法二 使用keras Pipeline來疊每一層要用的函數,彈性較低,但非常適合簡單的Model ...

2020-09-26 · 1 min read · 194 words · KbWen · ZH

Keras IMDb

IMDb 是一個電影相關的線上資料庫。這次要利用 IMDb 的影評文字,預測它屬於正面評價還是負面評價。 在深度學習模型中,輸入必須是數字。Keras 提供了 Tokenizer 模組,會依照英文單字出現頻率進行排序並編號:Keras Tokenizer 官方文件。 接著利用 Word Embedding 將編號清單轉換為向量清單,最後丟進 LSTM 模型進行學習。 Keras 封裝了許多方便的功能,讓文字轉數字與模型建立變得非常簡單。 這是我的 Model Summary。將數字序列轉換為 64 維的向量序列,並使用了三層隱藏層進行訓練。 準確率:0.8543 實際測試 造訪 IMDb 網站,抓取《蜘蛛人:返校日 (Spider-Man: Homecoming)》的評論進行檢驗。輸入正面評論後,模型正確辨識為正面(1 為正面,0 為負面)。 My Github

2017-07-11 · 1 min read · 34 words · KbWen · ZH

Keras Cifar-10

這次使用 Keras 建立 CNN 疊代模型,來辨識 CIFAR-10 影像資料。 CIFAR-10 是 32*32 的 RGB 彩色圖形,包含飛機、狗、貓等 10 個類別,可以視為 MNIST 的進階挑戰版。 在數據預處理 (Preprocess) 階段,流程與 MNIST 類似,包括標準化與 One-hot encoding。 模型架構: 卷積層 (Convolution):兩層,選用 3*3 Kernel,Same padding。 池化層 (Max-pooling):2*2 大小。 全連接層 (Dense):由 4096 降至 1024,最後輸出 10 個類別。 可以觀察 Keras 與 TensorFlow 在參數表現與語法上的些微差异。 利用 pandas 建立混淆矩陣 (Confusion Matrix),分析模型是否在特定類別間產生混淆。 從矩陣中可以看出: 第 3 類 (cat) 與第 5 類 (dog) 較容易混淆。 動物類與交通工具類之間區分得相當清楚。 兩層 CNN 準確率:0.732 My Github

2017-07-06 · 1 min read · 66 words · KbWen · ZH