-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
70 lines (48 loc) · 2.12 KB
/
train.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
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import logging
import os
import tensorflow as tf
import utils
from model import Model
from utils import read_data
from flags import parse_args
FLAGS, unparsed = parse_args()
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s', level=logging.DEBUG)
vocabulary = read_data(FLAGS.text)
print('Data size', len(vocabulary))
with open(FLAGS.dictionary, encoding='utf-8') as inf:
dictionary = json.load(inf, encoding='utf-8')
with open(FLAGS.reverse_dictionary, encoding='utf-8') as inf:
reverse_dictionary = json.load(inf, encoding='utf-8')
model = Model(learning_rate=FLAGS.learning_rate, batch_size=FLAGS.batch_size, num_steps=FLAGS.num_steps)
model.build()
with tf.Session() as sess:
summary_string_writer = tf.summary.FileWriter(FLAGS.output_dir, sess.graph)
saver = tf.train.Saver(max_to_keep=5)
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
logging.debug('Initialized')
try:
checkpoint_path = tf.train.latest_checkpoint(FLAGS.output_dir)
saver.restore(sess, checkpoint_path)
logging.debug('restore from [{0}]'.format(checkpoint_path))
except Exception:
logging.debug('no check point found....')
for x in range(1):
logging.debug('epoch [{0}]....'.format(x))
state = sess.run(model.state_tensor)
for dl in utils.get_train_data(vocabulary, batch_size=FLAGS.batch_size, num_steps=FLAGS.num_steps):
##################
# Your Code here
##################
gs, _, state, l, summary_string = sess.run(
[model.global_step, model.optimizer, model.outputs_state_tensor, model.loss, model.merged_summary_op], feed_dict=feed_dict)
summary_string_writer.add_summary(summary_string, gs)
if gs % 10 == 0:
logging.debug('step [{0}] loss [{1}]'.format(gs, l))
save_path = saver.save(sess, os.path.join(
FLAGS.output_dir, "model.ckpt"), global_step=gs)
summary_string_writer.close()