-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.py
executable file
·221 lines (187 loc) · 12 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import tensorflow as tf
from layers.convolution import conv2d_transpose
from layers.action_conditional_lstm import actionlstm_cell
class RESModel:
def __init__(self, config):
"""
:param config: configration object
"""
self.config = config
self.summaries = None
self.is_training = tf.placeholder(tf.bool, name='is_training')
with tf.name_scope('train_inputs'):
self.initial_lstm_state = tf.placeholder(tf.float32, [2, None, self.config.lstm_size],
name='lstm_initial_state')
self.x = tf.placeholder(tf.float32, [None, self.config.truncated_time_steps] + self.config.state_size,
name='states')
self.y = tf.placeholder(tf.int32, [None, self.config.truncated_time_steps] + self.config.labels_size,
name='next_states')
self.rewards = tf.placeholder(tf.float32, [None, self.config.truncated_time_steps, 1],
name='rewards')
self.actions = tf.placeholder(tf.float32, [None, self.config.truncated_time_steps, self.config.action_dim],
name='actions')
with tf.name_scope('test_inputs'):
self.x_test = tf.placeholder(tf.float32, [None] + self.config.state_size,
name='states_test')
self.initial_lstm_state_test = tf.placeholder(tf.float32, [2, None, self.config.lstm_size],
name='lstm_state_test')
self.actions_test = tf.placeholder(tf.float32, [None, self.config.action_dim],
name='actions_test')
def template(self, x, action, lstm_state):
"""
:param x: input tensor of shape: [None, truncated_time_steps ] + self.config.state_size
:param action: input tensor of shape:[None, truncated_time_steps, action_dim]
:param lstm_state: input tensor of shape: [2, lstm_size, lstm_size]
:return: the output and the lstm hidden state
"""
with tf.name_scope('encoder_1'):
h1 = tf.layers.conv2d(x, 64, kernel_size=(8, 8), strides=(2, 2),
kernel_initializer=tf.contrib.layers.xavier_initializer(), padding='SAME')
bn1 = tf.layers.batch_normalization(h1, training=self.is_training)
drp1 = tf.layers.dropout(tf.nn.relu(bn1), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('encoder_2'):
h2 = tf.layers.conv2d(drp1, 32, kernel_size=(6, 6), strides=(2, 2),
kernel_initializer=tf.contrib.layers.xavier_initializer(), padding='SAME')
bn2 = tf.layers.batch_normalization(h2, training=self.is_training)
drp2 = tf.layers.dropout(tf.nn.relu(bn2), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('encoder_3'):
h3 = tf.layers.conv2d(drp2, 32, kernel_size=(6, 6), strides=(2, 2),
kernel_initializer=tf.contrib.layers.xavier_initializer(), padding='SAME')
bn3 = tf.layers.batch_normalization(h3, training=self.is_training)
drp3 = tf.layers.dropout(tf.nn.relu(bn3), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('encoder_4'):
h4 = tf.layers.conv2d(drp3, 32, kernel_size=(4, 4), strides=(2, 2),
kernel_initializer=tf.contrib.layers.xavier_initializer(), padding='SAME')
bn4 = tf.layers.batch_normalization(h4, training=self.is_training)
drp4 = tf.layers.dropout(tf.nn.relu(bn4), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('flatten_1'):
encoded = tf.contrib.layers.flatten(drp4)
# the size of encodded vector
encoded_vector_size = encoded.get_shape()[1]
with tf.name_scope('lstm_layer') as scope:
lstm_out, lstm_new_state = actionlstm_cell(encoded, lstm_state, action, self.config.lstm_size,
self.config.action_dim,
initializer=tf.contrib.layers.xavier_initializer(),
activation=tf.tanh, scope='lstm_layer')
with tf.name_scope('hidden_layer_1'):
h5 = tf.layers.dense(lstm_out, encoded_vector_size,
kernel_initializer=tf.contrib.layers.xavier_initializer())
bn5 = tf.layers.batch_normalization(h5, training=self.is_training)
drp5 = tf.layers.dropout(tf.nn.relu(bn5), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('reshape_1'):
# the last encoder conv layer shape
deconv_init_shape = drp4.get_shape().as_list()
reshaped_drp4 = tf.reshape(drp5, [-1] + deconv_init_shape[1:])
with tf.name_scope('decoder_1'):
h6 = conv2d_transpose('decoder1', reshaped_drp4,
output_shape=[self.config.batch_size, self.config.state_size[0] // 8,
self.config.state_size[1] // 8, 32], kernel_size=(4, 4),
stride=(2, 2),
)
bn6 = tf.layers.batch_normalization(h6, training=self.is_training)
drp6 = tf.layers.dropout(tf.nn.relu(bn6), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('decoder_2'):
h7 = conv2d_transpose('decoder2', drp6,
output_shape=[self.config.batch_size, self.config.state_size[0] // 4,
self.config.state_size[1] // 4, 32], kernel_size=(6, 6),
stride=(2, 2),
)
bn7 = tf.layers.batch_normalization(h7, training=self.is_training)
drp7 = tf.layers.dropout(tf.nn.relu(bn7), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('decoder_3'):
h8 = conv2d_transpose('decoder3', drp7,
output_shape=[self.config.batch_size, self.config.state_size[0] // 2,
self.config.state_size[1] // 2, 32], kernel_size=(6, 6),
stride=(2, 2),
)
bn8 = tf.layers.batch_normalization(h8, training=self.is_training)
drp8 = tf.layers.dropout(tf.nn.relu(bn8), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('decoder_4'):
h9 = conv2d_transpose('decoder4', x=drp8,
output_shape=[self.config.batch_size, self.config.state_size[0],
self.config.state_size[1], 64], kernel_size=(8, 8),
stride=(2, 2),
)
bn9 = tf.layers.batch_normalization(h9, training=self.is_training)
drp9 = tf.layers.dropout(tf.nn.relu(bn9), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('decoder_5'):
next_state_out = tf.layers.conv2d(drp9, 2, kernel_size=(3, 3), strides=(1, 1),
kernel_initializer=tf.contrib.layers.xavier_initializer(), padding='SAME')
next_state_out_softmax = tf.nn.softmax(next_state_out)
if self.config.predict_reward:
with tf.name_scope('reward_flatten'):
flattened_drp7 = tf.contrib.layers.flatten(drp7)
with tf.name_scope('reward_hidden_layer_2'):
h7_2 = tf.layers.dense(flattened_drp7, 128, kernel_initializer=tf.contrib.layers.xavier_initializer())
drp7_2 = tf.layers.dropout(tf.nn.relu(h7_2), rate=self.config.dropout_rate, training=self.is_training,
name='dropout')
with tf.name_scope('reward_output_layer'):
reward_out = tf.layers.dense(drp7_2, 1, activation=None,
kernel_initializer=tf.contrib.layers.xavier_initializer())
else:
reward_out = None
return next_state_out, next_state_out_softmax, reward_out, lstm_new_state
def build_model(self):
net_unwrap = []
net_softmax_unwrap = []
reward_unwrap = []
self.network_template = tf.make_template('network', self.template)
lstm_state = tf.contrib.rnn.LSTMStateTuple(self.initial_lstm_state[0], self.initial_lstm_state[1])
for i in range(self.config.truncated_time_steps):
if i >= self.config.observation_steps_length:
state_out, next_state_out_softmax, reward_out, lstm_state = self.network_template(
next_state_out_softmax,
self.actions[:, i],
lstm_state)
else:
state_out, next_state_out_softmax, reward_out, lstm_state = self.network_template(self.x[:, i, :],
self.actions[:, i],
lstm_state)
if self.config.predict_reward:
reward_unwrap.append(reward_out)
net_unwrap.append(state_out)
net_softmax_unwrap.append(next_state_out_softmax)
else:
net_unwrap.append(state_out)
net_softmax_unwrap.append(next_state_out_softmax)
self.final_lstm_state = lstm_state
with tf.name_scope('wrap_out'):
net_unwrap = tf.stack(net_unwrap)
self.output = tf.transpose(net_unwrap, [1, 0, 2, 3, 4])
net_softmax_unwrap = tf.stack(net_softmax_unwrap)
self.output_softmax = tf.transpose(net_softmax_unwrap, [1, 0, 2, 3, 4])
if self.config.predict_reward:
reward_unwrap = tf.stack(reward_unwrap)
self.reward_output = tf.stack(reward_unwrap)
self.reward_output = tf.transpose(self.reward_output, [1, 0, 2])
with tf.name_scope('loss'):
# state loss
self.states_loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.output, labels=self.y))
self.loss = self.states_loss
# adding reward loss
if self.config.predict_reward:
self.reward_loss = tf.losses.mean_squared_error(self.reward_output, self.rewards)
self.loss += self.reward_loss
with tf.name_scope('train_step'):
# for batchnorm layers
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(extra_update_ops):
# RMSProp as in paper
self.train_step = tf.train.RMSPropOptimizer(self.config.learning_rate).minimize(self.loss)
# test_model
lstm_state_test = tf.contrib.rnn.LSTMStateTuple(self.initial_lstm_state_test[0],
self.initial_lstm_state_test[1])
self.output_test, self.output_softmax_test, self.reward_out_test, self.lstm_state_test = self.network_template(
self.x_test,
self.actions_test,
lstm_state_test)