強化学習 x ニューラルネットワーク 7 (Policy Gradient)

パラメータを持った関数で戦略を実装します。攻略する環境はCartPoleです。

まずは親クラスとなるフレームワークを作成します。(前回のCatcherと同じです。)

fn_framework.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
import io
import re
from collections import namedtuple
from collections import deque
import numpy as np
import tensorflow as tf
from tensorflow.python import keras as K
from PIL import Image
import matplotlib.pyplot as plt

# s:状態
# a:行動
# r:報酬
# n_s:遷移先の状態
# d:エピソード終了フラグ
Experience = namedtuple("Experience",
["s", "a", "r", "n_s", "d"])

# ニューラルネットワークを使い状態から評価を行う。
class FNAgent():

def __init__(self, epsilon, actions):
self.epsilon = epsilon
self.actions = actions
self.model = None
self.estimate_probs = False
self.initialized = False

# 学習したエージェントを保存
def save(self, model_path):
self.model.save(model_path, overwrite=True, include_optimizer=False)

# 学習したエージェントを読み込み
@classmethod
def load(cls, env, model_path, epsilon=0.0001):
actions = list(range(env.action_space.n))
agent = cls(epsilon, actions)
agent.model = K.models.load_model(model_path)
agent.initialized = True
return agent

# 初期化
# experiences:エージェントの経験
def initialize(self, experiences):
raise Exception("You have to implements estimate method.")

# 関数による予測
def estimate(self, s):
raise Exception("You have to implements estimate method.")

# パラメータの更新
def update(self, experiences, gamma):
raise Exception("You have to implements update method.")

def policy(self, s):
if np.random.random() < self.epsilon or not self.initialized:
return np.random.randint(len(self.actions))
else:
estimates = self.estimate(s)
if self.estimate_probs:
action = np.random.choice(self.actions,
size=1, p=estimates)[0]
return action
else:
return np.argmax(estimates)

def play(self, env, episode_count=5, render=True):
for e in range(episode_count):
s = env.reset()
done = False
episode_reward = 0
while not done:
if render:
env.render()
a = self.policy(s)
n_state, reward, done, info = env.step(a)
episode_reward += reward
s = n_state
else:
print("Get reward {}.".format(episode_reward))

# エージェントの学習を行う
class Trainer():

def __init__(self, buffer_size=1024, batch_size=32,
gamma=0.9, report_interval=10, log_dir=""):
self.buffer_size = buffer_size
self.batch_size = batch_size
self.gamma = gamma
self.report_interval = report_interval
self.logger = Logger(log_dir, self.trainer_name)
# エージェントの行動履歴(古い行動からすてる)
self.experiences = deque(maxlen=buffer_size)
self.training = False
self.training_count = 0
self.reward_log = []

@property
def trainer_name(self):
class_name = self.__class__.__name__
snaked = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", class_name)
snaked = re.sub("([a-z0-9])([A-Z])", r"\1_\2", snaked).lower()
snaked = snaked.replace("_trainer", "")
return snaked

def train_loop(self, env, agent, episode=200, initial_count=-1,
render=False, observe_interval=0):
self.experiences = deque(maxlen=self.buffer_size)
self.training = False
self.training_count = 0
self.reward_log = []
frames = []

for i in range(episode):
s = env.reset()
done = False
step_count = 0
self.episode_begin(i, agent)
while not done:
if render:
env.render()
if self.training and observe_interval > 0 and\
(self.training_count == 1 or
self.training_count % observe_interval == 0):
frames.append(s)

a = agent.policy(s)
n_state, reward, done, info = env.step(a)
e = Experience(s, a, reward, n_state, done)
self.experiences.append(e)
if not self.training and \
len(self.experiences) == self.buffer_size:
self.begin_train(i, agent)
self.training = True

self.step(i, step_count, agent, e)

s = n_state
step_count += 1
else:
self.episode_end(i, step_count, agent)

if not self.training and \
initial_count > 0 and i >= initial_count:
self.begin_train(i, agent)
self.training = True

if self.training:
if len(frames) > 0:
self.logger.write_image(self.training_count,
frames)
frames = []
self.training_count += 1

def episode_begin(self, episode, agent):
pass

def begin_train(self, episode, agent):
pass

def step(self, episode, step_count, agent, experience):
pass

def episode_end(self, episode, step_count, agent):
pass

def is_event(self, count, interval):
return True if count != 0 and count % interval == 0 else False

def get_recent(self, count):
recent = range(len(self.experiences) - count, len(self.experiences))
return [self.experiences[i] for i in recent]

# 環境から取得される「状態」の前処理を行う
class Observer():

def __init__(self, env):
self._env = env

@property
def action_space(self):
return self._env.action_space

@property
def observation_space(self):
return self._env.observation_space

def reset(self):
return self.transform(self._env.reset())

def render(self):
self._env.render()

def step(self, action):
n_state, reward, done, info = self._env.step(action)
return self.transform(n_state), reward, done, info

def transform(self, state):
raise Exception("You have to implements transform method.")

# 学習経過の記録を行う
class Logger():

def __init__(self, log_dir="", dir_name=""):
self.log_dir = log_dir
if not log_dir:
self.log_dir = os.path.join(os.path.dirname(__file__), "logs")
if not os.path.exists(self.log_dir):
os.mkdir(self.log_dir)

if dir_name:
self.log_dir = os.path.join(self.log_dir, dir_name)
if not os.path.exists(self.log_dir):
os.mkdir(self.log_dir)

self._callback = K.callbacks.TensorBoard(self.log_dir)

@property
def writer(self):
return self._callback.writer

def set_model(self, model):
self._callback.set_model(model)

def path_of(self, file_name):
return os.path.join(self.log_dir, file_name)

def describe(self, name, values, episode=-1, step=-1):
mean = np.round(np.mean(values), 3)
std = np.round(np.std(values), 3)
desc = "{} is {} (+/-{})".format(name, mean, std)
if episode > 0:
print("At episode {}, {}".format(episode, desc))
elif step > 0:
print("At step {}, {}".format(step, desc))

def plot(self, name, values, interval=10):
indices = list(range(0, len(values), interval))
means = []
stds = []
for i in indices:
_values = values[i:(i + interval)]
means.append(np.mean(_values))
stds.append(np.std(_values))
means = np.array(means)
stds = np.array(stds)
plt.figure()
plt.title("{} History".format(name))
plt.grid()
plt.fill_between(indices, means - stds, means + stds,
alpha=0.1, color="g")
plt.plot(indices, means, "o-", color="g",
label="{} per {} episode".format(name.lower(), interval))
plt.legend(loc="best")
plt.show()

def write(self, index, name, value):
summary = tf.Summary()
summary_value = summary.value.add()
summary_value.tag = name
summary_value.simple_value = value
self.writer.add_summary(summary, index)
self.writer.flush()

def write_image(self, index, frames):
# Deal with a 'frames' as a list of sequential gray scaled image.
last_frames = [f[:, :, -1] for f in frames]
if np.min(last_frames[-1]) < 0:
scale = 127 / np.abs(last_frames[-1]).max()
offset = 128
else:
scale = 255 / np.max(last_frames[-1])
offset = 0
channel = 1 # gray scale
tag = "frames_at_training_{}".format(index)
values = []

for f in last_frames:
height, width = f.shape
array = np.asarray(f * scale + offset, dtype=np.uint8)
image = Image.fromarray(array)
output = io.BytesIO()
image.save(output, format="PNG")
image_string = output.getvalue()
output.close()
image = tf.Summary.Image(
height=height, width=width, colorspace=channel,
encoded_image_string=image_string)
value = tf.Summary.Value(tag=tag, image=image)
values.append(value)

summary = tf.Summary(value=values)
self.writer.add_summary(summary, index)
self.writer.flush()

上記の親クラスを継承し、パラメータを持った関数として戦略を実装していきます。

policy_gradient_agent.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
import os
import argparse
import random
from collections import deque
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.externals import joblib
import tensorflow as tf
from tensorflow.python import keras as K
import gym
from fn_framework import FNAgent, Trainer, Observer, Experience

class PolicyGradientAgent(FNAgent):

def __init__(self, epsilon, actions):
super().__init__(epsilon, actions)
self.estimate_probs = True
self.scaler = None
self._updater = None

def save(self, model_path):
super().save(model_path)
joblib.dump(self.scaler, self.scaler_path(model_path))

@classmethod
def load(cls, env, model_path, epsilon=0.0001):
agent = super().load(env, model_path, epsilon)
agent.scaler = joblib.load(agent.scaler_path(model_path))
return agent

def scaler_path(self, model_path):
fname, _ = os.path.splitext(model_path)
fname += "_scaler.pkl"
return fname

def initialize(self, experiences, optimizer):
self.scaler = StandardScaler()
states = np.vstack([e.s for e in experiences])
self.scaler.fit(states)

feature_size = states.shape[1]
self.model = K.models.Sequential([
K.layers.Dense(10, activation="relu", input_shape=(feature_size,)),
K.layers.Dense(10, activation="relu"),
K.layers.Dense(len(self.actions), activation="softmax")
])
self.set_updater(optimizer)
self.initialized = True
print("Done initialization. From now, begin training!")

def set_updater(self, optimizer):
actions = tf.placeholder(shape=(None), dtype="int32")
rewards = tf.placeholder(shape=(None), dtype="float32")
one_hot_actions = tf.one_hot(actions, len(self.actions), axis=1)
action_probs = self.model.output
selected_action_probs = tf.reduce_sum(one_hot_actions * action_probs,
axis=1)
clipped = tf.clip_by_value(selected_action_probs, 1e-10, 1.0)
loss = - tf.log(clipped) * rewards
loss = tf.reduce_mean(loss)

updates = optimizer.get_updates(loss=loss,
params=self.model.trainable_weights)
self._updater = K.backend.function(
inputs=[self.model.input,
actions, rewards],
outputs=[loss],
updates=updates)

def estimate(self, s):
normalized = self.scaler.transform(s)
action_probs = self.model.predict(normalized)[0]
return action_probs

def update(self, states, actions, rewards):
normalizeds = self.scaler.transform(states)
actions = np.array(actions)
rewards = np.array(rewards)
self._updater([normalizeds, actions, rewards])

class CartPoleObserver(Observer):

def transform(self, state):
return np.array(state).reshape((1, -1))

class PolicyGradientTrainer(Trainer):

def __init__(self, buffer_size=1024, batch_size=32,
gamma=0.9, report_interval=10, log_dir=""):
super().__init__(buffer_size, batch_size, gamma,
report_interval, log_dir)
self._reward_scaler = None
self.d_experiences = deque(maxlen=buffer_size)

def train(self, env, episode_count=220, epsilon=0.1, initial_count=-1,
render=False):
actions = list(range(env.action_space.n))
agent = PolicyGradientAgent(epsilon, actions)

self.train_loop(env, agent, episode_count, initial_count, render)
return agent

def episode_begin(self, episode, agent):
self.experiences = []

def step(self, episode, step_count, agent, experience):
if agent.initialized:
agent.update(*self.make_batch())

def make_batch(self):
batch = random.sample(self.d_experiences, self.batch_size)
states = np.vstack([e.s for e in batch])
actions = [e.a for e in batch]
rewards = [e.r for e in batch]
rewards = np.array(rewards).reshape((-1, 1))
rewards = self._reward_scaler.transform(rewards).flatten()
return states, actions, rewards

def begin_train(self, episode, agent):
optimizer = K.optimizers.Adam(clipnorm=1.0)
agent.initialize(self.d_experiences, optimizer)
self._reward_scaler = StandardScaler(with_mean=False)
rewards = np.array([[e.r] for e in self.d_experiences])
self._reward_scaler.fit(rewards)

def episode_end(self, episode, step_count, agent):
rewards = [e.r for e in self.experiences]
self.reward_log.append(sum(rewards))

discounteds = []
for t, r in enumerate(rewards):
d_r = [_r * (self.gamma ** i) for i, _r in
enumerate(rewards[t:])]
d_r = sum(d_r)
discounteds.append(d_r)

for i, e in enumerate(self.experiences):
s, a, r, n_s, d = e
d_r = discounteds[i]
d_e = Experience(s, a, d_r, n_s, d)
self.d_experiences.append(d_e)

if not self.training and len(self.d_experiences) == self.buffer_size:
self.begin_train(i, agent)
self.training = True

if self.is_event(episode, self.report_interval):
recent_rewards = self.reward_log[-self.report_interval:]
self.logger.describe("reward", recent_rewards, episode=episode)

def main(play):
env = CartPoleObserver(gym.make("CartPole-v0"))
trainer = PolicyGradientTrainer()
path = trainer.logger.path_of("policy_gradient_agent.h5")

if play:
agent = PolicyGradientAgent.load(env, path)
agent.play(env)
else:
trained = trainer.train(env, episode_count=250)
trainer.logger.plot("Rewards", trainer.reward_log,
trainer.report_interval)
trained.save(path)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PG Agent")
parser.add_argument("--play", action="store_true", help="play with trained model")

args = parser.parse_args()
main(args.play)

結果は次の通りです。
結果(コンソール)

結果(グラフ)
前回実装した価値関数の場合より、報酬が増えるようになるには時間がかかるようです。

参考

Pythonで学ぶ強化学習 -入門から実践まで- サンプルコード