-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_cheetah_dir.py
149 lines (119 loc) · 6.35 KB
/
meta_cheetah_dir.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
import argparse
import sys
import time
import numpy as np
import tensorflow as tf
import tensorflow.compat.v1 as tf1
from data import EnvironmentsReplayBuffer
from envs import env_utils
from nn_bruno_net import BrunoNet
from sac_bruno import BrunoSAC
from sac_oracle import OracleSAC
from utils import nn_utils, misc_utils
from utils.nn_utils import gaussian_loglikelihood, squashing_func
np.random.seed(317071)
tf1.reset_default_graph()
tf1.set_random_seed(0)
def value_nn(inputs, hidden_layers_sizes=(128,), scope_name='critic_nn'):
input_shape = tf.shape(inputs)
batch_size = input_shape[0]
seq_len = input_shape[1]
input_dim = nn_utils.int_shape(inputs)[-1]
inputs = tf.reshape(inputs, (batch_size * seq_len, input_dim))
with tf1.variable_scope(scope_name):
for i, size in enumerate(hidden_layers_sizes):
inputs = tf.layers.dense(inputs, size,
activation=tf.nn.leaky_relu,
name=scope_name + '_l' + str(i))
out = tf.layers.dense(inputs, 1, activation=None, name=scope_name + '_l_out')
out = tf.reshape(out, (batch_size, seq_len))
return out
def policy_nn(inputs, hidden_layers_sizes=(128, 128,), scope_name='actor_nn'):
with tf1.variable_scope(scope_name):
for i, size in enumerate(hidden_layers_sizes):
inputs = tf.layers.dense(inputs, size, activation=tf.nn.leaky_relu, name=scope_name + '_l%d' % i)
mu = tf.layers.dense(inputs, action_dim, activation=None, name=scope_name + '_l_mu')
LOG_STD_MAX = 2
LOG_STD_MIN = -5
log_std = tf.layers.dense(inputs, action_dim, activation=tf.tanh, name=scope_name + '_l_sigma')
log_std = LOG_STD_MIN + 0.5 * (LOG_STD_MAX - LOG_STD_MIN) * (log_std + 1)
sigma = tf.exp(log_std)
samples = mu + sigma * tf.random.normal(tf.shape(mu))
log_prob = gaussian_loglikelihood(samples, mu, sigma)
mu, samples, log_prob = squashing_func(mu, samples, log_prob)
return mu, samples, log_prob
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--train', dest='train', action='store_true')
group.add_argument('--test', dest='train', action='store_false')
parser.set_defaults(train=None)
parser.add_argument('--oracle', action='store_true', help='use oracle (default: False)')
args = parser.parse_args()
print('train:', args.train)
print('oracle:', args.oracle)
config_name = __file__.split('/')[-1].replace('.py', '')
config_name = config_name + '_oracle' if args.oracle else config_name
train_params, test_params = [-1, 1.], [-1., 1.]
train_envs_names, test_envs_names = env_utils.get_envs(env_type='HalfCheetahDirEnv',
train_env_params=train_params,
test_env_params=test_params)
train_envs = [env_utils.initialize_envs([name], 43 + i)[0] for i, name in enumerate(train_envs_names)]
test_envs = [env_utils.initialize_envs([name], 42)[0] for name in test_envs_names]
print('Train envs:', env_utils.get_env_id(train_envs))
print('Test envs:', env_utils.get_env_id(test_envs))
action_dim = train_envs[0].action_space.shape[0]
obs_dim = train_envs[0].observation_space.shape[0]
print('observations dim', obs_dim)
print('actions dim', action_dim)
env_params_dim = len(train_params) # 1-hot encoding
extra_dims = 4 # extra latent dimensions
seq_len = 100
context_len = [25, 75]
batch_size = 10
bruno_model = BrunoNet(action_dim=action_dim, obs_dim=obs_dim, reward_dim=1, name='bruno',
maf_num_hidden=128,
n_maf_layers=6,
weight_norm=True,
extra_dims=extra_dims,
min_max_context_len=context_len,
use_posterior_var=True)
replay_buffer = EnvironmentsReplayBuffer(env_params=train_params, obs_dim=obs_dim, act_dim=action_dim,
size=int(1e6), encoding='1hot')
if args.oracle:
algorithm = OracleSAC(train_envs=train_envs, test_envs=test_envs,
replay_buffer=replay_buffer,
action_dim=action_dim, obs_dim=obs_dim, reward_dim=1, env_params_dim=env_params_dim,
qf1=value_nn, qf2=value_nn, vf=value_nn, policy=policy_nn,
seq_len=seq_len,
target_entropy='auto',
policy_lr=0.001, qf_lr=0.001, alpha_lr=0.001)
else:
algorithm = BrunoSAC(train_envs=train_envs, test_envs=test_envs,
action_dim=action_dim, obs_dim=obs_dim, reward_dim=1, env_params_dim=env_params_dim,
latent_dim=bruno_model.latent_ndim,
qf1=value_nn, qf2=value_nn, vf=value_nn, bruno_model=bruno_model, policy=policy_nn,
seq_len=seq_len,
target_entropy='auto',
policy_lr=0.001, qf_lr=0.001, alpha_lr=0.001, model_lr=0.001)
if args.train:
exp_id = '%s-%s' % (config_name, time.strftime("%Y_%m_%d", time.localtime()))
save_dir = 'metadata/' + exp_id
misc_utils.autodir(save_dir)
print(exp_id)
# output logs
misc_utils.autodir('logs')
sys.stdout = misc_utils.Logger('logs/%s.log' % exp_id)
sys.stderr = sys.stdout
algorithm.train(max_episodes=2500, n_exploration_episodes=250,
min_collected_episodes=20,
max_episode_length=200, max_test_episode_length=200,
n_updates=200,
batch_size_episodes=batch_size, batch_seq_len=seq_len,
replay_buffer=replay_buffer,
n_save_iter=20, plot_n_steps=0, plot_diagnostics=False, n_test_episodes=10,
save_dir=save_dir)
else:
save_dir = misc_utils.find_model_metadata('metadata', config_name)
algorithm.test(max_episode_length=1000, train_iteration=0, n_episodes=10, save_dir=save_dir,
plot_n_steps=1000, dump_data=True, plot_diagnostics=False)