-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
226 lines (182 loc) · 7.34 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
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
"""Model Definations for trpo."""
import gym
import numpy as np
import torch
import time
import scipy.optimize
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from distributions import DiagonalGaussian
from helpers import get_flat_params, set_flat_params, get_flat_grads
#from helpers import sample_trajectories, compute_advantage_returns, get_flat_params
class Model(object):
"""Generic Model Template"""
def __init__(self,
observation_space,
action_space,
**kwargs):
#super(Model).__init__(**kwargs)
self.observation_space = observation_space
self.action_space = action_space
self.obs_dim = None
self.act_dim = None
if isinstance(self.observation_space, gym.spaces.Box):
self.obs_dim = np.prod(self.observation_space.shape)
else:
self.obs_dim = self.observation_space.n
if isinstance(self.action_space, gym.spaces.Box):
self.act_dim = np.prod(self.action_space.shape)
else:
self.act_dim = self.action_space.n
class MLP_Policy(nn.Module):
"""MLP model fo the network"""
def __init__(self, input_dim, output_dim, name, **kwargs):
super(MLP_Policy, self).__init__()
self.name = name
self.use_new_head = False
self.fc1 = nn.Linear(input_dim, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, output_dim)
self.fc3.weight.data.mul_(0.1)
self.fc3.bias.data.mul_(0.0)
if bool(kwargs):
self.use_new_head = kwargs["use_new_head"]
self.fc4 = nn.Linear(64, output_dim)
else:
self.log_std = nn.Parameter(torch.zeros(output_dim))
#print(self.log_std.size())
#self.bn1 = nn.BatchNorm1d(64)
#self.bn2 = nn.BatchNorm1d(64)
def forward(self, x):
#print(self.fc1(x))
x = torch.tanh(self.fc1(x))
x = torch.tanh(self.fc2(x))
mean = self.fc3(x)
if self.use_new_head:
std = self.fc4(x)
else:
std = self.log_std.expand(mean.size())
#print(mean)
return mean, std
class MLP_Value(nn.Module):
"""MLP model fo the network"""
def __init__(self, input_dim, output_dim, name, **kwargs):
super(MLP_Value, self).__init__()
self.name = name
self.fc1 = nn.Linear(input_dim, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, output_dim)
self.fc3.weight.data.mul_(0.1)
self.fc3.bias.data.mul_(0.0)
def forward(self, x):
#print(self.fc1(x))
x = torch.tanh(self.fc1(x))
x = torch.tanh(self.fc2(x))
out = self.fc3(x)
return out
class GaussianMLPPolicy(Model):
"""Gaussian MLP Policy"""
def __init__(self, observation_space, action_space, **kwargs):
Model.__init__(self, observation_space, action_space, **kwargs)
#self.mean_network = MLP(self.obs_dim, self.act_dim, "mean").type(torch.float64)
self.std_net = None
#self.std_network = None
#print(kwargs)
if bool(kwargs):
self.std_net = kwargs["use_std_net"]
if self.std_net:
self.network = MLP_Policy(self.obs_dim, self.act_dim, "MLP_policy", use_new_head=True)#.type(torch.float64)
else:
self.network = MLP_Policy(self.obs_dim, self.act_dim, "MLP_policy")#.type(torch.float64)
def actions(self, obs):
obs = torch.from_numpy(obs)
mean, log_std = self.network(obs)
dist = DiagonalGaussian(mean, log_std)
sample = dist.sample()
return sample, dist.logli(sample)
def get_dists(self, obs):
obs = torch.from_numpy(obs)
mean, log_std = self.network(obs)
dist = DiagonalGaussian(mean, log_std)
return dist
def clear_grads(self):
self.network.zero_grad()
class MLPBaseline(Model):
""""MLP Baseline"""
def __init__(self, observation_space, action_space, **kwargs):
Model.__init__(self, observation_space, action_space, **kwargs)
self.value = MLP_Value(self.obs_dim, 1, "MLP_baseline")
#self.criterion = nn.MSELoss()
#self.optimizer = torch.optim.LBFGS(self.value.parameters())
def predict(self, obs):
obs = torch.tensor(obs)
with torch.no_grad():
val = self.value(obs)
return val
def compute_baseline(self, obs):
obs = Variable(torch.tensor(obs))
return self.value(obs)
def clear_grads(self):
self.value.zero_grad()
def update(self, trajs):
obs = np.asarray(trajs["state"])
#obs = torch.from_numpy(obs)
returns = trajs["returns"]
baselines = trajs["baselines"]
targets = returns * 0.9 + 0.1 * baselines
#returns =
#targets = Variable(returns)
#print(targets)
'''
def closure():
self.clear_grads()
values = self.value(torch.from_numpy(obs))
self.optimizer.zero_grad()
loss = self.criterion(values, targets)
print("LBFGS_LOSS:{}".format(loss))
loss.backward()
return loss
'''
#self.optimizer.step(closure)
#curr_params = get_flat_params(self.value.parameters()).data.detach().double().numpy()
curr_flat_params = get_flat_params(self.value).detach().double().numpy()
def val_loss_grad(x):
set_flat_params(self.value, torch.tensor(x))
self.clear_grads()
#for param in self.value.parameters():
#if param.grad is not None:
#print("HHAHAHAHAHHA")
#param.grad.data.fill_(0)
#values_ = #self.value(torch.from_numpy(obs))
values_ = self.compute_baseline(obs)
#print("VALUES",values_.size())
#print("TARGETS",targets.size())
#print((values_-targets).size())
#time1 = time.time()
vf_loss = (values_ - targets).pow(2).mean()
#print("LBFGS_LOSS:{}".format(vf_loss))
#time2 = time.time()
#print("TIME:{}".format(time2-time1))
#for param in self.value.parameters():
# vf_loss += param.pow(2).sum() * 1e-2
vf_loss.backward()
flat_grad = get_flat_grads(self.value)
return (vf_loss.data.double().numpy(), flat_grad.data.double().numpy())
new_params, _, opt_info = scipy.optimize.fmin_l_bfgs_b(val_loss_grad, curr_flat_params, maxiter=25)
set_flat_params(self.value, torch.tensor(new_params))
print(opt_info)
def test_policy_value():
env = gym.make("MountainCarContinuous-v0")
policy = GaussianMLPPolicy(env.observation_space, env.action_space, use_std_net=True)
paths = sample_trajectories(env, policy, 1000)
print(len(paths["rewards"]))
baseline = MLPBaseline(env.observation_space, env.action_space)
compute_advantage_returns(paths, baseline, 0.9, 0.1)
print(paths.keys())
baseline.update(paths)
print(paths['dist'].keys())
flat_params_mean = get_flat_params(policy.mean_network.parameters())
flat_params_std = get_flat_params(policy.std_network.parameters())
print(flat_params)
#test_policy_value()