/
dqn.py
145 lines (108 loc) · 3.76 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
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
import math
import random
from collections import namedtuple
from itertools import count
import gym
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
env = gym.make('CartPole-v0')
state_dim = env.observation_space.shape[0]
out_dim = env.action_space.n
Transition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward'))
BATCH_SIZE = 32
GAMMA = 0.9
INITIAL_EPSILON = 0.5
FINAL_EPSILON = 0.01
CAPACITY = 10000
torch.manual_seed(1234)
use_cuda = torch.cuda.is_available()
if use_cuda:
torch.cuda.manual_seed(1234)
if use_cuda:
byteTensor = torch.cuda.ByteTensor
tensor = torch.cuda.FloatTensor
longTensor = torch.cuda.LongTensor
else:
byteTensor = torch.ByteTensor
tensor = torch.Tensor
longTensor = torch.LongTensor
class DQN(nn.Module):
def __init__(self, state_dim, out_dim, capacity, bsz, epsilon):
super().__init__()
self.steps_done = 0
self.position = 0
self.pool = []
self.capacity = capacity
self.bsz = bsz
self.epsilon = epsilon
self.fc1 = nn.Linear(state_dim, 32)
self.fc2 = nn.Linear(32, out_dim)
self.fc1.weight.data.uniform_(-.1, .1)
self.fc2.weight.data.uniform_(-.1, .1)
def forward(self, x):
x = F.relu(self.fc1(x))
return self.fc2(x)
def action(self, state):
self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000
if random.random() > self.epsilon:
return self(Variable(state, volatile=True)).data.max(1)[1].view(1, 1)
else:
return longTensor([[random.randrange(2)]])
def push(self, *args):
if len(self) < self.capacity:
self.pool.append(None)
self.pool[self.position] = Transition(*args)
self.position = (self.position + 1) % self.capacity
def sample(self):
return random.sample(self.pool, self.bsz)
def __len__(self):
return len(self.pool)
dqn = DQN(state_dim, out_dim, CAPACITY, BATCH_SIZE, INITIAL_EPSILON)
if use_cuda:
dqn = dqn.cuda()
optimizer = optim.Adam(dqn.parameters(), lr=0.0001)
def optimize_model():
if len(dqn) < BATCH_SIZE:
return
transitions = dqn.sample()
batch = Transition(*zip(*transitions))
non_final_mask = byteTensor(
tuple(map(lambda x: x is not None, batch.next_state)))
non_final_next_states = Variable(
torch.cat([s for s in batch.next_state if s is not None]), volatile=True)
next_state_values = Variable(torch.zeros(BATCH_SIZE).type(tensor))
next_state_values[non_final_mask] = dqn(non_final_next_states).max(1)[0]
next_state_values.volatile = False
state_batch = Variable(torch.cat(batch.state))
action_batch = Variable(torch.cat(batch.action))
reward_batch = Variable(torch.cat(batch.reward))
state_action_values = dqn(state_batch).gather(1, action_batch)
expected_state_action_values = (next_state_values * GAMMA) + reward_batch
loss = F.smooth_l1_loss(state_action_values, expected_state_action_values)
optimizer.zero_grad()
loss.backward()
optimizer.step()
perfect = 0
for _ in range(10000):
state = env.reset()
state = torch.from_numpy(state).type(tensor).view(1, -1)
for t in count():
action = dqn.action(state)
next_state, reward, done, _ = env.step(action[0, 0])
next_state = torch.from_numpy(
next_state).type(tensor).view(1, -1)
if done:
next_state = None
reward = tensor([reward])
dqn.push(state, action, next_state, reward)
state = next_state
optimize_model()
if done:
if t > perfect:
print(t)
perfect = t
break