-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
185 lines (151 loc) · 7.66 KB
/
models.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
from torch import nn
import torch
from torch.distributions import Categorical
import torch.nn.functional as F
from utils import *
# noinspection PyUnresolvedReferences,PyTypeChecker
class MLPwLSTM(nn.Module):
def __init__(self, inputSpaceDim, outputSpaceDim, archSpecs):
super(MLPwLSTM, self).__init__()
layerSizes = [inputSpaceDim] + archSpecs[0].layerSizes
self.activationFunctions = archSpecs[0].activationFunctions
if len(self.activationFunctions) == (len(layerSizes) - 2):
self.activationFunctions.append('linear')
assert len(self.activationFunctions) == (len(layerSizes) - 1)
self.layers = nn.ModuleList()
for l in range(len(layerSizes) - 1):
self.layers.append(nn.Linear(layerSizes[l], layerSizes[l + 1]))
self.layers.append(nn.LSTM(layerSizes[-1], archSpecs[1].numCells[0]))
self.layers.append(nn.Linear(archSpecs[1].numCells[0], outputSpaceDim))
def forward(self, x, hiddenState, cellState):
for l in range(len(self.layers) - 2):
activationFunction = self.activationFunctions[l]
x = self.layers[l](x) if activationFunction.lower() == 'linear' \
else eval('torch.' + activationFunction.lower())(self.layers[l](x))
x = x.view(-1, 1, x.size(-1))
x, state = self.layers[-2](x, (hiddenState, cellState))
(hiddenState, cellState) = state
x = x.view(-1, x.size(-1))
x = self.layers[-1](x)
return x, hiddenState, cellState
# noinspection PyUnresolvedReferences,PyTypeChecker
class MLP(nn.Module):
def __init__(self, inputSpaceDim, outputSpaceDim, archSpecs):
super(MLP, self).__init__()
layerSizes = [inputSpaceDim] + archSpecs['layerSizes'] + [outputSpaceDim]
useBias = archSpecs['useBias']
self.activationFunctions = archSpecs['activationFunctions']
if len(self.activationFunctions) == (len(layerSizes) - 2):
self.activationFunctions.append('linear')
assert len(self.activationFunctions) == (len(layerSizes) - 1)
self.layers = nn.ModuleList()
for l in range(len(layerSizes) - 1):
self.layers.append(nn.Linear(layerSizes[l], layerSizes[l + 1],
bias=useBias if l < (len(layerSizes) - 2) else True))
def forward(self, x):
for l in range(len(self.layers)):
activationFunction = self.activationFunctions[l]
x = self.layers[l](x) if activationFunction.lower() == 'linear' \
else eval('torch.' + activationFunction.lower())(self.layers[l](x))
return x
# class convCategoricalPolicy()
class MLPCategoricalPolicy(MLP):
def __init__(self, stateSpaceDim, actionSpaceDim, archSpecs):
super().__init__(stateSpaceDim, actionSpaceDim, archSpecs)
def sampleAction(self, state, memory=None):
state = torch.from_numpy(state).float()
actionProbs = F.softmax(self(state), dim=-1)
dist = Categorical(actionProbs)
action = dist.sample()
if memory is not None:
memory['states'].append(state)
memory['actions'].append(action)
memory['logProbs'].append(dist.log_prob(action))
return action.item()
def evaluate(self, states, actions):
actionProbs = F.softmax(self(states), dim=-1)
dist = Categorical(actionProbs)
actionLogProbs = dist.log_prob(actions)
distEntropy = dist.entropy()
return actionLogProbs, distEntropy
class MLPwLSTMCategoricalPolicy(MLPwLSTM):
def __init__(self, stateSpaceDim, actionSpaceDim, archSpecs):
super().__init__(stateSpaceDim, actionSpaceDim, archSpecs)
def sampleAction(self, observation, hiddenState, cellState, memory=None):
observation = torch.from_numpy(observation).float()
hiddenState = torch.from_numpy(hiddenState).float()
cellState = torch.from_numpy(cellState).float()
unnormalizedProbs, hiddenState, cellState = self(observation, hiddenState, cellState)
actionProbs = F.softmax(unnormalizedProbs, dim=-1)
dist = Categorical(actionProbs)
action = dist.sample()
if memory is not None:
memory['states'].append(observation)
memory['actions'].append(action)
memory['logProbs'].append(dist.log_prob(action))
return action.item(), (hiddenState.detach().numpy(), cellState.detach().numpy())
def evaluate(self, observations, actions):
hiddenStates = torch.zeros(1, 1, 128)
cellStates = torch.zeros(1, 1, 128)
unnormalizedProbs, hiddenState, cellState = self(observations, hiddenStates, cellStates)
actionProbs = F.softmax(unnormalizedProbs, dim=-1)
dist = Categorical(actionProbs)
actionLogProbs = dist.log_prob(actions)
distEntropy = dist.entropy()
return actionLogProbs, distEntropy
# noinspection PyUnresolvedReferences
class PPOAgent:
def __init__(self, stateSpaceDim, actionSpaceDim, archSpecs, learningRate, betas=(0.9, 0.999), epsilon=0.2,
gamma=0.99):
self.actor = MLPwLSTMCategoricalPolicy(stateSpaceDim, actionSpaceDim, archSpecs)
self.critic = MLPwLSTM(stateSpaceDim, 1, archSpecs)
self.memory = {
'actions': [],
'states': [],
'logProbs': [],
'rewards': [],
'terminalFlags': []
}
self.optimizer = torch.optim.Adam(list(self.actor.parameters()) + list(self.critic.parameters()),
lr=learningRate, betas=betas)
self.refPolicy = MLPwLSTMCategoricalPolicy(stateSpaceDim, actionSpaceDim, archSpecs)
self.refPolicy.load_state_dict(self.actor.state_dict())
self.MSE = nn.MSELoss()
self.gamma = gamma
self.epsilon = epsilon
def clearMemory(self):
del self.memory['actions'][:]
del self.memory['states'][:]
del self.memory['logProbs'][:]
del self.memory['rewards'][:]
del self.memory['terminalFlags'][:]
def act(self, observation, hiddenState, cellState):
return self.actor.sampleAction(observation, hiddenState, cellState, self.memory)
def learn(self, numEpochs):
rewards = []
discountedReward = 0
for reward, isTerminal in zip(reversed(self.memory['rewards']), reversed(self.memory['terminalFlags'])):
if isTerminal:
discountedReward = 0
discountedReward = reward + (self.gamma * discountedReward)
rewards.insert(0, discountedReward)
rewards = torch.tensor(rewards)
rewards = (rewards - rewards.mean()) / (rewards.std() + 1e-5)
oldStates = torch.stack(self.memory['states']).detach()
oldActions = torch.stack(self.memory['actions']).detach()
oldLogProbs = torch.stack(self.memory['logProbs']).detach()
hiddenStates = torch.zeros(1, 1, 128)
cellStates = torch.zeros(1, 1, 128)
for _ in range(numEpochs):
logProbs, distEntropy = self.actor.evaluate(oldStates, oldActions)
stateValues, _, _ = self.critic(oldStates, hiddenStates, cellStates)
stateValues = torch.squeeze(stateValues)
ratios = torch.exp(logProbs - oldLogProbs.detach())
advantages = rewards - stateValues.detach()
surrogateLoss = ratios * advantages
clippedLoss = torch.clamp(ratios, 1 - self.epsilon, 1 + self.epsilon) * advantages
loss = -torch.min(surrogateLoss, clippedLoss) + 0.5 * self.MSE(stateValues, rewards) - 0.01 * distEntropy
self.optimizer.zero_grad()
loss.mean().backward()
self.optimizer.step()
self.refPolicy.load_state_dict(self.actor.state_dict())