-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn.py
131 lines (92 loc) · 3.79 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
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from environments.bit_switch import BitSwitchEnv
from modules.experience_buffer import *
class DQN(nn.Module):
def __init__(self, n_states: int, n_actions: int, n_hidden: int, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.qnet = nn.Sequential(
nn.Linear(n_states, n_hidden),
nn.ReLU(),
nn.Linear(n_hidden, n_hidden),
nn.ReLU(),
nn.Linear(n_hidden, n_actions),
)
def forward(self, x):
return self.qnet(x)
class DQNAgent:
def __init__(self, state_dim: int, action_dim: int, sample_size = 128, epsilon = 0.01, lr = 0.01, gamma = 0.99) -> None:
# hyperparameters and additional info
self.epsilon = epsilon
self.sample_size = sample_size
self.gamma = gamma
self.state_dim = state_dim
self.action_dim = action_dim
# create the networks and initialize with identical weights
self.policy_net = DQN(state_dim, action_dim, 128)
self.target_net = DQN(state_dim, action_dim, 128)
self.target_net.load_state_dict(self.policy_net.state_dict())
# learning modules
self.exp_buffer = ExperienceBuffer(int(1e4))
self.optimizer = optim.Adam(self.policy_net.parameters(), lr=lr)
def pick_action(self, state):
# TODO: implement epsilon decay for extra early exploration
if random.random() < self.epsilon:
return random.randrange(self.action_dim)
else:
with torch.no_grad():
q_values = self.policy_net(state)
return q_values.argmax().item()
def store(self, transition: Transition):
self.exp_buffer.add(transition)
def learning_step(self):
if len(self.exp_buffer) < self.sample_size:
return
transitions = self.exp_buffer.sample(self.sample_size)
batch = Transition(*zip(*transitions))
states = torch.cat(batch.state)
actions = torch.cat(batch.action)
rewards = torch.cat(batch.reward)
q_values = self.policy_net(states).gather(1, actions)
def compute_loss(self):
self.optimizer.zero_grad()
batch = self.exp_buffer.sample(self.sample_size)
states, actions, rewards, next_states, dones = batch
states = torch.FloatTensor(states)
actions = torch.LongTensor(actions)
rewards = torch.FloatTensor(rewards)
next_states = torch.FloatTensor(next_states)
dones = torch.FloatTensor(dones)
pre_q_values = self.policy_net(states)
q_values = pre_q_values.gather(1, actions)
next_q_values = self.target_net(next_states).max(1)[0].detach()
targets = rewards + (1 - dones) * self.gamma * next_q_values
loss = nn.MSELoss()(q_values.squeeze(), targets)
return loss
if __name__ == '__main__':
model = DQNAgent(4, 4, 100)
env = BitSwitchEnv(4)
goal = env.reset()
state = env.state
for epoch in range(1000):
while not model.exp_buffer.is_full():
action = model.pick_action(torch.from_numpy(state))
prev_state = np.array(state, copy=True)
state, reward, done = env.step(action)
model.exp_buffer.add(prev_state, [action], reward, state, done)
if env.reached:
goal = env.reset()
state = env.state
# done with the epoch
loss = model.compute_loss()
loss.backward()
model.optimizer.step()
goal = env.reset()
state = env.state
model.exp_buffer.reset()
if (epoch + 1) % 20 == 0:
print(f'Epoch {epoch + 1}, Loss: {loss.item():.4f}')
if (epoch + 1) == 800:
print("hi")