tensorflow2学习笔记一:模型的保存与加载

最近在学习tensorflow框架,参考的tensorflow的官方文档,虽然是英文的,但是很容易读懂,感觉看起来比一些中文文档还易读,而且流程更清晰。在这里通过CSDN平台对自己学习过程进行一下记录与总结,因为自己比较菜,很多东西还处于初级阶段,可能在认知上有些错误,欢迎各位指正,同时也希望能找到一些志同道合的朋友一起学习。


官方在这个例子中用的用的数据库为 MNIST dataset,为了加快运行速度,只用了数据库中的前1000张。

一、调用数据库

调用数据库的代码如下:

import os#后面会用到
import tensorflow as tf
from tensorflow import keras

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_labels = train_labels[:1000]#只取数据的前1000张
test_labels = test_labels[:1000]

train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0#(个人理解)-1代表维数不知道,根据后面需要的维数自动适应
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0

但是我在加载这个数据库的加载不出来,好像是连接不上,然后返回了一个网址,在浏览器中打开就可以直接下载数据库,下载出来是*.npy格式的,我是通过这种方式调用的,效果都是一样的

import os
import tensorflow as tf
import numpy as np
from tensorflow import keras

train_images = np.load('x_train.npy')
test_images = np.load('x_test.npy')

train_labels = np.load('y_train.npy')
test_labels = np.load('y_test.npy')

train_labels = train_labels[:1000]
test_labels = test_labels[:1000]

train_images = train_images[:1000].reshape(-1,28*28)/255.0
test_images = test_images[:1000].reshape(-1,28*28)/255.0

二、定义一个简单的模型

直接接着上面的代码进行添加

#define a simple sequential model
def creat_model():
    model = tf.keras.models.Sequential([
        keras.layers.Dense(512,activation='relu',input_shape=(784,)),
        keras.layers.Dropout(0.2),
        keras.layers.Dense(10)
    ])

    model.compile(optimizer='adam',
                  loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])

    return model

#Creat a basic model instance
model = creat_model()

#display the model's architecture
model.summary()

这个时候就可以运行试一下了,我的运行结果如下:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 512)               401920    
_________________________________________________________________
dropout (Dropout)            (None, 512)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________

三、在训练的时候保存检查点与加载

大致意思应该就是epoch假如有10的话,我们每进行一次epoch就将参数保存一下,防止你的训练过程被打断的话不必重新训练(大概这个意思吧,我英语不太好,可以直接参见官方文档)
代码如下(接着上面的写)

#---------------------------------------------------------------------------
#这边是设置检查点
checkpoint_path = 'train_1/cp.ckpt'
checkpoint_dir = os.path.dirname(checkpoint_path)

#creat a callback that saves the model's weighes
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)

#Train the model with the new callback
model.fit(train_images,
          train_labels,
          epochs=10,
          validation_data=(test_images,test_labels),
          callbacks=[cp_callback])
#------------------------------------------------------------------------------
#这边是模型加载
#ctrat a basic model instance
model = creat_model()
model.load_weights(checkpoint_path)
#evaluate the mosel
loss,acc = model.evaluate(test_images,test_labels,verbose=2)
print("untrained model,accuracy{:5.2f}%".format(100*acc))#{:5.2f}表示有5列,不足的补空格,2f表示小数点后两位

我的运行结果如下:

Epoch 1/10
23/32 [====================>.........] - ETA: 0s - loss: 1.3602 - accuracy: 0.6196
Epoch 00001: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 9ms/step - loss: 1.1518 - accuracy: 0.6720 - val_loss: 0.7157 - val_accuracy: 0.8000
Epoch 2/10
22/32 [===================>..........] - ETA: 0s - loss: 0.4427 - accuracy: 0.8878
Epoch 00002: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.4174 - accuracy: 0.8900 - val_loss: 0.5562 - val_accuracy: 0.8270
Epoch 3/10
21/32 [==================>...........] - ETA: 0s - loss: 0.2768 - accuracy: 0.9315
Epoch 00003: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.2788 - accuracy: 0.9280 - val_loss: 0.4747 - val_accuracy: 0.8540
Epoch 4/10
22/32 [===================>..........] - ETA: 0s - loss: 0.1958 - accuracy: 0.9588
Epoch 00004: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.2036 - accuracy: 0.9470 - val_loss: 0.4270 - val_accuracy: 0.8580
Epoch 5/10
20/32 [=================>............] - ETA: 0s - loss: 0.1656 - accuracy: 0.9578
Epoch 00005: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.1649 - accuracy: 0.9620 - val_loss: 0.4487 - val_accuracy: 0.8580
Epoch 6/10
21/32 [==================>...........] - ETA: 0s - loss: 0.1014 - accuracy: 0.9777
Epoch 00006: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.1113 - accuracy: 0.9750 - val_loss: 0.4263 - val_accuracy: 0.8660
Epoch 7/10
22/32 [===================>..........] - ETA: 0s - loss: 0.0820 - accuracy: 0.9915
Epoch 00007: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.0794 - accuracy: 0.9920 - val_loss: 0.4048 - val_accuracy: 0.8650
Epoch 8/10
22/32 [===================>..........] - ETA: 0s - loss: 0.0511 - accuracy: 0.9957
Epoch 00008: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.0596 - accuracy: 0.9920 - val_loss: 0.3839 - val_accuracy: 0.8700
Epoch 9/10
21/32 [==================>...........] - ETA: 0s - loss: 0.0518 - accuracy: 0.9970
Epoch 00009: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.0510 - accuracy: 0.9980 - val_loss: 0.3953 - val_accuracy: 0.8740
Epoch 10/10
22/32 [===================>..........] - ETA: 0s - loss: 0.0350 - accuracy: 0.9986
Epoch 00010: saving model to train_1/cp.ckpt
32/32 [==============================] - 0s 5ms/step - loss: 0.0354 - accuracy: 0.9990 - val_loss: 0.4144 - val_accuracy: 0.8650
32/32 - 0s - loss: 0.4144 - accuracy: 0.8650
untrained model,accuracy86.50%

这个时候去文件夹看一下就会发现多了几个文件。

四、保存各个阶段的模型??

我的理解就是把每个epoch阶段的模型都保存下来,比如epochs=50,把epoch=5时的模型保存下来,在epoch=10时再保存成另一个文件,这样调用模型的时候任意阶段的模型都可以调用
代码如下:
把第三步的代码注释掉,换成这个。

#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)

#creat a callback that saves the model's weights every 5 epoch
cp_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path,
    verbose=1,
    save_weights_only=True,
    period=5)

#creat a new model instance
model = creat_model()

#save the weights using the 'checkpoint_path' format
#这段代码是手动保存模型
model.save_weights(checkpoint_path.format(epoch=0))

#train the model with the new callback
model.fit(train_images,
          train_labels,
          epochs=50,
          callbacks=[cp_callback],
          validation_data=(test_images,test_labels),
          verbose=0)

运行结果如下

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 00045: saving model to training_2/cp-0045.ckpt

Epoch 00050: saving model to training_2/cp-0050.ckpt

未完待续。。。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值