-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotter for dqn cartpool.py
196 lines (164 loc) · 6.06 KB
/
plotter for dqn cartpool.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
''' Code inspired from https://github.com/gsurma/cartpole, ported to pytorch.'''
import matplotlib.pyplot as plt
import random
import gym
import numpy as np
from collections import deque
import torch.nn as nn
import torch
import time
import math
ENV_NAME = "CartPole-v1"
GAMMA = 0.99
LEARNING_RATE = 0.001
MEMORY_SIZE = 1000000
BATCH_SIZE = 20
EXPLORATION_MAX = 1.0
EXPLORATION_MIN = 0.01
EXPLORATION_DECAY = 0.995
PATH = 'model.pth'
TRAIN_EPS = 90
class Netx(nn.Module):
def __init__(self, observation_space, action_space):
super().__init__()
hiddenlayers = 64
self.fc1 = nn.Linear(observation_space, hiddenlayers)
# self.bn1 = nn.BatchNorm1d(hiddenlayers)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Linear(hiddenlayers, hiddenlayers)
# self.bn2 = nn.BatchNorm1d(hiddenlayers)
self.fc3 = nn.Linear(hiddenlayers, action_space)
# self.bn3 = nn.BatchNorm1d(action_space)
def forward(self, x):
out = self.fc1(x)
# out = self.bn1(out)
out = self.relu(out)
out = self.fc2(out)
# out = self.bn2(out)
out = self.relu(out)
out = self.fc3(out)
# out = self.bn3(out)
out = self.relu(out)
return out
class DQNSolver:
def __init__(self, observation_space, action_space):
self.exploration_rate = EXPLORATION_MAX
self.action_space = action_space
self.memory = deque(maxlen=MEMORY_SIZE)
## Model and the optimizer
self.model = Netx(observation_space, action_space)
self.model.double()
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=LEARNING_RATE)
self.criterion = nn.MSELoss()
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def act(self, state):
if np.random.rand() < self.exploration_rate:
return random.randrange(self.action_space)
with torch.no_grad():
q_values = self.model(torch.tensor(state))
return torch.argmax(q_values[0]).item()
def save_model(self):
torch.save(self.model, PATH)
def experience_replay(self):
if len(self.memory) < BATCH_SIZE:
return
batch = random.sample(self.memory, BATCH_SIZE)
losses = []
for state, action, reward, state_next, terminal in batch:
q_update = reward
if not terminal:
with torch.no_grad():
preds = self.model(torch.tensor(state_next))
q_update = (reward + GAMMA * torch.max(preds[0]).item())
q_values_output = self.model(torch.tensor(state))
q_values_target = q_values_output.clone().detach().requires_grad_(False)
q_values_target[0][action] = q_update
loss = self.criterion(q_values_output, q_values_target)
losses.append(loss.item())
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.exploration_rate *= EXPLORATION_DECAY**2
self.exploration_rate = max(EXPLORATION_MIN, self.exploration_rate)
losses = np.mean(np.array(losses))
return losses
def inference():
model = torch.load(PATH)
env = gym.make(ENV_NAME)
observation_space = env.observation_space.shape[0]
action_space = env.action_space.n
num_epsoid = 0
while num_epsoid <1000000:
num_epsoid += 1
state = env.reset()
state = np.reshape(state, [1, observation_space])
step = 0
while True:
step += 1
time.sleep(0.01)
env.render()
with torch.no_grad():
q_values = model(torch.tensor(state))
action = torch.argmax(q_values[0]).item()
#action = DQNSolver(observation_space, action_space).act(state)
state_next, reward, terminal, info = env.step(action)
reward = reward if not terminal else -reward
state_next = np.reshape(state_next, [1, observation_space])
state = state_next
if terminal:
print ("Epsoid: " + str(num_epsoid) + "score: " + str(step))
break
def train():
env = gym.make(ENV_NAME)
observation_space = env.observation_space.shape[0]
action_space = env.action_space.n
dqn_solver = DQNSolver(observation_space, action_space)
num_epsoid = 0
l = []
start = time.time()
x_axis = []
losses = []
while num_epsoid < TRAIN_EPS:
num_epsoid += 1
state = env.reset()
state = np.reshape(state, [1, observation_space])
step = 0
lossac = []
while True:
step += 1
env.render()
action = dqn_solver.act(state)
state_next, reward, terminal, info = env.step(action)
reward = reward if not terminal else -reward
state_next = np.reshape(state_next, [1, observation_space])
dqn_solver.remember(state, action, reward, state_next, terminal)
state = state_next
if terminal:
print ("Epsoid: " + str(num_epsoid) + ", exploration: " + str(dqn_solver.exploration_rate) + ", score: " + str(step))
if step >=150:
l.append(step)
break
loss = dqn_solver.experience_replay()
if loss is None:
pass
else:
lossac.append(loss)
if lossac != []:
lossac = np.mean(np.array(lossac))
losses.append(lossac)
x_axis.append(num_epsoid)
end = time.time()
print ("time taken to train: ",end-start)
dqn_solver.save_model()
return losses, x_axis
if __name__ == "__main__":
losses, x_axis = train()
plt.plot(x_axis, np.log(np.array(losses)))
plt.title("Losses vs Episode #, lr = " + str(LEARNING_RATE))
plt.xlabel("Episode #")
plt.ylabel("Log(Losses)")
plt.show()
print ("---------------------inferencing now---------------------------")
env.close()
# inference()