-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbase_model.py
57 lines (47 loc) · 2.08 KB
/
base_model.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
import tensorflow as tf
class BaseModel:
def __init__(self, config):
self.config = config
self.summaries = None
# global tensors
self.cur_epoch_tensor = None
self.cur_epoch_input = None
self.cur_epoch_assign_op = None
self.global_step_tensor = None
self.global_step_input = None
self.global_step_assign_op = None
# init the global step, global time step, the current epoch and the summaries
self.init_global_step()
self.init_cur_epoch()
def init_saver(self):
self.saver = tf.train.Saver(max_to_keep=self.config.max_to_keep)
def save(self,sess):
print("Saving model...")
self.saver.save(sess, self.config.checkpoint_dir, self.global_step_tensor)
print("Model saved")
def load(self, sess):
latest_checkpoint = tf.train.latest_checkpoint(self.config.checkpoint_dir)
if latest_checkpoint:
print("Loading model checkpoint {} ...\n".format(latest_checkpoint))
self.saver.restore(sess, latest_checkpoint)
print("Model loaded")
def init_cur_epoch(self):
"""
Create cur epoch tensor to totally save the process of the training
:return:
"""
with tf.variable_scope('cur_epoch'):
self.cur_epoch_tensor = tf.Variable(0, trainable=False, name='cur_epoch')
self.cur_epoch_input = tf.placeholder('int32', None, name='cur_epoch_input')
self.cur_epoch_assign_op = self.cur_epoch_tensor.assign(self.cur_epoch_input)
def init_global_step(self):
"""
Create a global step variable to be a reference to the number of iterations
:return:
"""
with tf.variable_scope('global_step'):
self.global_step_tensor = tf.Variable(0, trainable=False, name='global_step')
self.global_step_input = tf.placeholder('int32', None, name='global_step_input')
self.global_step_assign_op = self.global_step_tensor.assign(self.global_step_input)
def build_model(self):
raise NotImplementedError