Tensorflow模型保存与加载
Tensorflow模型保存与加载
1 | import os |
1 | def preprocess(x, y): |
1 | batchsz = 128 |
datasets: (60000, 28, 28) (60000,) 0 255
(128, 784) (128, 10)
1.保存权值
模型创建
1 | network = Sequential([layers.Dense(256, activation='relu'), |
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) multiple 200960
_________________________________________________________________
dense_1 (Dense) multiple 32896
_________________________________________________________________
dense_2 (Dense) multiple 8256
_________________________________________________________________
dense_3 (Dense) multiple 2080
_________________________________________________________________
dense_4 (Dense) multiple 330
=================================================================
Total params: 244,522
Trainable params: 244,522
Non-trainable params: 0
_________________________________________________________________
模型训练
1 | network.compile(optimizer=optimizers.Adam(lr=0.01), |
Epoch 1/3
469/469 [==============================] - 10s 22ms/step - loss: 0.2594 - accuracy: 0.9232
Epoch 2/3
469/469 [==============================] - 12s 25ms/step - loss: 0.1406 - accuracy: 0.9619 - val_loss: 0.1288 - val_accuracy: 0.9629
Epoch 3/3
469/469 [==============================] - 9s 20ms/step - loss: 0.1076 - accuracy: 0.9703
79/79 [==============================] - 3s 39ms/step - loss: 0.1237 - accuracy: 0.9689
[0.12368294241494805, 0.9689]
保存权值 删除之前创建的网络
network.save_weights
1 | network.save_weights('weights.ckpt') |
saved weights.
创建与之前结构相同的网络 并加载保存的权值
1 | network = Sequential([layers.Dense(256, activation='relu'), |
加载权值 评估网络效果
network.load_weights
1 | network.load_weights('weights.ckpt') |
loaded weights!
79/79 [==============================] - 3s 40ms/step - loss: 0.1237 - accuracy: 0.9689
[0.12368294241494805, 0.9689]
2.保存模型
保存模型的所有信息 效率较低 重新加载前无需创建与之前结构相同的网络
模型创建&训练
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
_________________________________________________________________
Epoch 1/2
469/469 [==============================] - 11s 23ms/step - loss: 0.2920 - accuracy: 0.9103
Epoch 2/2
469/469 [==============================] - 13s 28ms/step - loss: 0.1434 - accuracy: 0.9591 - val_loss: 0.1399 - val_accuracy: 0.9636
79/79 [==============================] - 4s 46ms/step - loss: 0.1399 - accuracy: 0.9636
[0.1399224520914398, 0.9636]
保存模型所有信息
network.save
1 | network.save('model.h5') |
saved total model.
加载模型 评估效果
tf.keras.models.load_model
1 | print('loaded model from file.') |
loaded model from file.
WARNING:tensorflow:Sequential models without an `input_shape` passed to the first layer cannot reload their optimizer state. As a result, your model isstarting with a freshly initialized optimizer.
79/79 [==============================] - 3s 35ms/step - loss: 0.1399 - accuracy: 0.9636
[0.1399224520914398, 0.9636]
1 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 没有胡子的猫Asimok!
评论