-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathddpg.py
227 lines (184 loc) · 8.44 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Deep Deterministic Policy Gradient agent
Author: Sameera Lanka
Website: https://sameera-lanka.com
"""
# Torch
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
# Lib
import numpy as np
import random
from copy import deepcopy
from dm_control import suite
import matplotlib.pyplot as plt
get_ipython().magic(u'matplotlib inline')
#from IPython.display import clear_output
from IPython import display
import os
# Files
from noise import OrnsteinUhlenbeckActionNoise as OUNoise
from replaybuffer import Buffer
from actorcritic import Actor, Critic
# Hyperparameters
ACTOR_LR = 0.0001
CRITIC_LR = 0.001
MINIBATCH_SIZE = 64
NUM_EPISODES = 10000
MU = 0
SIGMA = 0.2
CHECKPOINT_DIR = './checkpoints/manipulator/'
BUFFER_SIZE = 1000000
DISCOUNT = 0.9
TAU = 0.001
WARMUP = 70
EPSILON = 1.0
EPSILON_DECAY = 1e-6
def obs2state(observation):
"""Converts observation dictionary to state tensor"""
l1 = [val.tolist() for val in list(observation.values())]
l2 = []
for sublist in l1:
try:
l2.extend(sublist)
except:
l2.append(sublist)
return torch.FloatTensor(l2).view(1, -1)
class DDPG:
def __init__(self, env):
self.env = env
self.stateDim = obs2state(env.reset().observation).size()[1]
self.actionDim = env.action_spec().shape[0]
self.actor = Actor(self.env).cuda()
self.critic = Critic(self.env).cuda()
self.targetActor = deepcopy(Actor(self.env)).cuda()
self.targetCritic = deepcopy(Critic(self.env)).cuda()
self.actorOptim = optim.Adam(self.actor.parameters(), lr=ACTOR_LR)
self.criticOptim = optim.Adam(self.critic.parameters(), lr=CRITIC_LR)
self.criticLoss = nn.MSELoss()
self.noise = OUNoise(mu=np.zeros(self.actionDim), sigma=SIGMA)
self.replayBuffer = Buffer(BUFFER_SIZE)
self.batchSize = MINIBATCH_SIZE
self.checkpoint_dir = CHECKPOINT_DIR
self.discount = DISCOUNT
self.warmup = WARMUP
self.epsilon = EPSILON
self.epsilon_decay = EPSILON_DECAY
self.rewardgraph = []
self.start = 0
self.end = NUM_EPISODES
def getQTarget(self, nextStateBatch, rewardBatch, terminalBatch):
"""Inputs: Batch of next states, rewards and terminal flags of size self.batchSize
Calculates the target Q-value from reward and bootstraped Q-value of next state
using the target actor and target critic
Outputs: Batch of Q-value targets"""
targetBatch = torch.FloatTensor(rewardBatch).cuda()
nonFinalMask = torch.ByteTensor(tuple(map(lambda s: s != True, terminalBatch)))
nextStateBatch = torch.cat(nextStateBatch)
nextActionBatch = self.targetActor(nextStateBatch)
nextActionBatch.volatile = True
qNext = self.targetCritic(nextStateBatch, nextActionBatch)
nonFinalMask = self.discount * nonFinalMask.type(torch.cuda.FloatTensor)
targetBatch += nonFinalMask * qNext.squeeze().data
return Variable(targetBatch, volatile=False)
def updateTargets(self, target, original):
"""Weighted average update of the target network and original network
Inputs: target actor(critic) and original actor(critic)"""
for targetParam, orgParam in zip(target.parameters(), original.parameters()):
targetParam.data.copy_((1 - TAU)*targetParam.data + \
TAU*orgParam.data)
def getMaxAction(self, curState):
"""Inputs: Current state of the episode
Returns the action which maximizes the Q-value of the current state-action pair"""
noise = self.epsilon * Variable(torch.FloatTensor(self.noise()), volatile=True).cuda()
action = self.actor(curState)
actionNoise = action + noise
return actionNoise
def train(self):
if not os.path.exists(self.checkpoint_dir):
os.makedirs(self.checkpoint_dir)
print('Training started...')
for i in range(self.start, self.end):
time_step = self.env.reset()
ep_reward = 0
while not time_step.last():
#Visualize Training
display.clear_output(wait=True)
plt.imshow(self.env.physics.render())
plt.show()
# Get maximizing action
curState = Variable(obs2state(time_step.observation), volatile=True).cuda()
self.actor.eval()
action = self.getMaxAction(curState)
curState.volatile = False
action.volatile = False
self.actor.train()
# Step episode
time_step = self.env.step(action.data)
nextState = Variable(obs2state(time_step.observation), volatile=True).cuda()
reward = time_step.reward
ep_reward += reward
terminal = time_step.last()
# Update replay bufer
self.replayBuffer.append((curState, action, nextState, reward, terminal))
# Training loop
if len(self.replayBuffer) >= self.warmup:
curStateBatch, actionBatch, nextStateBatch, \
rewardBatch, terminalBatch = self.replayBuffer.sample_batch(self.batchSize)
curStateBatch = torch.cat(curStateBatch)
actionBatch = torch.cat(actionBatch)
qPredBatch = self.critic(curStateBatch, actionBatch)
qTargetBatch = self.getQTarget(nextStateBatch, rewardBatch, terminalBatch)
# Critic update
self.criticOptim.zero_grad()
criticLoss = self.criticLoss(qPredBatch, qTargetBatch)
print('Critic Loss: {}'.format(criticLoss))
criticLoss.backward()
self.criticOptim.step()
# Actor update
self.actorOptim.zero_grad()
actorLoss = -torch.mean(self.critic(curStateBatch, self.actor(curStateBatch)))
print('Actor Loss: {}'. format(actorLoss))
actorLoss.backward()
self.actorOptim.step()
# Update Targets
self.updateTargets(self.targetActor, self.actor)
self.updateTargets(self.targetCritic, self.critic)
self.epsilon -= self.epsilon_decay
if i % 20 == 0:
self.save_checkpoint(i)
self.rewardgraph.append(ep_reward)
def save_checkpoint(self, episode_num):
checkpointName = self.checkpoint_dir + 'ep{}.pth.tar'.format(episode_num)
checkpoint = {
'episode': episode_num,
'actor': self.actor.state_dict(),
'critic': self.critic.state_dict(),
'targetActor': self.targetActor.state_dict(),
'targetCritic': self.targetCritic.state_dict(),
'actorOpt': self.actorOptim.state_dict(),
'criticOpt': self.criticOptim.state_dict(),
'replayBuffer': self.replayBuffer,
'rewardgraph': self.rewardgraph,
'epsilon': self.epsilon
}
torch.save(checkpoint, checkpointName)
def loadCheckpoint(self, checkpointName):
if os.path.isfile(checkpointName):
print("Loading checkpoint...")
checkpoint = torch.load(checkpointName)
self.start = checkpoint['episode'] + 1
self.actor.load_state_dict(checkpoint['actor'])
self.critic.load_state_dict(checkpoint['critic'])
self.targetActor.load_state_dict(checkpoint['targetActor'])
self.targetCritic.load_state_dict(checkpoint['targetCritic'])
self.actorOptim.load_state_dict(checkpoint['actorOpt'])
self.criticOptim.load_state_dict(checkpoint['criticOpt'])
self.replayBuffer = checkpoint['replayBuffer']
self.rewardgraph = checkpoint['rewardgraph']
self.epsilon = checkpoint['epsilon']
print('Checkpoint loaded')
else:
raise OSError('Checkpoint not found')