-
Notifications
You must be signed in to change notification settings - Fork 11
/
DDPG.py
204 lines (145 loc) · 7.82 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import torch
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
import os
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import copy
from itertools import count
from torch.distributions import Categorical
capacity=1000000
batch_size=64
update_iteration=200
tau=0.001
# tau for soft updating
gamma=0.99
directory = './'
hidden1=20
hidden2=64
import numpy as np
import random
import copy
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Replay_buffer():
def __init__(self, max_size=capacity):
"""
This code snippet creates a Replay buffer.
The buffer is designed to store a maximum number of transitions, indicated by the "size" parameter.
When the buffer reaches its maximum capacity, it starts dropping the oldest memories to make space for new ones.
"""
self.storage = []
self.max_size = max_size
self.ptr = 0
def push(self, data):
if len(self.storage) == self.max_size: #If storage is full
self.storage[int(self.ptr)] = data #Store data where the pointer currently points at
self.ptr = (self.ptr + 1) % self.max_size #Increment pointer. if pointer is at last index, the modulus ensures it reutrn to begiining
else:
self.storage.append(data) #If buffer has not yet been filled, keep appending to buffer
def sample(self, batch_size):
ind = np.random.randint(0, len(self.storage), size=batch_size) #Choose random batch of indices
state, next_state, probs, action, reward, done = [], [], [], [], [], [] #Initialise list for sampled experience
for i in ind:
st, n_st, prbs, act, rew, dn = self.storage[i] #iterate through indices and retrieve sampled experience
state.append(np.array(st, copy=False))
next_state.append(np.array(n_st, copy=False))
probs.append(np.array(prbs, copy=False))
action.append(np.array(act, copy=False))
reward.append(np.array(rew, copy=False))
done.append(np.array(dn, copy=False))
#return batch of sampled experiences
return np.array(state), np.array(next_state), np.array(probs), np.array(action), np.array(reward).reshape(-1, 1), np.array(done).reshape(-1, 1)
class Actor(nn.Module):
def __init__(self, n_states, action_dim, hidden1):
super(Actor, self).__init__()
self.net = nn.Sequential( #Initialise our actor netowrk with 2 hidden layers and ReLU activation
nn.Linear(n_states, hidden1),
nn.ReLU(),
nn.Linear(hidden1, hidden1),
nn.ReLU(),
nn.Linear(hidden1, hidden1),
nn.ReLU(),
nn.Linear(hidden1, action_dim)
)
def forward(self, state):
action = self.net(state) #Get action probabilities from actor network
softmax_tensor = torch.softmax(action, dim = 0) #Normalise them such that they sum to 1
cat_dist = Categorical(probs=softmax_tensor) #Use our probabilities to create a categorical distribution
sampled_action = cat_dist.sample() #Sample from the categorical distribution
return action,sampled_action #Return action probabilities, as well as the sampled action
class Critic(nn.Module):
def __init__(self, n_states, action_dim, hidden2):
super(Critic, self).__init__()
self.net = nn.Sequential(
nn.Linear(n_states + action_dim, hidden2), #Initialise our actor netowrk with 2 hidden layers and ReLU activation
nn.ReLU(),
nn.Linear(hidden2, hidden2),
nn.ReLU(),
nn.Linear(hidden2, hidden2),
nn.ReLU(),
nn.Linear(hidden2, 1)
)
def forward(self, state, action):
return self.net(torch.cat((state, action), 1)) #Return our Q value output
class DDPG(object):
def __init__(self, state_dim, action_dim):
self.replay_buffer = Replay_buffer() #initialise replay buffer
self.actor = Actor(state_dim, action_dim, hidden1).to(device) #initialise actor network
self.actor_target = Actor(state_dim, action_dim, hidden1).to(device)
self.actor_target.load_state_dict(self.actor.state_dict())
self.actor_optimizer = optim.Adam(self.actor.parameters(), lr=3e-3)
self.critic = Critic(state_dim, 1, hidden2).to(device) #initialise critic network
self.critic_target = Critic(state_dim, 1, hidden2).to(device)
self.critic_target.load_state_dict(self.critic.state_dict())
self.critic_optimizer = optim.Adam(self.critic.parameters(), lr=2e-2)
#initialise hyperparameters
self.num_critic_update_iteration = 0
self.num_actor_update_iteration = 0
self.num_training = 0
def select_action(self, state):
state = torch.FloatTensor(state.reshape(1, -1)).to(device) #Pass the state vector to the actor network
return self.actor(state)[0].cpu().data.numpy().flatten(), self.actor(state)[1].cpu().data.numpy().flatten() #Return the action and probability vector
def update(self):
for it in range(update_iteration):
# For each Sample in replay buffer batch
state, next_state, probs, action, reward, done = self.replay_buffer.sample(batch_size)
state = torch.FloatTensor(state).to(device) #convert numpy array in replay buffer to tensor
probs = torch.FloatTensor(probs).to(device)
action = torch.FloatTensor(action).to(device)
next_state = torch.FloatTensor(next_state).to(device)
done = torch.FloatTensor(1-done).to(device)
reward = torch.FloatTensor(reward).to(device)
# Compute the target Q value
target_Q = self.critic_target(next_state, np.reshape(self.actor_target(next_state)[1],(-1,1)))
target_Q = reward + (done * gamma * target_Q).detach()
# Get current Q estimate
current_Q = self.critic(state, action)
# Compute critic loss
critic_loss = F.mse_loss(current_Q, target_Q)
# Optimize the critic
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
#Initialise out categorical distribution using the probabilities we obtained from the actor network.
dist = Categorical(logits=probs)
# Compute actor loss as the negative mean Q value using the critic network and the actor network
actor_loss = -dist.log_prob(action).mean()*self.critic(state, np.reshape(self.actor_target(next_state)[1],(-1,1))).mean()
# Optimize the actor
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
"""
Update the frozen target models using
soft updates, where
tau,a small fraction of the actor and critic network weights are transferred to their target counterparts.
"""
for param, target_param in zip(self.critic.parameters(), self.critic_target.parameters()):
target_param.data.copy_(tau * param.data + (1 - tau) * target_param.data)
for param, target_param in zip(self.actor.parameters(), self.actor_target.parameters()):
target_param.data.copy_(tau * param.data + (1 - tau) * target_param.data)
self.num_actor_update_iteration += 1
self.num_critic_update_iteration += 1