forked from google-deepmind/deepmind-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
258 lines (221 loc) · 9.97 KB
/
main.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Lint as: python2, python3
# pylint: disable=g-bad-file-header
# Copyright 2019 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Batched synchronous actor/learner training."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
from absl import app
from absl import flags
from absl import logging
import numpy as np
from six.moves import range
from six.moves import zip
import tensorflow.compat.v1 as tf
from tvt import batch_env
from tvt import nest_utils
from tvt import rma
from tvt import tvt_rewards as tvt_module
from tvt.pycolab import env as pycolab_env
from tensorflow.contrib import framework as contrib_framework
nest = contrib_framework.nest
FLAGS = flags.FLAGS
flags.DEFINE_integer('logging_frequency', 1,
'Log training progress every logging_frequency episodes.')
flags.DEFINE_string('logdir', None, 'Directory for tensorboard logging.')
flags.DEFINE_boolean('with_memory', True,
'whether or not agent has external memory.')
flags.DEFINE_boolean('with_reconstruction', True,
'whether or not agent reconstruct the observation.')
flags.DEFINE_float('gamma', 0.92, 'Agent discount factor')
flags.DEFINE_float('entropy_cost', 0.05, 'weight of the entropy loss')
flags.DEFINE_float('image_cost_weight', 50., 'image recon cost weight.')
flags.DEFINE_float('read_strength_cost', 5e-5,
'Cost weight of the memory read strength.')
flags.DEFINE_float('read_strength_tolerance', 2.,
'The tolerance of hinge loss of the read_strength_cost.')
flags.DEFINE_boolean('do_tvt', True, 'whether or not do tvt')
flags.DEFINE_enum('pycolab_game', 'key_to_door',
['key_to_door', 'active_visual_match'],
'The name of the game in pycolab environment')
flags.DEFINE_integer('num_episodes', None,
'Number of episodes to train for. None means run forever.')
flags.DEFINE_integer('batch_size', 16, 'Batch size')
flags.DEFINE_float('learning_rate', 2e-4, 'Adam optimizer learning rate')
flags.DEFINE_float('beta1', 0., 'Adam optimizer beta1')
flags.DEFINE_float('beta2', 0.95, 'Adam optimizer beta2')
flags.DEFINE_float('epsilon', 1e-6, 'Adam optimizer epsilon')
# Pycolab-specific flags:
flags.DEFINE_integer('pycolab_num_apples', 10,
'Number of apples to sample from the distractor grid.')
flags.DEFINE_float('pycolab_apple_reward_min', 1.,
'A reward range [min, max) to uniformly sample from.')
flags.DEFINE_float('pycolab_apple_reward_max', 10.,
'A reward range [min, max) to uniformly sample from.')
flags.DEFINE_boolean('pycolab_fix_apple_reward_in_episode', True,
'Fix the sampled apple reward within an episode.')
flags.DEFINE_float('pycolab_final_reward', 10.,
'Reward obtained at the last phase.')
flags.DEFINE_boolean('pycolab_crop', True,
'Whether to crop observations or not.')
def main(_):
batch_size = FLAGS.batch_size
env_builder = pycolab_env.PycolabEnvironment
env_kwargs = {
'game': FLAGS.pycolab_game,
'num_apples': FLAGS.pycolab_num_apples,
'apple_reward': [FLAGS.pycolab_apple_reward_min,
FLAGS.pycolab_apple_reward_max],
'fix_apple_reward_in_episode': FLAGS.pycolab_fix_apple_reward_in_episode,
'final_reward': FLAGS.pycolab_final_reward,
'crop': FLAGS.pycolab_crop
}
env = batch_env.BatchEnv(batch_size, env_builder, **env_kwargs)
ep_length = env.episode_length
agent = rma.Agent(batch_size=batch_size,
num_actions=env.num_actions,
observation_shape=env.observation_shape,
with_reconstructions=FLAGS.with_reconstruction,
gamma=FLAGS.gamma,
read_strength_cost=FLAGS.read_strength_cost,
read_strength_tolerance=FLAGS.read_strength_tolerance,
entropy_cost=FLAGS.entropy_cost,
with_memory=FLAGS.with_memory,
image_cost_weight=FLAGS.image_cost_weight)
# Agent step placeholders and agent step.
batch_shape = (batch_size,)
observation_ph = tf.placeholder(
dtype=tf.uint8, shape=batch_shape + env.observation_shape, name='obs')
reward_ph = tf.placeholder(
dtype=tf.float32, shape=batch_shape, name='reward')
state_ph = nest.map_structure(
lambda s: tf.placeholder(dtype=s.dtype, shape=s.shape, name='state'),
agent.initial_state(batch_size=batch_size))
step_outputs, state = agent.step(reward_ph, observation_ph, state_ph)
# Update op placeholders and update op.
observations_ph = tf.placeholder(
dtype=tf.uint8, shape=(ep_length + 1, batch_size) + env.observation_shape,
name='observations')
rewards_ph = tf.placeholder(
dtype=tf.float32, shape=(ep_length + 1, batch_size), name='rewards')
actions_ph = tf.placeholder(
dtype=tf.int64, shape=(ep_length, batch_size), name='actions')
tvt_rewards_ph = tf.placeholder(
dtype=tf.float32, shape=(ep_length, batch_size), name='tvt_rewards')
loss, loss_logs = agent.loss(
observations_ph, rewards_ph, actions_ph, tvt_rewards_ph)
optimizer = tf.train.AdamOptimizer(
learning_rate=FLAGS.learning_rate,
beta1=FLAGS.beta1,
beta2=FLAGS.beta2,
epsilon=FLAGS.epsilon)
update_op = optimizer.minimize(loss)
initial_state = agent.initial_state(batch_size)
if FLAGS.logdir:
if not tf.io.gfile.exists(FLAGS.logdir):
tf.io.gfile.makedirs(FLAGS.logdir)
summary_writer = tf.summary.FileWriter(FLAGS.logdir)
# Do init
init_ops = (tf.global_variables_initializer(),
tf.local_variables_initializer())
tf.get_default_graph().finalize()
sess = tf.Session()
sess.run(init_ops)
run = True
ep_num = 0
prev_logging_time = time.time()
while run:
observation, reward = env.reset()
agent_state = sess.run(initial_state)
# Initialise episode data stores.
observations = [observation]
rewards = [reward]
actions = []
baselines = []
read_infos = []
for _ in range(ep_length):
step_feed = {reward_ph: reward, observation_ph: observation}
for ph, ar in zip(nest.flatten(state_ph), nest.flatten(agent_state)):
step_feed[ph] = ar
step_output, agent_state = sess.run(
(step_outputs, state), feed_dict=step_feed)
action = step_output.action
baseline = step_output.baseline
read_info = step_output.read_info
# Take step in environment, append results.
observation, reward = env.step(action)
observations.append(observation)
rewards.append(reward)
actions.append(action)
baselines.append(baseline)
if read_info is not None:
read_infos.append(read_info)
# Stack the lists of length ep_length so that each array (or each element
# of nest stucture for read_infos) has shape (ep_length, batch_size, ...).
observations = np.stack(observations)
rewards = np.array(rewards)
actions = np.array(actions)
baselines = np.array(baselines)
read_infos = nest_utils.nest_stack(read_infos)
# Compute TVT rewards.
if FLAGS.do_tvt:
tvt_rewards = tvt_module.compute_tvt_rewards(read_infos,
baselines,
gamma=FLAGS.gamma)
else:
tvt_rewards = np.squeeze(np.zeros_like(baselines))
# Run update op.
loss_feed = {observations_ph: observations,
rewards_ph: rewards,
actions_ph: actions,
tvt_rewards_ph: tvt_rewards}
ep_loss, _, ep_loss_logs = sess.run([loss, update_op, loss_logs],
feed_dict=loss_feed)
# Log episode results.
if ep_num % FLAGS.logging_frequency == 0:
steps_per_second = (
FLAGS.logging_frequency * ep_length * batch_size / (
time.time() - prev_logging_time))
mean_reward = np.mean(np.sum(rewards, axis=0))
mean_last_phase_reward = np.mean(env.last_phase_rewards())
mean_tvt_reward = np.mean(np.sum(tvt_rewards, axis=0))
logging.info('Episode %d. SPS: %s', ep_num, steps_per_second)
logging.info('Episode %d. Mean episode reward: %f', ep_num, mean_reward)
logging.info('Episode %d. Last phase reward: %f', ep_num,
mean_last_phase_reward)
logging.info('Episode %d. Mean TVT episode reward: %f', ep_num,
mean_tvt_reward)
logging.info('Episode %d. Loss: %s', ep_num, ep_loss)
logging.info('Episode %d. Loss logs: %s', ep_num, ep_loss_logs)
if FLAGS.logdir:
summary = tf.Summary()
summary.value.add(tag='reward', simple_value=mean_reward)
summary.value.add(tag='last phase reward',
simple_value=mean_last_phase_reward)
summary.value.add(tag='tvt reward', simple_value=mean_tvt_reward)
summary.value.add(tag='total loss', simple_value=ep_loss)
for k, v in ep_loss_logs.items():
summary.value.add(tag='loss - {}'.format(k), simple_value=v)
# Tensorboard x-axis is total number of episodes run.
summary_writer.add_summary(summary, ep_num * batch_size)
summary_writer.flush()
prev_logging_time = time.time()
ep_num += 1
if FLAGS.num_episodes and ep_num >= FLAGS.num_episodes:
run = False
if __name__ == '__main__':
app.run(main)