-
Notifications
You must be signed in to change notification settings - Fork 1
/
DPG.py
147 lines (108 loc) · 4.1 KB
/
DPG.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
# -*- coding: utf-8 -*-
import os
import numpy as np
from keras.layers import Input, Dense
from keras.models import Model
from keras.optimizers import Adam
import keras.backend as K
from DRL import DRL
class DPG(DRL):
"""Deterministic Policy Gradient Algorithms
"""
def __init__(self):
super(DPG, self).__init__()
self.model = self._build_model()
if os.path.exists('model/dpg.h5'):
self.model.load_weights('model/dpg.h5')
self.gamma = 0.95
def _build_model(self):
"""basic model.
"""
inputs = Input(shape=(4,), name='ob_input')
x = Dense(16, activation='relu')(inputs)
x = Dense(16, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=x)
return model
def loss(self, y_true, y_pred):
"""loss function.
Arguments:
y_true: (action, reward)
y_pred: action_prob
Returns:
loss: reward loss
"""
action_pred = y_pred
action_true, discount_episode_reward = y_true[:, 0], y_true[:, 1]
action_true = K.reshape(action_true, (-1, 1))
loss = K.binary_crossentropy(action_true, action_pred)
loss = loss * K.flatten(discount_episode_reward)
return loss
def discount_reward(self, rewards):
"""Discount reward
Arguments:
rewards: rewards in a episode.
"""
# compute the discounted reward backwards through time.
discount_rewards = np.zeros_like(rewards, dtype=np.float32)
cumulative = 0.
for i in reversed(range(len(rewards))):
cumulative = cumulative * self.gamma + rewards[i]
discount_rewards[i] = cumulative
# size the rewards to be unit normal (helps control the gradient estimator variance).
discount_rewards -= np.mean(discount_rewards)
discount_rewards //= np.std(discount_rewards)
return list(discount_rewards)
def train(self, episode, batch):
"""training model.
Arguments:
episode: ganme episode
batch: batch size of episode
Returns:
history: training history
"""
self.model.compile(loss=self.loss, optimizer=Adam(lr=0.01))
history = {'episode': [], 'Episode_reward': [], 'Loss': []}
episode_reward = 0
states = []
actions = []
rewards = []
discount_rewards = []
for i in range(episode):
observation = self.env.reset()
erewards = []
while True:
x = observation.reshape(-1, 4)
prob = self.model.predict(x)[0][0]
# choice action with prob.
action = np.random.choice(np.array(range(2)), size=1, p=[1 - prob, prob])[0]
observation, reward, done, _ = self.env.step(action)
states.append(x[0])
actions.append(action)
erewards.append(reward)
rewards.append(reward)
if done:
# calculate discount rewards every episode.
discount_rewards.extend(self.discount_reward(erewards))
break
if i != 0 and i % batch == 0:
episode_reward = sum(rewards) / batch
X = np.array(states)
y = np.array(list(zip(actions, discount_rewards)))
loss = self.model.train_on_batch(X, y)
history['episode'].append(i)
history['Episode_reward'].append(episode_reward)
history['Loss'].append(loss)
print('Episode: {} | Episode reward: {} | loss: {:.3f}'.format(i, episode_reward, loss))
episode_reward = 0
states = []
actions = []
rewards = []
discount_rewards = []
self.model.save_weights('model/dpg.h5')
return history
if __name__ == '__main__':
model = DPG()
history = model.train(5000, 5)
model.save_history(history, 'dpg.csv')
model.play()