tensorflow保存模型到本地
#import import tensorflow as tf #导入框架 import numpy as np #导入科学计算库 import os #调用路径 # #train, test # mnist = tf.keras.datasets.mnist #加载手写识别数据集 # # (x_train, y_train), (x_test, y_test) = mnist.load_data() #分配训练集和测试集的输入特征和标签 # # x_train, x_test = x_train / 255.0, x_test / 255.0 #输入特征进行归一化处理 #第二步,train, test # mnist = tf.keras.datasets.mnist #导入mnist数据集 # (x_train, y_train), (x_test, y_test) = mnist.load_data() #分别配置训练集和测试集的输入和标签 path = './mnist.npz' #数据路径 f = np.load(path) #加载数据 x_train, y_train = f['x_train'], f['y_train'] #导入训练集的输入和标签 x_test, y_test = f['x_test'], f['y_test'] #导入测试集的输入和标签 f.close() x_train, x_test = x_train/255.0, x_test/255.0 #把输入特征做归一化处理,让值在0-1之间,更容易让神经网络吸收 #model.Sequential() model = tf.keras.models.Sequential([ #实现前向传播 tf.keras.layers.Flatten(), #拉成一维 tf.keras.layers.Dense(128, activation = 'relu'), #全连接网络,128个神经元,激活函数为relu tf.keras.layers.Dense(10, activation = 'softmax') #全连接网络,10个神经元,激活函数为softmax ]) #model.compile() model.compile( #配置训练方式 optimizer = 'adam', #使用adam优化器 loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False), #配置损失函数,输出是概率分布 metrics = ['sparse_categorical_accuracy'] #评测指标方法 ) checkpoint_save_path = './checkpoint/mnist.ckpt' #模型保存地址 if os.path.exists(checkpoint_save_path + '.index'): #判断模型是否存在 print('---------------加载模型------------------') #如果模型存在就打印加载模型 model.load_weights(checkpoint_save_path) #加载模型 #保存模型 cp_callback = tf.keras.callbacks.ModelCheckpoint( #保存模型 filepath = checkpoint_save_path, #模型保存地址 save_weights_only = True, save_best_only = True ) histroy = model.fit( #执行训练过程 x_train, y_train, batch_size = 32, epochs = 5, validation_data = (x_test, y_test), validation_freq = 1, callbacks = [cp_callback] ) #model.summary model.summary #打印参数
结果为:(这里我的模型已经存在了,所以出现如下结果)
E:\Anaconda3\envs\TF2\python.exe C:/Users/Administrator/PycharmProjects/untitled8/保存模型.py
---------------加载模型------------------
Train on 60000 samples, validate on 10000 samples
Epoch 1/532/60000 [..............................] - ETA: 55:02 - loss: 0.1279 - sparse_categorical_accuracy: 0.9688
256/60000 [..............................] - ETA: 7:03 - loss: 0.0318 - sparse_categorical_accuracy: 0.9961
448/60000 [..............................] - ETA: 4:08 - loss: 0.0272 - sparse_categorical_accuracy: 0.9978
640/60000 [..............................] - ETA: 2:58 - loss: 0.0310 - sparse_categorical_accuracy: 0.9969
832/60000 [..............................] - ETA: 2:20 - loss: 0.0284 - sparse_categorical_accuracy: 0.9964
1024/60000 [..............................] - ETA: 1:57 - loss: 0.0261 - sparse_categorical_accuracy: 0.9961
省略.......
59296/60000 [============================>.] - ETA: 0s - loss: 0.0130 - sparse_categorical_accuracy: 0.9962
59648/60000 [============================>.] - ETA: 0s - loss: 0.0130 - sparse_categorical_accuracy: 0.9962
60000/60000 [==============================] - 9s 156us/sample - loss: 0.0131 - sparse_categorical_accuracy: 0.9961 - val_loss: 0.0823 - val_sparse_categorical_accuracy: 0.9793