In [1]:
import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from sklearn.preprocessing import StandardScaler import pandas as pdIn [2]:
(x_train_all, y_train_all), (x_test, y_test) = tf.keras.datasets.mnist.load_data()In [3]:
x_valid, x_train = x_train_all[:5000], x_train_all[5000:] y_valid, y_train = y_train_all[:5000], y_train_all[5000:]In [4]:
scaler = StandardScaler() x_train.shapeOut[4]:
(55000, 28, 28)In [5]:
x_train = x_train.astype(np.float32).reshape(-1, 1) x_train.shapeOut[5]:
(43120000, 1)In [6]:
x_train_scaled = scaler.fit_transform(x_train) x_train_scaledOut[6]:
array([[-0.4241323], [-0.4241323], [-0.4241323], ..., [-0.4241323], [-0.4241323], [-0.4241323]], dtype=float32)In [7]:
x_train_scaled = x_train_scaled.reshape(-1, 28, 28) display(x_train_scaled.shape) x_train_scaled
(55000, 28, 28)Out[7]:
array([[[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], ..., [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323]], [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], ..., [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323]], [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], ..., [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323]], ..., [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], ..., [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323]], [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], ..., [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323]], [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], ..., [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323], [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323, -0.4241323, -0.4241323]]], dtype=float32)In [8]:
x_valid_scaled = scaler.fit_transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1, 28, 28) x_test_scaled = scaler.fit_transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1, 28, 28)In [9]:
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=[28, 28]), # 定义输入数据的格式 tf.keras.layers.Dense(512, activation='relu', kernel_regularizer='l2'), # tf.keras.layers.AlphaDropout(0.5), 也可以取消正则化,使用改进版的dropout(均值和方差不变,归一化性质也不变) tf.keras.layers.Dense(256, activation='relu', kernel_regularizer='l2'), tf.keras.layers.Dense(128, activation='relu', kernel_regularizer='l2'), tf.keras.layers.Dense(10, activation='softmax') ]) # 配置模型 model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])In [10]:
history = model.fit(x_train_scaled, y_train, epochs=10, validation_data=(x_valid_scaled, y_valid))
Epoch 1/10 1719/1719 [==============================] - 5s 3ms/step - loss: 8.6183 - accuracy: 0.8999 - val_loss: 6.0358 - val_accuracy: 0.9376 Epoch 2/10 1719/1719 [==============================] - 5s 3ms/step - loss: 4.4725 - accuracy: 0.9445 - val_loss: 3.2236 - val_accuracy: 0.9510 Epoch 3/10 1719/1719 [==============================] - 5s 3ms/step - loss: 2.4441 - accuracy: 0.9506 - val_loss: 1.8074 - val_accuracy: 0.9582 Epoch 4/10 1719/1719 [==============================] - 5s 3ms/step - loss: 1.4214 - accuracy: 0.9556 - val_loss: 1.0957 - val_accuracy: 0.9600 Epoch 5/10 1719/1719 [==============================] - 5s 3ms/step - loss: 0.9005 - accuracy: 0.9595 - val_loss: 0.7379 - val_accuracy: 0.9586 Epoch 6/10 1719/1719 [==============================] - 5s 3ms/step - loss: 0.6334 - accuracy: 0.9615 - val_loss: 0.5429 - val_accuracy: 0.9670 Epoch 7/10 1719/1719 [==============================] - 5s 3ms/step - loss: 0.4934 - accuracy: 0.9639 - val_loss: 0.4449 - val_accuracy: 0.9680 Epoch 8/10 1719/1719 [==============================] - 5s 3ms/step - loss: 0.4187 - accuracy: 0.9654 - val_loss: 0.3877 - val_accuracy: 0.9688 Epoch 9/10 1719/1719 [==============================] - 5s 3ms/step - loss: 0.3773 - accuracy: 0.9665 - val_loss: 0.3654 - val_accuracy: 0.9682 Epoch 10/10 1719/1719 [==============================] - 5s 3ms/step - loss: 0.3529 - accuracy: 0.9679 - val_loss: 0.3481 - val_accuracy: 0.9684In [11]:
history.historyOut[11]:
{'loss': [8.618253707885742, 4.4725117683410645, 2.4440758228302, 1.4214469194412231, 0.9005103707313538, 0.633385956287384, 0.49344661831855774, 0.4187164306640625, 0.3772663474082947, 0.3529081642627716], 'accuracy': [0.8999454379081726, 0.9445454478263855, 0.9505817890167236, 0.9556363821029663, 0.9594908952713013, 0.961509108543396, 0.9638727307319641, 0.9654181599617004, 0.9665091037750244, 0.9678727388381958], 'val_loss': [6.035829067230225, 3.2235751152038574, 1.8073712587356567, 1.0957262516021729, 0.7379411458969116, 0.5428724884986877, 0.4449375569820404, 0.38767603039741516, 0.36538875102996826, 0.34807276725769043], 'val_accuracy': [0.9376000165939331, 0.9509999752044678, 0.9581999778747559, 0.9599999785423279, 0.9585999846458435, 0.9670000076293945, 0.9679999947547913, 0.9688000082969666, 0.9682000279426575, 0.9684000015258789]}In [12]:
pd.DataFrame(history.history).plot(figsize=(8,5)) plt.grid() plt.gca().set_ylim(0,1) plt.show()In [13]:
model.evaluate(x_test_scaled, y_test)
313/313 [==============================] - 0s 1ms/step - loss: 0.3486 - accuracy: 0.9644Out[13]:
[0.34860044717788696, 0.9643999934196472]In [14]:
model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten (Flatten) (None, 784) 0 dense (Dense) (None, 512) 401920 dense_1 (Dense) (None, 256) 131328 dense_2 (Dense) (None, 128) 32896 dense_3 (Dense) (None, 10) 1290 ================================================================= Total params: 567434 (2.16 MB) Trainable params: 567434 (2.16 MB) Non-trainable params: 0 (0.00 Byte) _________________________________________________________________In [ ]: 标签:loss,val,...,0.4241323,train,TensorFlow,手写,识别,accuracy From: https://www.cnblogs.com/funsion/p/18679462