tensorflow如何继续训练之前保存的模型实例
作者:by_side_with_sun 时间:2023-05-22 22:54:57
一:需重定义神经网络继续训练的方法
1.训练代码
import numpy as np
import tensorflow as tf
x_data=np.random.rand(100).astype(np.float32)
y_data=x_data*0.1+0.3
weight=tf.Variable(tf.random_uniform([1],-1.0,1.0),name="w")
biases=tf.Variable(tf.zeros([1]),name="b")
y=weight*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data)) #loss
optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
saver=tf.train.Saver(max_to_keep=0)
for step in range(10):
sess.run(train)
saver.save(sess,"./save_mode",global_step=step) #保存
print("当前进行:",step)
第一次训练截图:
2.恢复上一次的训练
import numpy as np
import tensorflow as tf
sess=tf.Session()
saver=tf.train.import_meta_graph(r'save_mode-9.meta')
saver.restore(sess,tf.train.latest_checkpoint(r'./'))
print(sess.run("w:0"),sess.run("b:0"))
graph=tf.get_default_graph()
weight=graph.get_tensor_by_name("w:0")
biases=graph.get_tensor_by_name("b:0")
x_data=np.random.rand(100).astype(np.float32)
y_data=x_data*0.1+0.3
y=weight*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data))
optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
saver=tf.train.Saver(max_to_keep=0)
for step in range(10):
sess.run(train)
saver.save(sess,r"./save_new_mode",global_step=step)
print("当前进行:",step," ",sess.run(weight),sess.run(biases))
使用上次保存下的数据进行继续训练和保存:
#最后要提一下的是:
checkpoint文件
meta保存了TensorFlow计算图的结构信息
datat保存每个变量的取值
index保存了 表
加载restore时的文件路径名是以checkpoint文件中的“model_checkpoint_path”值决定的
这个方法需要重新定义神经网络
二:不需要重新定义神经网络的方法:
在上面训练的代码中加入:tf.add_to_collection("name",参数)
import numpy as np
import tensorflow as tf
x_data=np.random.rand(100).astype(np.float32)
y_data=x_data*0.1+0.3
weight=tf.Variable(tf.random_uniform([1],-1.0,1.0),name="w")
biases=tf.Variable(tf.zeros([1]),name="b")
y=weight*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data))
optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
tf.add_to_collection("new_way",train)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
saver=tf.train.Saver(max_to_keep=0)
for step in range(10):
sess.run(train)
saver.save(sess,"./save_mode",global_step=step)
print("当前进行:",step)
在下面的载入代码中加入:tf.get_collection("name"),就可以直接使用了
import numpy as np
import tensorflow as tf
sess=tf.Session()
saver=tf.train.import_meta_graph(r'save_mode-9.meta')
saver.restore(sess,tf.train.latest_checkpoint(r'./'))
print(sess.run("w:0"),sess.run("b:0"))
graph=tf.get_default_graph()
weight=graph.get_tensor_by_name("w:0")
biases=graph.get_tensor_by_name("b:0")
y=tf.get_collection("new_way")[0]
saver=tf.train.Saver(max_to_keep=0)
for step in range(10):
sess.run(y)
saver.save(sess,r"./save_new_mode",global_step=step)
print("当前进行:",step," ",sess.run(weight),sess.run(biases))
总的来说,下面这种方法好像是要便利一些
来源:https://blog.csdn.net/by_side_with_sun/article/details/79829619
标签:tensorflow,训练,模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pandas 条件搜索返回列表的方法
2022-09-16 14:46:34
IPv6设置后如何解决MySQL无法连接localhost的问题
2024-01-20 00:50:38
![](https://img.aspxhome.com/file/2023/1/124821_0s.png)
win10上如何安装mysql5.7.16(解压缩版)
2024-01-23 23:22:53
![](https://img.aspxhome.com/file/2023/2/69862_0s.png)
asp如何制作一个安全的页面?
2010-06-29 21:22:00
Go语言入门13之runtime包案例讲解
2024-02-02 05:54:08
vite.config.js配置入门详解
2024-04-30 10:38:25
![](https://img.aspxhome.com/file/2023/9/130129_0s.jpg)
详解Python中的装饰器、闭包和functools的教程
2023-12-30 13:43:03
详解Python的Django框架中的模版相关知识
2023-04-22 02:46:25
实现web打印的各种方法介绍及实现代码
2024-04-18 09:40:31
sql将一个表中的数据插入到另一个表中的方法
2024-01-27 22:30:20
网页布局设计基础
2008-10-16 13:58:00
MySQL多实例配置方案
2024-01-14 12:55:49
SQL SERVER 中构建执行动态SQL语句的方法
2024-01-22 10:01:46
Python大数据用Numpy Array的原因解读
2023-06-19 05:14:00
![](https://img.aspxhome.com/file/2023/4/91904_0s.jpg)
Anaconda+Pycharm+Pytorch虚拟环境创建(各种包安装保姆级教学)
2023-06-27 23:21:39
![](https://img.aspxhome.com/file/2023/9/85139_0s.png)
Python模拟登陆淘宝并统计淘宝消费情况的代码实例分享
2023-05-01 17:28:00
![](https://img.aspxhome.com/file/2023/8/86218_0s.gif)
PHP闭包定义与使用简单示例
2023-11-23 03:12:15
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
Python离线安装包教程分享
2023-08-03 18:16:40
![](https://img.aspxhome.com/file/2023/4/127704_0s.jpg)
小程序云开发实战小结
2024-06-18 12:12:03
![](https://img.aspxhome.com/file/2023/1/56451_0s.png)