Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 0s 0us/step
定义模型
我们来构建一个简单的模型,以演示如何保存和加载权重。
# Returns a short sequential model defcreate_model(): model = tf.keras.models.Sequential([ keras.layers.Dense(512, activation=tf.nn.relu, input_shape=(784,)), keras.layers.Dropout(0.2), keras.layers.Dense(10, activation=tf.nn.softmax) ])
model.fit(train_images, train_labels, epochs = 10, validation_data = (test_images,test_labels), callbacks = [cp_callback]) # pass callback to training
Train on 1000 samples, validate on 1000 samples Epoch 1/10 960/1000 [===========================>..] - ETA: 0s - loss: 1.2667 - acc: 0.6531 Epoch 00001: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 421us/step - loss: 1.2358 - acc: 0.6620 - val_loss: 0.7326 - val_acc: 0.7760 Epoch 2/10 928/1000 [==========================>...] - ETA: 0s - loss: 0.4499 - acc: 0.8696 Epoch 00002: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 165us/step - loss: 0.4324 - acc: 0.8760 - val_loss: 0.5149 - val_acc: 0.8430 Epoch 3/10 832/1000 [=======================>......] - ETA: 0s - loss: 0.2870 - acc: 0.9255 Epoch 00003: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 177us/step - loss: 0.2847 - acc: 0.9240 - val_loss: 0.4527 - val_acc: 0.8570 Epoch 4/10 864/1000 [========================>.....] - ETA: 0s - loss: 0.2065 - acc: 0.9572 Epoch 00004: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 174us/step - loss: 0.2011 - acc: 0.9560 - val_loss: 0.4250 - val_acc: 0.8680 Epoch 5/10 864/1000 [========================>.....] - ETA: 0s - loss: 0.1503 - acc: 0.9676 Epoch 00005: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 178us/step - loss: 0.1601 - acc: 0.9620 - val_loss: 0.4080 - val_acc: 0.8660 Epoch 6/10 864/1000 [========================>.....] - ETA: 0s - loss: 0.1068 - acc: 0.9861 Epoch 00006: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 175us/step - loss: 0.1076 - acc: 0.9850 - val_loss: 0.4124 - val_acc: 0.8650 Epoch 7/10 864/1000 [========================>.....] - ETA: 0s - loss: 0.0856 - acc: 0.9861 Epoch 00007: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 174us/step - loss: 0.0837 - acc: 0.9880 - val_loss: 0.4060 - val_acc: 0.8680 Epoch 8/10 864/1000 [========================>.....] - ETA: 0s - loss: 0.0626 - acc: 0.9965 Epoch 00008: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 172us/step - loss: 0.0616 - acc: 0.9970 - val_loss: 0.4055 - val_acc: 0.8720 Epoch 9/10 832/1000 [=======================>......] - ETA: 0s - loss: 0.0475 - acc: 1.0000 Epoch 00009: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 176us/step - loss: 0.0455 - acc: 1.0000 - val_loss: 0.3973 - val_acc: 0.8730 Epoch 10/10 864/1000 [========================>.....] - ETA: 0s - loss: 0.0394 - acc: 0.9965 Epoch 00010: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7fb8028c2630>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.
Consider using a TensorFlow optimizer from tf.train. 1000/1000 [==============================] - 0s 176us/step - loss: 0.0389 - acc: 0.9960 - val_loss: 0.4114 - val_acc: 0.8720
# include the epoch in the file name. (uses `str.format`) checkpoint_path = "training_2/cp-{epoch:04d}.ckpt" checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint( checkpoint_path, verbose=1, save_weights_only=True, # Save weights, every 5-epochs. period=5)
Epoch 00005: saving model to training_2/cp-0005.ckpt Epoch 00010: saving model to training_2/cp-0010.ckpt Epoch 00015: saving model to training_2/cp-0015.ckpt Epoch 00020: saving model to training_2/cp-0020.ckpt Epoch 00025: saving model to training_2/cp-0025.ckpt Epoch 00030: saving model to training_2/cp-0030.ckpt Epoch 00035: saving model to training_2/cp-0035.ckpt Epoch 00040: saving model to training_2/cp-0040.ckpt Epoch 00050: saving model to training_2/cp-0050.ckpt