(二)Tensorflow神经网络保存模型(持久化)

1 Tensorflow搭建神经网络

详见:Tensorflow搭建神经网络

2 持久化模型

2.1 小序

Tensorflow训练模型过程中,模型参数只在训练过程中更新及保存,训练完成自动退出,模型未保存,这导致每次测试数据都需要重新训练一次模型,极为耗时且耽误生产,为此Tensorflow提供了模型的持久化接口功能,开发人员在训练好模型后,直接保存,后续的模型训练或迁移学习都可以接着保存的模型进行fine-tuning.

2.2 Demo

import tensorflow as tf
v1 = tf.Variable(tf.constant(1.0, shape=[1]), name='v1')
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name='v2')
result = v1 + v2
# 保存模型的类
saver = tf.train.Saver()
with tf.Session() as sess:
	# 初始化变量
	init_op = tf.global_variable_initializer()
	# 运行初始化
	sess.run(init_op)
	# 保存模型
	saver.save(sess, "modelpath", "modelname")

2.3 模型文件解析

序号文件描述
1checkpoint文本文件,记录最新的模型文件列表
2.data包含训练变量的值value
3.index包含.data和.meta文件对应关系
4.meta包含网络图结构,如GraphDef,SaverDef

2.4 神经网络模型持久化

import tensorflow as tf 
import numpy as np 
import os
import matplotlib.pyplot as plt 

# 保存模型路径及模型名称
MODEL_SAVE_PATH = "./models"
MODEL_NAME = "model.ckpt"
# 模拟数据集
# :params x_data模拟输入
# :params noise模拟噪声
# :params y_data模拟输出
x_data = np.linspace(-1, 1, 250, dtype=np.float32)[:, np.newaxis] 
noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
y_data = np.square(x_data) - 0.5*x_data + noise

# tf变量
xs = tf.placeholder(tf.float32, [None, 1], name='x')
ys = tf.placeholder(tf.float32, [None, 1], name='y')
# 神经网络:输入层->隐藏层
input_size_1 = 1
output_size_1 = 10
# 神经网络:隐藏层->输出层
input_size_2 = 10
output_size_2 = 1
# 权重
weights_1 = tf.Variable(tf.random_normal([input_size_1, output_size_1]), name='weights_1')
weights_2 = tf.Variable(tf.random_normal([input_size_2, output_size_2]), name='weights_2')
# 偏置
biases_1 = tf.Variable(tf.zeros([1, output_size_1]), name='biases_1')
biases_2 = tf.Variable(tf.zeros([1, output_size_2]), name='biases_2')
# 前向计算
# 输入层->隐藏层,加入Relu,非线性化
layer_1 = tf.nn.relu(tf.matmul(xs, weights_1) + biases_1)
# 隐藏层->输出层:预测值
prediction = tf.matmul(layer_1, weights_2) + biases_2
# 损失函数
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
# 反向计算
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# 添加图变量:模型载入使用
tf.add_to_collection('prediction', prediction)
tf.add_to_collection('loss', loss)
# 损失函数
def _loss():
	with tf.Session() as sess:
		# 初始化变量
		init_op = tf.global_variables_initializer()
		# 运行初始化
		sess.run(init_op)
		a = 0
		# 迭代
		for i in range(300):
			sess.run(train_step, feed_dict={xs: x_data, ys: y_data})

			loss2 = sess.run(loss, feed_dict={xs: x_data, ys: y_data})
			lay1 = sess.run(layer_1, feed_dict={xs: x_data})
def _result_():
	# 调用保存模型类
	saver = tf.train.Saver()
	with tf.Session() as sess:
		# 初始化变量
		init_op = tf.global_variables_initializer()
		# 运行初始化
		sess.run(init_op)
		x = sess.run(xs, feed_dict={xs: x_data})
		y = sess.run(ys, feed_dict={ys: y_data})
		a = 0
		# 迭代
		for i in range(300):
			train_step_value, loss_value, pre = sess.run([train_step,loss,prediction], feed_dict={xs: x_data, ys: y_data})
			if i % 50 == 0:
				a += 1
				w1 = sess.run(weights_1)
				w2 = sess.run(weights_2)
				print("Weights_1 :{}".format(w1))
				print("weights_2 :{}".format(w2))
				print("Loss :{}".format(loss_value))
				print(prediction)
				print(loss)
				print(train_step_value)
				pre = sess.run(prediction, feed_dict={xs: x_data})
			# 保存模型:global_step=i,按迭代次数轮数保存,最终保存训练轮数的最后5轮模型
			saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME),global_step=i)
_loss()
_result_()

2.4 模型文件结构

-- models
   |-- checkpoint
   |-- model.ckpt-295.data-00000-of-00001
   |-- model.ckpt-295.index
   |-- model.ckpt-295.meta
   |-- model.ckpt-296.data-00000-of-00001
   |-- model.ckpt-296.index
   |-- model.ckpt-296.meta
   |-- model.ckpt-297.data-00000-of-00001
   |-- model.ckpt-297.index
   |-- model.ckpt-297.meta
   |-- model.ckpt-298.data-00000-of-00001
   |-- model.ckpt-298.index
   |-- model.ckpt-298.meta
   |-- model.ckpt-299.data-00000-of-00001
   |-- model.ckpt-299.index
   |-- model.ckpt-299.meta

3 总结

  • 蛇精网络训练完成后保存模型,该模型可直接使用或继续fine-tuning;
  • 保存模型提高训练效率;
模型载入: Tensorflow神经网络模型载入及迁移学习

[参考文献]
[1]https://blog.csdn.net/Xin_101/article/details/84442097
[2]https://blog.csdn.net/liuxiao214/article/details/79048136


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值