-
Notifications
You must be signed in to change notification settings - Fork 0
/
TF_Agents_Reinforcement_Learning.py
277 lines (217 loc) · 7.2 KB
/
TF_Agents_Reinforcement_Learning.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# %% [markdown]
# <a href="https://colab.research.google.com/github/vochicong/ai-memo/blob/master/TF_Agents_Reinforcement_Learning.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
# %% [markdown]
# # TensorFlow Agentsで強化学習してみよう
# %% [markdown]
# ## 準備
# %% [markdown]
# ### gym や TF Agentsなどのインストール
# %%
!apt-get install -qq xvfb python-opengl
!pip install 'gym==0.10.11'
!pip install imageio
!pip install PILLOW
!pip install pyglet
!pip install pyvirtualdisplay
!pip install tf-agents-nightly
!pip install tf-nightly
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import base64
import gym
import imageio
import IPython
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
import pyvirtualdisplay
import tensorflow as tf
from tf_agents.agents.dqn import dqn_agent
from tf_agents.agents.dqn import q_network
from tf_agents.drivers import dynamic_episode_driver
from tf_agents.drivers import dynamic_step_driver
from tf_agents.environments import suite_gym
from tf_agents.environments import tf_py_environment
from tf_agents.environments import trajectory
from tf_agents.metrics import metric_utils
from tf_agents.metrics import tf_metrics
from tf_agents.policies import random_tf_policy
from tf_agents.replay_buffers import tf_uniform_replay_buffer
from tf_agents.utils import common
tf.compat.v1.enable_v2_behavior()
# Set up a virtual display for rendering OpenAI gym environments.
display = pyvirtualdisplay.Display(visible=0, size=(1400, 900)).start()
# %% [markdown]
# ### 設定
# %%
env_name = 'CartPole-v0' # @param
num_iterations = 4000 # @param
initial_collect_steps = 1000 # @param
collect_steps_per_iteration = 10 # @param
replay_buffer_capacity = 100000 # @param
fc_layer_params = (100, 100) # @param
batch_size = 64 # @param
learning_rate = 1e-3 # @param
log_interval = 200 # @param
num_eval_episodes = 10 # @param
eval_interval = 1000 # @param
# %% [markdown]
# ## Cartpole カートポール 倒立振子
#
# [Cartpole](https://gym.openai.com/docs/#environments) は強化学習の Hello world 的問題です。
#
# 倒立振子が倒れないように、左か右へ移動(Action)できる。報酬の最大値は 200。
# %%
env = suite_gym.load(env_name)
env.reset()
PIL.Image.fromarray(env.render())
# %%
print('Observation Spec:')
print(env.time_step_spec().observation)
print('Action Spec:')
print(env.action_spec())
# %% [markdown]
# ### Random 行動
#
# 大体 30 位の rewards (報酬)を得られる
# %% [markdown]
# #### TF suite_gym利用
# %%
env = suite_gym.load(env_name)
rewards = []
for i_episode in range(10):
env.reset()
for t in range(200):
action = env.action_space.sample()
next_step = env.step(action)
assert 1 == next_step.reward
if next_step.is_last():
rewards.append(t + 1)
break
# %%
np.mean(rewards), rewards
# %% [markdown]
# #### OpenAI gym利用
# %%
env = gym.make(env_name)
rewards = []
for i_episode in range(10):
env.reset()
for t in range(100):
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
assert 1 == reward
if done:
rewards.append(t + 1)
break
# %%
np.mean(rewards), rewards
# %%
observation, reward, done, info
# %% [markdown]
# ### TF-AgentsでDQN強化学習
# %% [markdown]
# #### 訓練と評価用の Environment を用意
# %%
train_py_env = suite_gym.load(env_name)
eval_py_env = suite_gym.load(env_name)
train_env = tf_py_environment.TFPyEnvironment(train_py_env)
eval_env = tf_py_environment.TFPyEnvironment(eval_py_env)
# %% [markdown]
# #### Agent
# %%
def create_agent(train_env):
q_net = q_network.QNetwork(
train_env.observation_spec(),
train_env.action_spec(),
fc_layer_params=fc_layer_params,
)
adam = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate)
train_step_counter = tf.compat.v2.Variable(0)
tf_agent = dqn_agent.DqnAgent(
train_env.time_step_spec(),
train_env.action_spec(),
q_network=q_net,
optimizer=adam,
td_errors_loss_fn=dqn_agent.element_wise_squared_loss,
train_step_counter=train_step_counter,
)
tf_agent.initialize()
return tf_agent
# %% [markdown]
# #### Policies & Metrics
#
#
# [tf_metrics.AverageReturnMetric](https://github.com/tensorflow/agents/blob/dba93a7b2ebdd1d64e7a46822804b57ad11878d0/tf_agents/metrics/py_metrics.py#L110) で評価し、[TF Driver](https://github.com/tensorflow/agents/blob/master/tf_agents/colabs/4_drivers_tutorial.ipynb) で for loop を使わずに簡潔に記述。
# %%
random_policy = random_tf_policy.RandomTFPolicy(
train_env.time_step_spec(),
train_env.action_spec())
# 評価メソッド
def evaluate(env, policy, num_episodes=num_eval_episodes):
observers = [tf_metrics.AverageReturnMetric()]
dynamic_episode_driver.DynamicEpisodeDriver(
env, policy, observers, num_episodes).run()
return observers[0].result().numpy()
# Random policy を評価してみる
evaluate(eval_env, random_policy)
# %% [markdown]
# #### Replay Buffer & Data Collection
#
# Environment から情報を収集。[TF
# Driver](https://github.com/tensorflow/agents/blob/master/tf_agents/colabs/4_drivers_tutorial.ipynb)
# で簡潔に記述できる。
# %%
def create_replay_buffer(tf_agent, train_env):
replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer(
data_spec=tf_agent.collect_data_spec,
batch_size=train_env.batch_size,
max_length=replay_buffer_capacity
)
print(replay_buffer.capacity.numpy(), replay_buffer._batch_size)
print(replay_buffer.data_spec)
return replay_buffer
def collect_data(replay_buffer, env, policy, n_steps):
dynamic_step_driver.DynamicStepDriver(
env, policy, [replay_buffer.add_batch], n_steps
).run()
return
# %% [markdown]
# #### TF-Agentの訓練
# %%
num_iterations = 5000 # @param
def train_agent(num_iterations=num_iterations):
tf_agent = create_agent(train_env)
replay_buffer = create_replay_buffer(tf_agent, train_env)
collect_data(replay_buffer, train_env, random_policy, initial_collect_steps)
dataset = replay_buffer.as_dataset(
num_parallel_calls=3, sample_batch_size=batch_size, num_steps=2
).prefetch(3)
iterator = iter(dataset)
avg_return = evaluate(eval_env, tf_agent.policy, num_eval_episodes)
avg_returns = [avg_return]
for step in range(1, 1 + num_iterations):
collect_data(replay_buffer, train_env, tf_agent.collect_policy,
collect_steps_per_iteration)
experience, _ = next(iterator)
train_loss = tf_agent.train(experience)
if step % log_interval == 0:
print('Step = {}, Loss= {}'.format(step, train_loss.loss))
if step % eval_interval == 0:
avg_return = evaluate(eval_env, tf_agent.policy, num_eval_episodes)
print('Step = {}, Average Return= {}'.format(step, avg_return))
avg_returns.append(avg_return)
return avg_returns
%time avg_returns = train_agent()
# %%
avg_returns
# %%
steps = range(0, num_iterations + 1, eval_interval)
plt.ylabel('Average Return')
plt.xlabel('Step')
plt.plot(steps, avg_returns)
plt.ylim(top=210)
# %% [markdown]
# 最大の報酬 200 が得られている。