-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
86 lines (70 loc) · 2.83 KB
/
test2.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
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
class DQN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(DQN, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
class Agent:
def __init__(self, input_size, hidden_size, output_size, lr=0.001, gamma=0.99, epsilon=1.0, epsilon_decay=0.995, epsilon_min=0.01):
self.input_size = input_size
self.output_size = output_size
self.epsilon = epsilon
self.epsilon_decay = epsilon_decay
self.epsilon_min = epsilon_min
self.gamma = gamma
self.policy_net = DQN(input_size, hidden_size, output_size)
self.target_net = DQN(input_size, hidden_size, output_size)
self.target_net.load_state_dict(self.policy_net.state_dict())
self.target_net.eval()
self.optimizer = optim.Adam(self.policy_net.parameters(), lr=lr)
self.loss_fn = nn.MSELoss()
def select_action(self, state):
if np.random.rand() <= self.epsilon:
return np.random.choice(self.output_size)
else:
with torch.no_grad():
q_values = self.policy_net(state)
return torch.argmax(q_values).item()
def train(self, state, action, next_state, reward, done):
self.optimizer.zero_grad()
q_values = self.policy_net(state)
target_q_values = q_values.clone().detach()
if done:
target_q_values[action] = reward
else:
with torch.no_grad():
target_q_values[action] = reward + self.gamma * torch.max(self.target_net(next_state))
loss = self.loss_fn(q_values, target_q_values.unsqueeze(0))
loss.backward()
self.optimizer.step()
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
def update_target_net(self):
self.target_net.load_state_dict(self.policy_net.state_dict())
input_size = 3000000
hidden_size = 30
output_size = 30
agent = Agent(input_size, hidden_size, output_size)
# Training loop
for episode in range(num_episodes):
state = env.reset()
state = torch.tensor(state, dtype=torch.float32).unsqueeze(0)
done = False
total_reward = 0
while not done:
action = agent.select_action(state)
next_state, reward, done, _ = env.step(action)
next_state = torch.tensor(next_state, dtype=torch.float32).unsqueeze(0)
reward = torch.tensor(reward, dtype=torch.float32)
agent.train(state, action, next_state, reward, done)
total_reward += reward.item()
state = next_state
if episode % target_update_frequency == 0:
agent.update_target_net()