-
Notifications
You must be signed in to change notification settings - Fork 10
/
pointer_model.py
104 lines (62 loc) · 2.86 KB
/
pointer_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib.rnn import BasicLSTMCell, MultiRNNCell, LSTMCell
class Pointer_Model(object):
"""docstring for Question_Generator"""
def __init__(self, config):
self.task = config.task
self.debug = config.debug
self.config = config
self.input_dim = config.input_dim
self.hidden_dim = config.hidden_dim
self.num_layers = config.num_layers
self.max_enc_length = config.max_enc_length
self.max_dec_length = config.max_dec_length
self.num_glimpse = config.num_glimpse
self.init_min_val = config.init_min_val
self.init_max_val = config.init_max_val
self.initializer = tf.random_uniform_initializer(self.init_min_val, self.init_max_val)
self.use_terminal_symbol = config.use_terminal_symbol
self.lr_start = config.lr_start
self.lr_decay_step = config.lr_decay_step
self.lr_decay_rate = config.lr_decay_rate
self.max_grad_norm = config.max_grad_norm
self.layer_dict = {}
self.encoder_inputs = tf.placeholder()
self.decoder_targets = tf.placeholder()
self.encoder_seq_length = tf.placeholder()
self.decoder_seq_length = tf.placeholder()
self.mask = tf.placeholder()
if self.use_terminal_symbol:
self.decoder_seq_length +=1
def _build_model(self, inputs):
self.global_step = tf.Variable(0, trainable = False)
with tf.variable_scope('embedding'):
self.embedding = tf.get_variable(name = 'embedding' , shape = [self.vocab_size, self.embedding_dim],
initializer = self.initializer)
self.embedding_lookup = tf.nn.embedding_lookup()
with tf.variable_scope('encoder'):
encoder_cell = LSTMCell(self.hidden_dim,
initializer = self.initializer)
if self.num_layers>1:
cells = [encoder_cell] * self.num_layers
encoder_cell = MultiRNNCell(cells)
self.encoder_outputs, self.encoder_final_state = tf.nn.dynamic_rnn(encoder_cell, self.encoder_inputs, sequence_length = self.encoder_seq_length)
with tf.variable_scope('decoder'):
self.decoder_cell = LSTMCell(self.hidden_dim, initializer = self.initializer)
if self.num_layers > 1:
cells = [self.decoder_cell] * self.num_layers
self.decoder_cell = MultiRNNCell(cells)
#self.decoder_rnn = tf.contrib.seq2seq.Decoder()
self.decoder_pred_logits , _ = decoder_rnn.step(, )
self.dec_pred_prob = tf.nn.softmax(
self.dec_pred_logits, 2, name="dec_pred_prob")
self.dec_pred = tf.argmax(
self.dec_pred_logits, 2, name="dec_pred")
with tf.variable_scope('decoder', reuse = True):
self.decoder_pred_logits , _ =
self.dec_inference_prob = tf.nn.softmax(
self.dec_inference_logits, 2, name="dec_inference_logits")
self.dec_inference = tf.argmax(
self.dec_inference_logits, 2, name="dec_inference")
def _build_optim(self):