-
Notifications
You must be signed in to change notification settings - Fork 65
/
model.py
133 lines (120 loc) · 7.16 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import tensorflow as tf
import numpy as np
class NTMCopyModel():
def __init__(self, args, seq_length, reuse=False):
self.x = tf.placeholder(name='x', dtype=tf.float32, shape=[args.batch_size, seq_length, args.vector_dim])
self.y = self.x
eof = np.zeros([args.batch_size, args.vector_dim + 1])
eof[:, args.vector_dim] = np.ones([args.batch_size])
eof = tf.constant(eof, dtype=tf.float32)
zero = tf.constant(np.zeros([args.batch_size, args.vector_dim + 1]), dtype=tf.float32)
if args.model == 'LSTM':
# single_cell = tf.nn.rnn_cell.BasicLSTMCell(args.rnn_size)
# cannot use [single_cell] * 3 in tensorflow 1.2
def rnn_cell(rnn_size):
return tf.nn.rnn_cell.BasicLSTMCell(rnn_size, reuse=reuse)
cell = tf.nn.rnn_cell.MultiRNNCell([rnn_cell(args.rnn_size) for _ in range(args.rnn_num_layers)])
elif args.model == 'NTM':
import ntm.ntm_cell as ntm_cell
cell = ntm_cell.NTMCell(args.rnn_size, args.memory_size, args.memory_vector_dim, 1, 1,
addressing_mode='content_and_location',
reuse=reuse,
output_dim=args.vector_dim)
state = cell.zero_state(args.batch_size, tf.float32)
self.state_list = [state]
for t in range(seq_length):
output, state = cell(tf.concat([self.x[:, t, :], np.zeros([args.batch_size, 1])], axis=1), state)
self.state_list.append(state)
output, state = cell(eof, state)
self.state_list.append(state)
self.o = []
for t in range(seq_length):
output, state = cell(zero, state)
self.o.append(output[:, 0:args.vector_dim])
self.state_list.append(state)
self.o = tf.sigmoid(tf.transpose(self.o, perm=[1, 0, 2]))
# self.copy_loss = tf.reduce_mean(tf.reduce_sum(tf.square(self.y - self.o), reduction_indices=[1, 2]))
eps = 1e-8
self.copy_loss = -tf.reduce_mean( # cross entropy function
self.y * tf.log(self.o + eps) + (1 - self.y) * tf.log(1 - self.o + eps)
)
with tf.variable_scope('optimizer', reuse=reuse):
self.optimizer = tf.train.RMSPropOptimizer(learning_rate=args.learning_rate, momentum=0.9, decay=0.95)
gvs = self.optimizer.compute_gradients(self.copy_loss)
capped_gvs = [(tf.clip_by_value(grad, -10., 10.), var) for grad, var in gvs]
self.train_op = self.optimizer.apply_gradients(capped_gvs)
self.copy_loss_summary = tf.summary.scalar('copy_loss_%d' % seq_length, self.copy_loss)
# self.merged_summary = tf.summary.merge(self.copy_loss_summary)
class NTMOneShotLearningModel():
def __init__(self, args):
if args.label_type == 'one_hot':
args.output_dim = args.n_classes
elif args.label_type == 'five_hot':
args.output_dim = 25
self.x_image = tf.placeholder(dtype=tf.float32,
shape=[args.batch_size, args.seq_length, args.image_width * args.image_height])
self.x_label = tf.placeholder(dtype=tf.float32,
shape=[args.batch_size, args.seq_length, args.output_dim])
self.y = tf.placeholder(dtype=tf.float32,
shape=[args.batch_size, args.seq_length, args.output_dim])
if args.model == 'LSTM':
def rnn_cell(rnn_size):
return tf.nn.rnn_cell.BasicLSTMCell(rnn_size)
cell = tf.nn.rnn_cell.MultiRNNCell([rnn_cell(args.rnn_size) for _ in range(args.rnn_num_layers)])
elif args.model == 'NTM':
import ntm.ntm_cell as ntm_cell
cell = ntm_cell.NTMCell(args.rnn_size, args.memory_size, args.memory_vector_dim,
read_head_num=args.read_head_num,
write_head_num=args.write_head_num,
addressing_mode='content_and_location',
output_dim=args.output_dim)
elif args.model == 'MANN':
import ntm.mann_cell as mann_cell
cell = mann_cell.MANNCell(args.rnn_size, args.memory_size, args.memory_vector_dim,
head_num=args.read_head_num)
elif args.model == 'MANN2':
import ntm.mann_cell_2 as mann_cell
cell = mann_cell.MANNCell(args.rnn_size, args.memory_size, args.memory_vector_dim,
head_num=args.read_head_num)
state = cell.zero_state(args.batch_size, tf.float32)
self.state_list = [state] # For debugging
self.o = []
for t in range(args.seq_length):
output, state = cell(tf.concat([self.x_image[:, t, :], self.x_label[:, t, :]], axis=1), state)
# output, state = cell(self.y[:, t, :], state)
with tf.variable_scope("o2o", reuse=(t > 0)):
o2o_w = tf.get_variable('o2o_w', [output.get_shape()[1], args.output_dim],
initializer=tf.random_uniform_initializer(minval=-0.1, maxval=0.1))
# initializer=tf.random_normal_initializer(mean=0.0, stddev=0.1))
o2o_b = tf.get_variable('o2o_b', [args.output_dim],
initializer=tf.random_uniform_initializer(minval=-0.1, maxval=0.1))
# initializer=tf.random_normal_initializer(mean=0.0, stddev=0.1))
output = tf.nn.xw_plus_b(output, o2o_w, o2o_b)
if args.label_type == 'one_hot':
output = tf.nn.softmax(output, dim=1)
elif args.label_type == 'five_hot':
output = tf.stack([tf.nn.softmax(o) for o in tf.split(output, 5, axis=1)], axis=1)
self.o.append(output)
self.state_list.append(state)
self.o = tf.stack(self.o, axis=1)
self.state_list.append(state)
eps = 1e-8
if args.label_type == 'one_hot':
self.learning_loss = -tf.reduce_mean( # cross entropy function
tf.reduce_sum(self.y * tf.log(self.o + eps), axis=[1, 2])
)
elif args.label_type == 'five_hot':
self.learning_loss = -tf.reduce_mean( # cross entropy function
tf.reduce_sum(tf.stack(tf.split(self.y, 5, axis=2), axis=2) * tf.log(self.o + eps), axis=[1, 2, 3])
)
self.o = tf.reshape(self.o, shape=[args.batch_size, args.seq_length, -1])
self.learning_loss_summary = tf.summary.scalar('learning_loss', self.learning_loss)
with tf.variable_scope('optimizer'):
self.optimizer = tf.train.AdamOptimizer(learning_rate=args.learning_rate)
# self.optimizer = tf.train.RMSPropOptimizer(
# learning_rate=args.learning_rate, momentum=0.9, decay=0.95
# )
# gvs = self.optimizer.compute_gradients(self.learning_loss)
# capped_gvs = [(tf.clip_by_value(grad, -10., 10.), var) for grad, var in gvs]
# self.train_op = self.optimizer.apply_gradients(gvs)
self.train_op = self.optimizer.minimize(self.learning_loss)