-
Notifications
You must be signed in to change notification settings - Fork 0
/
td3_torch.py
141 lines (109 loc) · 5.44 KB
/
td3_torch.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
import torch as T
import torch.nn.functional as F
import numpy as np
from buffer import ReplayBuffer
from networks import Actor, Critic
device = T.device("cuda" if T.cuda.is_available() else "cpu")
class Agent():
def __init__(self, alpha, beta, input_dims, tau, a_max,
gamma=0.99, update_actor_interval=2,
n_actions=2, max_size=1000000, batch_size=100):
self.gamma = gamma
self.tau = tau
self.max_action = a_max
self.min_action = a_max
self.memory = ReplayBuffer(max_size, input_dims, n_actions)
self.batch_size = batch_size
self.learn_step_cntr = 0
self.n_actions = n_actions
self.update_actor_iter = update_actor_interval
self.actor = Actor(s_dim=input_dims, a_dim=n_actions,a_max=a_max)
self.critic_1 = Critic(s_dim=input_dims,a_dim=n_actions)
self.critic_2 = Critic(s_dim=input_dims,a_dim=n_actions)
self.target_actor = Actor(s_dim=input_dims, a_dim=n_actions,a_max=a_max)
self.target_critic_1 = Critic(s_dim=input_dims, a_dim=n_actions)
self.target_critic_2 = Critic(s_dim=input_dims, a_dim=n_actions)
self.actor.optimizer = T.optim.Adam(self.actor.parameters(), lr=alpha)
self.critic_1.optimizer = T.optim.Adam(self.critic_1.parameters(),
lr=beta)
self.critic_2.optimizer = T.optim.Adam(self.critic_2.parameters(),
lr=beta)
self.target_actor.optimizer = T.optim.Adam(self.target_actor.parameters(),
lr=alpha)
self.target_critic_1.optimizer = T.optim.Adam(self.target_critic_1.parameters(),
lr=beta)
self.target_critic_2.optimizer = T.optim.Adam(self.target_critic_2.parameters(),
lr=beta)
self.update_network_parameters(tau=1)
def choose_action(self, s):
s = T.FloatTensor(s.reshape(1, -1)).to(device)
return self.actor(s).cpu().data.numpy().flatten()
def remember(self, state, action, reward, new_state, done):
self.memory.store_transition(state, action, reward, new_state, done)
def learn(self):
if self.memory.mem_cntr < self.batch_size:
return
state, action, reward, new_state, done = \
self.memory.sample_buffer(self.batch_size)
reward = T.tensor(reward, dtype=T.float).to(self.critic_1.device)
done = T.tensor(done,dtype=T.int).to(self.critic_1.device)
state_ = T.tensor(new_state, dtype=T.float).to(self.critic_1.device)
state = T.tensor(state, dtype=T.float).to(self.critic_1.device)
action = T.tensor(action, dtype=T.float).to(self.critic_1.device)
target_actions = self.target_actor.forward(state_)
q1_ = self.target_critic_1.forward(state_, target_actions)
q2_ = self.target_critic_2.forward(state_, target_actions)
q1 = self.critic_1.forward(state, action)
q2 = self.critic_2.forward(state, action)
#print(done)
q1_[done] = 0.0
q2_[done] = 0.0
q1_ = q1_.view(-1)
q2_ = q2_.view(-1)
critic_value_ = T.min(q1_, q2_)
target = reward + self.gamma*critic_value_
target = target.view(self.batch_size, 1)
self.critic_1.optimizer.zero_grad()
self.critic_2.optimizer.zero_grad()
q1_loss = F.mse_loss(target, q1)
q2_loss = F.mse_loss(target, q2)
critic_loss = q1_loss + q2_loss
critic_loss.backward()
self.critic_1.optimizer.step()
self.critic_2.optimizer.step()
self.learn_step_cntr += 1
if self.learn_step_cntr % self.update_actor_iter != 0:
return
self.actor.optimizer.zero_grad()
actor_q1_loss = self.critic_1.forward(state, self.actor.forward(state))
actor_loss = -T.mean(actor_q1_loss)
actor_loss.backward()
self.actor.optimizer.step()
self.update_network_parameters()
def update_network_parameters(self, tau=None):
if tau is None:
tau = self.tau
actor_params = self.actor.named_parameters()
critic_1_params = self.critic_1.named_parameters()
critic_2_params = self.critic_2.named_parameters()
target_actor_params = self.target_actor.named_parameters()
target_critic_1_params = self.target_critic_1.named_parameters()
target_critic_2_params = self.target_critic_2.named_parameters()
critic_1_state_dict = dict(critic_1_params)
critic_2_state_dict = dict(critic_2_params)
actor_state_dict = dict(actor_params)
target_actor_state_dict = dict(target_actor_params)
target_critic_1_state_dict = dict(target_critic_1_params)
target_critic_2_state_dict = dict(target_critic_2_params)
for name in critic_1_state_dict:
critic_1_state_dict[name] = tau*critic_1_state_dict[name].clone() + \
(1-tau)*target_critic_1_state_dict[name].clone()
for name in critic_2_state_dict:
critic_2_state_dict[name] = tau*critic_2_state_dict[name].clone() + \
(1-tau)*target_critic_2_state_dict[name].clone()
for name in actor_state_dict:
actor_state_dict[name] = tau*actor_state_dict[name].clone() + \
(1-tau)*target_actor_state_dict[name].clone()
self.target_critic_1.load_state_dict(critic_1_state_dict)
self.target_critic_2.load_state_dict(critic_2_state_dict)
self.target_actor.load_state_dict(actor_state_dict)