-
Notifications
You must be signed in to change notification settings - Fork 0
/
pend_npg_GAE.py
201 lines (153 loc) · 5.95 KB
/
pend_npg_GAE.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
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from actor import Actor
from algorithms import GAE, ActorOnlyMC, NPG, get_returns
from utils import sample_memory
import gym
from experiment_class import Experiment, mult_seed_exp
from time import time
import os
class Critic(nn.Module):
def __init__(self, num_inputs, num_hidden):
super(Critic, self).__init__()
self.fc1 = nn.Linear(num_inputs, num_hidden)
self.fc2 = nn.Linear(num_hidden, num_hidden)
self.fc3 = nn.Linear(num_hidden, 1)
# init weights
gains = [np.sqrt(2), np.sqrt(2), 1]
layers = [self.fc1, self.fc2, self.fc3]
for i in range(len(layers)):
nn.init.xavier_uniform_(layers[i].weight, gain=gains[i])
layers[i].bias.data.fill_(0.01)
def forward(self, x):
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
v = self.fc3(x)
return v
class ContActor(Actor):
'''
Actor for continous action space
'''
def __init__(self, num_input, num_hidden, **kwargs):
super(ContActor, self).__init__(dist_type=torch.distributions.normal.Normal)
self.fc1 = nn.Linear(num_input, num_hidden)
self.fc2 = nn.Linear(num_hidden, num_hidden)
self.fc3 = nn.Linear(num_hidden, 2)
# init weights
gains = [np.sqrt(2), np.sqrt(2), 1]
layers = [self.fc1, self.fc2, self.fc3]
for i in range(len(layers)):
nn.init.xavier_uniform_(layers[i].weight, gain=gains[i])
layers[i].bias.data.fill_(0.01)
#self.log_var = nn.parameter.Parameter(data=torch.Tensor([0]), requires_grad=True)
# override the parent class' forward method
def forward(self, states):
'''
Computes parameters of gaussian distributions over actions given a tensor of states
Input:
states - pytorch tensor of shape (batch, *|S|)
Output:
params - pytorch tensor of shape (batch, *|params|)
'''
x = nn.functional.relu(self.fc1(states))
x = nn.functional.relu(self.fc2(x))
params = self.fc3(x)
params[:,1] = torch.exp(params[:,1]) # make sure variance is positive
#var = torch.exp(self.log_var)
#params = torch.cat((params, torch.ones_like(params) * var), dim=1)
return params
# run a single experiment
####
# dont change these params:
ac_kwargs = {'num_hidden':64}
critic_kwargs = {'num_hidden':64}
critic_optim_kwargs = {'lr':3e-4}
target_alg_kwargs = {'batch_size':16, 'epochs':3}
num_iters = 200
seeds = [0,1,2,3,4,5,6,7,8,9] # subject to change
log_dir = './pendulum/npg/GAE/'
try:
os.mkdir(log_dir)
except:
pass
try:
os.mkdir(log_dir+'plots/')
except:
pass
####
####
# do change these params:
target_alg_kwargs['gamma'] = 0.8
target_alg_kwargs['lamda'] = 0.97
ep_per_iter = 5
ac_alg_kwargs = {'lr':0.1}
lr_list = [0.1]
####
experiment_parameters = {'seed':42,
'env_str':'Pendulum-v0',
'ac':ContActor,
'ac_kwargs':ac_kwargs,
'ac_alg':NPG,
'ac_alg_kwargs':ac_alg_kwargs,
'target_alg':GAE,
'target_alg_kwargs':target_alg_kwargs,
'critic':Critic,
'critic_kwargs':critic_kwargs,
'critic_optim':torch.optim.Adam,
'critic_optim_kwargs':critic_optim_kwargs,
'num_iters':num_iters,
'ep_per_iter':ep_per_iter,
'log_file':'./pendulum/npg/GAE/single.npz'}
search_time = time()
max_return = -np.inf
lr_return_list = []
lr_return_std_list = []
for n in range(len(lr_list)):
trial_time = time()
# draw a sample from the loguniform distribution
lr = lr_list[n]
print('\nNow running wiht lr = {}'.format(lr))
# set up experiment
ac_alg_kwargs['lr'] = lr
experiment_parameters['ac_alg_kwargs'] = ac_alg_kwargs
trial_log_dir = log_dir + 'lr_{0:1.4f}_gam_{1:1.2f}_lam_{2:1.2f}/'.format(lr, target_alg_kwargs['gamma'], target_alg_kwargs['lamda'])
try:
os.mkdir(trial_log_dir)
os.mkdir(trial_log_dir+'plots/')
except:
pass
#experiment = Experiment(**experiment_parameters)
# run mutliple experiments with different seeds
mult_exp = mult_seed_exp(experiment_parameters, seeds, trial_log_dir, save_all=False)
mult_exp.run()
try:
mult_exp.plot(trial_log_dir+'plots/')
except:
pass
# get results
mean_results, std_results = mult_exp.get_mean_results()
print('Mean return at termination with lr {0:1.5f} was {1:1.2f}'.format(lr, mean_results[-1,0]))
lr_return_list.append(mean_results[-1,0])
lr_return_std_list.append(std_results[-1,0])
if mean_results[1,0]>max_return:
print('Updating max return run..')
max_return = mean_results[-1,0]
mult_exp.plot(log_dir+ 'plots/best_run_')
#mult_exp.plot(plot_path=log_dir+'plots/')
print('This trial took {0:1.2f} seconds'.format((time()-trial_time)))
print('Hyperparameter search finished. Time: {0:1.0f} minutes and {1:1.2f} seconds'.format((time()-search_time)//60, (time()-search_time)))
print('Plotting Return - KL diagram')
plt.figure()
plt.errorbar(lr_list, lr_return_list, yerr=lr_return_std_list, fmt='o', capsize=6)
plt.xlabel('lr')
plt.ylabel('Log Final Return')
#plt.yscale('log')
plt.savefig(log_dir + 'plots/lr_return_plot.pdf')
# save results
np.savez_compressed(log_dir + 'kl_return_summary.npz', lr_list=lr_list, lr_return_list=lr_return_list, lr_return_std_list=lr_return_std_list)
#def run_loguniform_hp_search(low, high, num_trials, keyword, params):
# '''
# Runs a loguniform hyperparameter search of the param 'keyword'
# '''