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 · 194 words · KbWen

Keras IMDb

IMDb是一個電影相關的線上資料庫 這次要利用IMDb的影評文字預測它是正面評價或是負面評價 在深度學習模型中只能接受數字,Keras有提供Tokenizer模組 會依照英文次數進行排序,在給每個單字編號:Keras Tokenizer 在利用Word embedding 將數字list 轉換成向量list,最後丟進去LSTM做學習 (在Keras 使用 RNN LSTM 模型很方便,一行解決) Keras也提供讓我們方便把英文轉成數字的模型 這是model summary 把數字list轉換成64維的向量list,並且用三層的隱藏層來做訓練。 準確率:0.8543 實際使用 進入IMDb網站,抓取Spider-Man: Homecoming評論,檢驗是否正確。 拿了正面評論結果也是顯示正面(1:正面,0:負面) My Github

2017-07-11 · 1 min · 25 words · KbWen

Keras Cifar-10

這次來用Keras建立CNN,辨識Cifar-10影像資料 Cifar-10 是32*32 RBG的圖形,裡面包含了10種,像是飛機、狗、貓等等 可以看成是MNIST的困難版 因此在Preprocess的時候做的事情都是一樣的,並進行one hot encoding 其中在convolution選擇兩層,kernal 3*3 ,same padding maxpooling 是2*2的大小,在接上 NN從4096–1024–10(最後輸出) 可以注意一下Keras 和 Tensorflow一些參數表現的不同 這是Cifar-10的圖像 利用pandas建立confusion matrix,來看出是不是混淆了某些類別。 可以看出第三類(cat)和第五類(dog)容易混淆,以及動物類和交通工具類不太容易混淆 兩層CNN準確率:0.732 My Github

2017-07-06 · 1 min · 25 words · KbWen