tensorflow.keras.Model compile fit evaluate应用
tensorflow.keras.Model compile fit evaluate应用
1 | import tensorflow as tf |
数据预处理
1 | def preprocess(x, y): |
1 | batchsz = 128 |
datasets: (60000, 28, 28) (60000,) 0 255
(128, 784) (128, 10)
模型构建
1 | network = Sequential([layers.Dense(256, activation='relu'), |
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_10 (Dense) multiple 200960
_________________________________________________________________
dense_11 (Dense) multiple 32896
_________________________________________________________________
dense_12 (Dense) multiple 8256
_________________________________________________________________
dense_13 (Dense) multiple 2080
_________________________________________________________________
dense_14 (Dense) multiple 330
=================================================================
Total params: 244,522
Trainable params: 244,522
Non-trainable params: 0
_________________________________________________________________
使用compile设置模型的优化器,损失函数,metrics
1 | network.compile(optimizer=optimizers.Adam(lr=0.01), |
使用fit配置模型的训练数据集,训练轮次,验证数据集,验证频次
1 | network.fit(db, epochs=5, validation_data=ds_val, validation_freq=2) |
Epoch 1/5
469/469 [==============================] - 11s 24ms/step - loss: 0.2834 - accuracy: 0.9153
Epoch 2/5
469/469 [==============================] - 12s 27ms/step - loss: 0.1356 - accuracy: 0.9628 - val_loss: 0.1399 - val_accuracy: 0.9614
Epoch 3/5
469/469 [==============================] - 9s 19ms/step - loss: 0.1134 - accuracy: 0.9696
Epoch 4/5
469/469 [==============================] - 12s 26ms/step - loss: 0.1001 - accuracy: 0.9736 - val_loss: 0.1208 - val_accuracy: 0.9685
Epoch 5/5
469/469 [==============================] - 10s 21ms/step - loss: 0.0846 - accuracy: 0.9773
<tensorflow.python.keras.callbacks.History at 0x7f893e44de90>
使用evaluate评估模型
1 | network.evaluate(ds_val) |
79/79 [==============================] - 3s 36ms/step - loss: 0.1085 - accuracy: 0.9738
[0.10845260255486716, 0.9738]
使用predict预测
1 | sample = next(iter(ds_val)) |
tf.Tensor(
[7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 0 7 2 7
1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 9 6 4 3 0 7 0 2 9
1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9 6 0 5 4 9 9 2 1 9 4 8
7 3 9 7 9 4 4 9 2 5 4 7 6 7 9 0 5], shape=(128,), dtype=int64)
tf.Tensor(
[7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7
1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4 6 4 3 0 7 0 2 9
1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9 6 0 5 4 9 9 2 1 9 4 8
7 3 9 7 4 4 4 9 2 5 4 7 6 7 9 0 5], shape=(128,), dtype=int64)
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 没有胡子的猫Asimok!
评论