-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_template.py
69 lines (47 loc) · 1.6 KB
/
model_template.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
import tensorflow as tf
#初始化变量和模型参数,定义训练闭环中的运算
def inference(X):
#计算推断模型在数据X上的输出,并将结果返回
return
def loss(X,Y):
#依据训练数据X及期期望输出Y计算损失
return
def inputs():
#读取或生成训练数据X及期望输出Y
return
def train(total_loss):
#依据计算的总损失训练或调整模型参数
return
def evaluate(sess,X,Y):
#对训练得到的模型 进行评估
return
#创建检查点Saver对象
saver = tf.train.Saver()
save_path = "my_model"
with tf.Session() as sess:
tf.initialize_all_variables().run()
X,Y = inputs()
initial_step = 0
#验证之前是否已经保存了检查点文件
ckpt = tf.train.get_checkpoint_state(save_path)
if ckpt and ckpt.model_checkpoint_path:
#从检查点恢复
saver.restore(sess,ckpt.model_checkpoint_path)
initial_step = int(ckpt.model_checkpoint_path.rsplit('-',1)[1])
total_loss = loss(X,Y)
train_op = train(total_loss)
coord = tf.train.Coodinator()
threads = tf.train.start_queue_runners(sess = sess,coord = coord)
#实际训练迭代次数
training_steps = 1000
for step in range(initial_step,training_steps):
sess.run(train_op)
if step % 10 == 0:
print "loss:",sess.run([total_loss])
saver.save(sess,save_path,global_step = step)
evaluate(sess,X,Y)
coord.request_stop()
coord.join(threads)
saver.save(sess,save_path,global_step = training_steps)
sess.close()