-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
53 lines (42 loc) · 1.94 KB
/
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
import tensorflow as tf
class Model(object):
def __init__(self, sequence_length, num_classes, vocab_size, embedding_size,
position_size, pretrained_embedding, wpe, hparams):
# data parameters
self.sequence_length = sequence_length
self.num_classes = num_classes
self.vocab_size = vocab_size
self.embedding_size = embedding_size
self.position_size = position_size
self.pretrained_embedding = pretrained_embedding
self.wpe = wpe
# required params
self.l2_reg_lambda = hparams.l2_reg_lambda
self.lr = hparams.lr
self.batch_size = hparams.batch_size
self.num_epochs = hparams.num_epochs
# global step for tensorflow
self.global_step = tf.Variable(0, name="global_step", trainable=False)
def add_placeholders(self):
raise NotImplementedError("Each Model must re-implement this method.")
def create_feed_dict(self, inputs_batch, labels_batch=None):
raise NotImplementedError("Each Model must re-implement this method.")
def add_prediction_op(self):
raise NotImplementedError("Each Model must re-implement this method.")
def add_loss_op(self, pred):
raise NotImplementedError("Each Model must re-implement this method.")
def add_training_op(self, loss):
raise NotImplementedError("Each Model must re-implement this method.")
def train_on_batch(self, sess, inputs_batch, labels_batch):
feed = self.create_feed_dict(inputs_batch, labels_batch=labels_batch)
_, loss = sess.run([self.train_op, self.loss], feed_dict=feed)
return loss
def predict_on_batch(self, sess, inputs_batch):
feed = self.create_feed_dict(inputs_batch)
predictions = sess.run(self.pred, feed_dict=feed)
return predictions
def build(self):
self.add_placeholders()
self.add_prediction_op()
self.add_loss_op()
self.add_training_op()