您好,登錄后才能下訂單哦!
這篇文章主要介紹了TensorFlow2之Fashion Mnist的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Fashion Mnist 是一個(gè)類似于 Mnist 的圖像數(shù)據(jù)集. 涵蓋 10 種類別的 7 萬 (6 萬訓(xùn)練集 + 1 萬測(cè)試集) 個(gè)不同商品的圖片.
Tensorboard 是 tensorflow 的一個(gè)可視化工具.
我們可以通過tf.summary.create_file_writer(file_path)
來創(chuàng)建一個(gè)新的 summary 實(shí)例.
例子:
# 將當(dāng)前時(shí)間作為子文件名 current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") # 監(jiān)聽的文件的路徑 log_dir = 'logs/' + current_time # 創(chuàng)建writer summary_writer = tf.summary.create_file_writer(log_dir)
通過tf.summary.scalar
我們可以向 summary 對(duì)象存入數(shù)據(jù).
格式:
tf.summary.scalar( name, data, step=None, description=None )
例子:
with summary_writer.as_default(): tf.summary.scalar("train-loss", float(Cross_Entropy), step=step)
metrics.Mean()
可以幫助我們計(jì)算平均數(shù).
格式:
tf.keras.metrics.Mean( name='mean', dtype=None )
例子:
# 準(zhǔn)確率表 loss_meter = tf.keras.metrics.Mean()
格式:
tf.keras.metrics.Accuracy( name='accuracy', dtype=None )
例子:
# 損失表 acc_meter = tf.keras.metrics.Accuracy()
我們可以通過update_state
來實(shí)現(xiàn)變量更新, 通過rest_state
來實(shí)現(xiàn)變量重置.
例如:
# 跟新?lián)p失 loss_meter.update_state(Cross_Entropy) # 重置 loss_meter.reset_state()
def pre_process(x, y): """ 數(shù)據(jù)預(yù)處理 :param x: 特征值 :param y: 目標(biāo)值 :return: 返回處理好的x, y """ # 轉(zhuǎn)換x x = tf.cast(x, tf.float32) / 255 x = tf.reshape(x, [-1, 784]) # 轉(zhuǎn)換y y = tf.cast(y, dtype=tf.int32) y = tf.one_hot(y, depth=10) return x, y
def get_data(): """ 獲取數(shù)據(jù) :return: 返回分批完的訓(xùn)練集和測(cè)試集 """ # 獲取數(shù)據(jù) (X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data() # 分割訓(xùn)練集 train_db = tf.data.Dataset.from_tensor_slices((X_train, y_train)).shuffle(60000, seed=0) train_db = train_db.batch(batch_size).map(pre_process) # 分割測(cè)試集 test_db = tf.data.Dataset.from_tensor_slices((X_test, y_test)).shuffle(10000, seed=0) test_db = test_db.batch(batch_size).map(pre_process) # 返回 return train_db, test_db
def train(epoch, train_db): """ 訓(xùn)練數(shù)據(jù) :param train_db: 分批的數(shù)據(jù)集 :return: 無返回值 """ for step, (x, y) in enumerate(train_db): with tf.GradientTape() as tape: # 獲取模型輸出結(jié)果 logits = model(x) # 計(jì)算交叉熵 Cross_Entropy = tf.losses.categorical_crossentropy(y, logits, from_logits=True) Cross_Entropy = tf.reduce_sum(Cross_Entropy) # 跟新?lián)p失 loss_meter.update_state(Cross_Entropy) # 計(jì)算梯度 grads = tape.gradient(Cross_Entropy, model.trainable_variables) # 跟新參數(shù) optimizer.apply_gradients(zip(grads, model.trainable_variables)) # 每100批調(diào)試輸出一下誤差 if step % 100 == 0: print("step:", step, "Cross_Entropy:", loss_meter.result().numpy()) # 重置 loss_meter.reset_state() # 可視化 with summary_writer.as_default(): tf.summary.scalar("train-loss", float(Cross_Entropy), step= epoch * 235 + step)
def test(epoch, test_db): """ 測(cè)試模型 :param epoch: 輪數(shù) :param test_db: 分批的測(cè)試集 :return: 無返回值 """ # 重置 acc_meter.reset_state() for x, y in test_db: # 獲取模型輸出結(jié)果 logits = model(x) # 預(yù)測(cè)結(jié)果 pred = tf.argmax(logits, axis=1) # 從one_hot編碼變回來 y = tf.argmax(y, axis=1) # 計(jì)算準(zhǔn)確率 acc_meter.update_state(y, pred) # 調(diào)試輸出 print("epoch:", epoch + 1, "Accuracy:", acc_meter.result().numpy() * 100, "%", ) # 可視化 with summary_writer.as_default(): tf.summary.scalar("val-acc", acc_meter.result().numpy(), step=epoch * 235)
def main(): """ 主函數(shù) :return: 無返回值 """ # 獲取數(shù)據(jù) train_db, test_db = get_data() # 輪期 for epoch in range(iteration_num): train(epoch, train_db) test(epoch, test_db)
import datetime import tensorflow as tf # 定義超參數(shù) batch_size = 256 # 一次訓(xùn)練的樣本數(shù)目 learning_rate = 0.001 # 學(xué)習(xí)率 iteration_num = 20 # 迭代次數(shù) # 優(yōu)化器 optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate) # 準(zhǔn)確率表 loss_meter = tf.keras.metrics.Mean() # 損失表 acc_meter = tf.keras.metrics.Accuracy() # 可視化 current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") log_dir = 'logs/' + current_time summary_writer = tf.summary.create_file_writer(log_dir) # 創(chuàng)建writer # 模型 model = tf.keras.Sequential([ tf.keras.layers.Dense(256, activation=tf.nn.relu), tf.keras.layers.Dense(128, activation=tf.nn.relu), tf.keras.layers.Dense(64, activation=tf.nn.relu), tf.keras.layers.Dense(32, activation=tf.nn.relu), tf.keras.layers.Dense(10) ]) # 調(diào)試輸出summary model.build(input_shape=[None, 28 * 28]) print(model.summary()) def pre_process(x, y): """ 數(shù)據(jù)預(yù)處理 :param x: 特征值 :param y: 目標(biāo)值 :return: 返回處理好的x, y """ # 轉(zhuǎn)換x x = tf.cast(x, tf.float32) / 255 x = tf.reshape(x, [-1, 784]) # 轉(zhuǎn)換y y = tf.cast(y, dtype=tf.int32) y = tf.one_hot(y, depth=10) return x, y def get_data(): """ 獲取數(shù)據(jù) :return: 返回分批完的訓(xùn)練集和測(cè)試集 """ # 獲取數(shù)據(jù) (X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data() # 分割訓(xùn)練集 train_db = tf.data.Dataset.from_tensor_slices((X_train, y_train)).shuffle(60000, seed=0) train_db = train_db.batch(batch_size).map(pre_process) # 分割測(cè)試集 test_db = tf.data.Dataset.from_tensor_slices((X_test, y_test)).shuffle(10000, seed=0) test_db = test_db.batch(batch_size).map(pre_process) # 返回 return train_db, test_db def train(epoch, train_db): """ 訓(xùn)練數(shù)據(jù) :param train_db: 分批的數(shù)據(jù)集 :return: 無返回值 """ for step, (x, y) in enumerate(train_db): with tf.GradientTape() as tape: # 獲取模型輸出結(jié)果 logits = model(x) # 計(jì)算交叉熵 Cross_Entropy = tf.losses.categorical_crossentropy(y, logits, from_logits=True) Cross_Entropy = tf.reduce_sum(Cross_Entropy) # 跟新?lián)p失 loss_meter.update_state(Cross_Entropy) # 計(jì)算梯度 grads = tape.gradient(Cross_Entropy, model.trainable_variables) # 跟新參數(shù) optimizer.apply_gradients(zip(grads, model.trainable_variables)) # 每100批調(diào)試輸出一下誤差 if step % 100 == 0: print("step:", step, "Cross_Entropy:", loss_meter.result().numpy()) # 重置 loss_meter.reset_state() # 可視化 with summary_writer.as_default(): tf.summary.scalar("train-loss", float(Cross_Entropy), step=epoch * 235 + step) def test(epoch, test_db): """ 測(cè)試模型 :param epoch: 輪數(shù) :param test_db: 分批的測(cè)試集 :return: 無返回值 """ # 重置 acc_meter.reset_state() for x, y in test_db: # 獲取模型輸出結(jié)果 logits = model(x) # 預(yù)測(cè)結(jié)果 pred = tf.argmax(logits, axis=1) # 從one_hot編碼變回來 y = tf.argmax(y, axis=1) # 計(jì)算準(zhǔn)確率 acc_meter.update_state(y, pred) # 調(diào)試輸出 print("epoch:", epoch + 1, "Accuracy:", acc_meter.result().numpy() * 100, "%", ) # 可視化 with summary_writer.as_default(): tf.summary.scalar("val-acc", acc_meter.result().numpy(), step=epoch * 235) def main(): """ 主函數(shù) :return: 無返回值 """ # 獲取數(shù)據(jù) train_db, test_db = get_data() # 輪期 for epoch in range(iteration_num): train(epoch, train_db) test(epoch, test_db) if __name__ == "__main__": main()
輸出結(jié)果:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 256) 200960
_________________________________________________________________
dense_1 (Dense) (None, 128) 32896
_________________________________________________________________
dense_2 (Dense) (None, 64) 8256
_________________________________________________________________
dense_3 (Dense) (None, 32) 2080
_________________________________________________________________
dense_4 (Dense) (None, 10) 330
=================================================================
Total params: 244,522
Trainable params: 244,522
Non-trainable params: 0
_________________________________________________________________
None
2021-06-14 18:01:27.399812: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
step: 0 Cross_Entropy: 591.5974
step: 100 Cross_Entropy: 196.49309
step: 200 Cross_Entropy: 125.2562
epoch: 1 Accuracy: 84.72999930381775 %
step: 0 Cross_Entropy: 107.64579
step: 100 Cross_Entropy: 105.854385
step: 200 Cross_Entropy: 99.545975
epoch: 2 Accuracy: 85.83999872207642 %
step: 0 Cross_Entropy: 95.42945
step: 100 Cross_Entropy: 91.366234
step: 200 Cross_Entropy: 90.84072
epoch: 3 Accuracy: 86.69999837875366 %
step: 0 Cross_Entropy: 82.03317
step: 100 Cross_Entropy: 83.20552
step: 200 Cross_Entropy: 81.57012
epoch: 4 Accuracy: 86.11000180244446 %
step: 0 Cross_Entropy: 82.94046
step: 100 Cross_Entropy: 77.56677
step: 200 Cross_Entropy: 76.996346
epoch: 5 Accuracy: 87.27999925613403 %
step: 0 Cross_Entropy: 75.59219
step: 100 Cross_Entropy: 71.70899
step: 200 Cross_Entropy: 74.15144
epoch: 6 Accuracy: 87.29000091552734 %
step: 0 Cross_Entropy: 76.65844
step: 100 Cross_Entropy: 70.09151
step: 200 Cross_Entropy: 70.84446
epoch: 7 Accuracy: 88.27999830245972 %
step: 0 Cross_Entropy: 67.50707
step: 100 Cross_Entropy: 64.85907
step: 200 Cross_Entropy: 68.63099
epoch: 8 Accuracy: 88.41999769210815 %
step: 0 Cross_Entropy: 65.50318
step: 100 Cross_Entropy: 62.2706
step: 200 Cross_Entropy: 63.80803
epoch: 9 Accuracy: 86.21000051498413 %
step: 0 Cross_Entropy: 66.95486
step: 100 Cross_Entropy: 61.84385
step: 200 Cross_Entropy: 62.18851
epoch: 10 Accuracy: 88.45999836921692 %
step: 0 Cross_Entropy: 59.779297
step: 100 Cross_Entropy: 58.602314
step: 200 Cross_Entropy: 59.837025
epoch: 11 Accuracy: 88.66000175476074 %
step: 0 Cross_Entropy: 58.10068
step: 100 Cross_Entropy: 55.097878
step: 200 Cross_Entropy: 59.906315
epoch: 12 Accuracy: 88.70999813079834 %
step: 0 Cross_Entropy: 57.584858
step: 100 Cross_Entropy: 54.95376
step: 200 Cross_Entropy: 55.797752
epoch: 13 Accuracy: 88.44000101089478 %
step: 0 Cross_Entropy: 53.54782
step: 100 Cross_Entropy: 53.62939
step: 200 Cross_Entropy: 54.632828
epoch: 14 Accuracy: 87.02999949455261 %
step: 0 Cross_Entropy: 54.387398
step: 100 Cross_Entropy: 52.323734
step: 200 Cross_Entropy: 53.968185
epoch: 15 Accuracy: 88.98000121116638 %
step: 0 Cross_Entropy: 50.468914
step: 100 Cross_Entropy: 50.79311
step: 200 Cross_Entropy: 51.296227
epoch: 16 Accuracy: 88.67999911308289 %
step: 0 Cross_Entropy: 48.753258
step: 100 Cross_Entropy: 46.809692
step: 200 Cross_Entropy: 48.08208
epoch: 17 Accuracy: 89.10999894142151 %
step: 0 Cross_Entropy: 46.830627
step: 100 Cross_Entropy: 47.208813
step: 200 Cross_Entropy: 48.671318
epoch: 18 Accuracy: 88.77999782562256 %
step: 0 Cross_Entropy: 46.15514
step: 100 Cross_Entropy: 45.026627
step: 200 Cross_Entropy: 45.371685
epoch: 19 Accuracy: 88.7399971485138 %
step: 0 Cross_Entropy: 47.696465
step: 100 Cross_Entropy: 41.52749
step: 200 Cross_Entropy: 46.71362
epoch: 20 Accuracy: 89.56000208854675 %
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“TensorFlow2之Fashion Mnist的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。