-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDPG.py
78 lines (58 loc) · 3.11 KB
/
DDPG.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
import torch
import torch.nn as nn
from torch.autograd import Variable
from models import *
from utils import *
from ExperienceReplayMemory import SequentialDequeMemory
class DDPG:
def __init__(self, env, hidden_size=256, actor_lr = 1e-4, critic_lr=1e-3, batch_size=64, gamma=0.99,
tau=1e-2, max_memory_size=50000):
self.num_states = env.observation_space.shape[0]
self.num_actions = env.action_space.shape[0]
self.gamma = gamma
self.tau = tau
self.max_memory_size = max_memory_size
self.batch_size = batch_size
# Networks
self.actor = Actor(self.num_states, hidden_size, self.num_actions)
self.actor_target = Actor(self.num_states, hidden_size, self.num_actions)
self.critic = Critic(self.num_states + self.num_actions, hidden_size, self.num_actions)
self.critic_target = Critic(self.num_states + self.num_actions, hidden_size, self.num_actions)
# Copy weights
for target_param, param in zip(self.actor_target.parameters(), self.actor.parameters()):
target_param.data.copy_(param)
for target_param, param in zip(self.critic_target.parameters(), self.critic.parameters()):
target_param.data.copy_(param)
# Training
self.replay_memory = SequentialDequeMemory(queue_capacity=self.max_memory_size)
self.actor_optimizer = torch.optim.Adam(self.actor.parameters(), lr=actor_lr)
self.critic_optimizer = torch.optim.Adam(self.critic.parameters(), lr=critic_lr)
self.critic_criterion = nn.MSELoss()
def get_action(self, state):
state = Variable(torch.from_numpy(state).float().unsqueeze(0))
action = self.actor(state)
action = action.detach().numpy()[0, 0]
return action
def update(self):
states, actions, rewards, next_states, done = self.replay_memory.get_random_batch_for_replay(self.batch_size)
states, actions = torch.FloatTensor(states), torch.FloatTensor(actions)
rewards, next_states = torch.FloatTensor(rewards), torch.FloatTensor(next_states)
if actions.dim() < 2:
actions = actions.unsqueeze(1)
Qvals = self.critic(states, actions)
next_actions = self.actor_target(next_states)
next_Q = self.critic_target(next_states, next_actions.detach())
Q_prime = rewards.unsqueeze(1) + self.gamma*next_Q
critic_loss = self.critic_criterion(Qvals, Q_prime)
actor_loss = -1*self.critic(states, self.actor(states)).mean()
# updates networks
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
for target_param, param in zip(self.actor_target.parameters(), self.actor.parameters()):
target_param.data.copy_(param.data*self.tau + target_param.data*(1.0 - self.tau))
for target_param, param in zip(self.critic_target.parameters(), self.critic.parameters()):
target_param.data.copy_(param.data*self.tau + target_param.data*(1.0 - self.tau))