-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdqn.py
62 lines (42 loc) · 1.32 KB
/
dqn.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
import dynet as dy
import cart_environment as env
import math
MAX_NUM_EPISODES = 100000
MAX_NUM_STEPS = 500
GAMMA = 0.999
model = env.CartModel()
episode_num = 0
total_step_num = 0
def compute_loss(model, prev_state, current_state, action, reward, step_num):
q = dy.pick(model.forward(prev_state), action)
v = dy.max_dim(model.forward(current_state))
expval = v * math.pow(GAMMA, step_num) + reward
loss = q - expval
return loss
def batch_optimize(model, batch):
dy.renew_cg()
loss = [ ]
for item in batch:
loss.append(compute_loss(model, item[0], item[1], item[2], item[3], item[4]))
loss = dy.esum(loss)
loss.forward()
loss.backward()
model.trainer.update()
avg_reward = 0
batch = [ ]
while episode_num < MAX_NUM_EPISODES:
step_num = 0
environment = env.CartEnvironment()
while not (environment.has_finished() or step_num > MAX_NUM_STEPS):
action = model.select_action(environment.current_state(), total_step_num)
environment.take_action(action)
reward = environment.reward()
batch.append((environment.previous_state(), environment.current_state(), action, reward, total_step_num))
total_step_num += 1
step_num += 1
batch_optimize(model, batch)
episode_num += 1
avg_reward += step_num
if episode_num % 100 == 0:
print(avg_reward / 100)
avg_reward = 0