-
Notifications
You must be signed in to change notification settings - Fork 1
/
DQN_Plotting.py
183 lines (154 loc) · 5.82 KB
/
DQN_Plotting.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
''' Code inspired from https://github.com/gsurma/cartpole, ported to pytorch.'''
import random
import gym
import numpy as np
from collections import deque
import torch.nn as nn
import torch
import time
import matplotlib.pyplot as plt
ENV_NAME = "CartPole-v1"
GAMMA = 0.99
LEARNING_RATE = 0.0005
MEMORY_SIZE = 1000000
BATCH_SIZE = 20
EXPLORATION_MAX = 1.0
EXPLORATION_MIN = 0.01
EXPLORATION_DECAY = 0.95
TRAIN_EPISODES = 100
TEST_EPISODES = 50
PATH = 'model.pth'
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)
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)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.exploration_rate *= EXPLORATION_DECAY**2
self.exploration_rate = max(EXPLORATION_MIN, self.exploration_rate)
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
all_scores=[]
while num_epsoid <TEST_EPISODES:
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 ("Episode: " + str(num_epsoid) + " score: " + str(step))
all_scores.append(step)
break
print('Average score over ' + TEST_EPISODES+ ' episodes = '+ np.mean(all_scores))
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 = []
all_scores = []
start = time.time()
while num_epsoid < TRAIN_EPISODES:
if len(l)>8:
break
num_epsoid += 1
state = env.reset()
state = np.reshape(state, [1, observation_space])
step = 0
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 ("Episode: " + str(num_epsoid) + ", exploration: " + str(dqn_solver.exploration_rate) + ", score: " + str(step))
all_scores.append(step)
if step >=150:
l.append(step)
break
dqn_solver.experience_replay()
end = time.time()
plt.plot(range(len(all_scores)),all_scores)
plt.xlabel('No of iterations')
plt.ylabel('Reward Obtained')
plt.show()
print ("time taken to train: ",end-start)
dqn_solver.save_model()
return all_scores
if __name__ == "__main__":
# scores = train()
print ("---------------------inferencing now---------------------------")
inference()