一:需重定义神经网络继续训练的方法
1.训练代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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.恢复上一次的训练
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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",参数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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"),就可以直接使用了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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))
|
总的来说,下面这种方法好像是要便利一些
以上这篇tensorflow如何继续训练之前保存的模型实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/by_side_with_sun/article/details/79829619