天天看點

TensorFlow深度學習2--手寫數學圖檔識别初體驗

1、 手寫數字圖檔資料集

TensorFlow深度學習2--手寫數學圖檔識别初體驗

手寫數字圖檔資料集,它包含了0~9共10種數字的手寫圖檔,采集自不同書寫風格的真實手寫圖檔,一共70000張圖檔。其中60000張圖檔作為訓練集,用來訓練模型,剩下10000張圖檔作為測試集,用來預測或者測試,訓練集和測試集共同組成了整個MNIST資料集。

2、 模型搭建

(x, y), (x_val, y_val) = datasets.mnist.load_data()  #加載MNIST資料集
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.    #轉換為浮點數
y = tf.convert_to_tensor(y, dtype=tf.int32)            #轉換為整形張量
y = tf.one_hot(y, depth=10)                  #one-hot 編碼
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))     #建構資料集
train_dataset = train_dataset.batch(200)                       #批量訓練
#利用Sequential容器封裝3個網絡層,前網絡層的輸出預設作為下一層的輸入
model = keras.Sequential([ 
    layers.Dense(512, activation='relu'),
    layers.Dense(256, activation='relu'),
    layers.Dense(10)])
optimizer = optimizers.SGD(learning_rate=0.001)
           

3、 模型訓練

def train_epoch(epoch):
    losses=[]
    # Step4.loop
    for step, (x, y) in enumerate(train_dataset):
        with tf.GradientTape() as tape:    #建構梯度記錄環境
            # 打平操作:[b, 28, 28] => [b, 784]
            x = tf.reshape(x, (-1, 28*28))
            # Step1. compute output
            # [b, 784] => [b, 10]
            out = model(x)
            # Step2. compute loss
            loss = tf.reduce_sum(tf.square(out - y)) / x.shape[0]
        # Step3. 自動計算參數的梯度  w1, w2, w3, b1, b2, b3
        grads = tape.gradient(loss, model.trainable_variables)
        # w' = w - lr * grad  更新網絡參數
        optimizer.apply_gradients(zip(grads, model.trainable_variables))
        if step % 100 == 0:
            print(epoch, step, 'loss:', loss.numpy())
    return loss
           

4、 模型運作

def train():
    losses = []
    for epoch in range(50):
        loss=train_epoch(epoch)
        losses.append(float(loss))
    return  losses
if __name__ == '__main__':
    losses =train()
    plt.figure()
    x = [i  for i in range(len(losses))]
    plt.plot(x, losses, color='C0', marker='s', label='訓練')
    plt.ylabel('MSE')
    plt.xlabel('Epoch')
    plt.legend()
    plt.savefig('train.png')
    plt.show()
           

5、 MNIST資料集的訓練誤差曲線

TensorFlow深度學習2--手寫數學圖檔識别初體驗

繼續閱讀