-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiquids.py
206 lines (182 loc) · 6.84 KB
/
Liquids.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
import argparse
import os
import gym
from ray.tune.registry import register_env
from ray.rllib.models import ModelCatalog
from ray.rllib.algorithms.ppo import PPO
import time
import ale_py
from ray.rllib.env.wrappers.atari_wrappers import wrap_deepmind
import numpy as np
from ray.rllib.models.modelv2 import ModelV2
from ray.rllib.models.tf.recurrent_net import RecurrentNetwork
from ray.rllib.utils.annotations import override
import tensorflow as tf
from ncps.tf import CfC
class ConvCfCModel(RecurrentNetwork):
"""Example of using the Keras functional API to define a RNN model."""
def __init__(
self,
obs_space,
action_space,
num_outputs,
model_config,
name,
cell_size=64,
):
super(ConvCfCModel, self).__init__(
obs_space, action_space, num_outputs, model_config, name
)
self.cell_size = cell_size
# Define input layers
input_layer = tf.keras.layers.Input(
shape=(None, obs_space.shape[0] * obs_space.shape[1] * obs_space.shape[2]),
name="inputs",
)
state_in_h = tf.keras.layers.Input(shape=(cell_size,), name="h")
seq_in = tf.keras.layers.Input(shape=(), name="seq_in", dtype=tf.int32)
# Preprocess observation with a hidden layer and send to CfC
self.conv_block = tf.keras.models.Sequential(
[
tf.keras.Input(
(obs_space.shape[0] * obs_space.shape[1] * obs_space.shape[2])
), # batch dimension is implicit
tf.keras.layers.Lambda(
lambda x: tf.cast(x, tf.float32) / 255.0
), # normalize input
tf.keras.layers.Reshape(
(obs_space.shape[0], obs_space.shape[1], obs_space.shape[2])
),
tf.keras.layers.Conv2D(
64, 5, padding="same", activation="relu", strides=2
),
tf.keras.layers.Conv2D(
128, 5, padding="same", activation="relu", strides=2
),
tf.keras.layers.Conv2D(
128, 5, padding="same", activation="relu", strides=2
),
tf.keras.layers.Conv2D(
256, 5, padding="same", activation="relu", strides=2
),
tf.keras.layers.GlobalAveragePooling2D(),
]
)
self.td_conv = tf.keras.layers.TimeDistributed(self.conv_block)
dense1 = self.td_conv(input_layer)
cfc_out, state_h = CfC(
cell_size, return_sequences=True, return_state=True, name="cfc"
)(
inputs=dense1,
mask=tf.sequence_mask(seq_in),
initial_state=[state_in_h],
)
# Postprocess CfC output with another hidden layer and compute values
logits = tf.keras.layers.Dense(
self.num_outputs, activation=tf.keras.activations.linear, name="logits"
)(cfc_out)
values = tf.keras.layers.Dense(1, activation=None, name="values")(cfc_out)
# Create the RNN model
self.rnn_model = tf.keras.Model(
inputs=[input_layer, seq_in, state_in_h],
outputs=[logits, values, state_h],
)
self.rnn_model.summary()
@override(RecurrentNetwork)
def forward_rnn(self, inputs, state, seq_lens):
model_out, self._value_out, h = self.rnn_model([inputs, seq_lens] + state)
return model_out, [h]
@override(ModelV2)
def get_initial_state(self):
return [
np.zeros(self.cell_size, np.float32),
]
@override(ModelV2)
def value_function(self):
return tf.reshape(self._value_out, [-1])
def run_closed_loop(algo, config):
print(" \n\n\nRunning closed loop\n\n\n\n")
env = gym.make(args.env, render_mode="human")
env = wrap_deepmind(env)
rnn_cell_size = config["model"]["custom_model_config"]["cell_size"]
obs = env.reset()
state = init_state = [np.zeros(rnn_cell_size, np.float32)]
while True:
action, state, _ = algo.compute_single_action(
obs, state=state, explore=False, policy_id="default_policy"
)
obs, reward, done, _ = env.step(action)
if done:
obs = env.reset()
state = init_state
ModelCatalog.register_custom_model("cfc", ConvCfCModel)
if __name__ == "__main__":
print("Running")
parser = argparse.ArgumentParser()
parser.add_argument("--env", type=str, default="ALE/Breakout-v5")
parser.add_argument("--cont", default="policies/default_policy/policy_state.pkl")
parser.add_argument("--render", action="store_true")
parser.add_argument("--hours", default=2, type=int)
args = parser.parse_args()
register_env("atari_env", lambda env_config: wrap_deepmind(gym.make(args.env)))
config = {
"log_level":"INFO",
"monitor":True,
"env": "atari_env",
"preprocessor_pref": None,
"gamma": 0.99,
"num_gpus": 0,
"num_workers": 4,
"num_envs_per_worker": 1,
"create_env_on_driver": True,
"lambda": 0.95,
"kl_coeff": 0.5,
"clip_rewards": True,
"clip_param": 0.1,
"vf_clip_param": 10.0,
"entropy_coeff": 0.01,
"rollout_fragment_length": 100,
"sgd_minibatch_size": 500,
"train_batch_size": 4000,
"num_sgd_iter": 10,
"recreate_failed_workers":True,
"ignore_worker_failures":True,
"batch_mode": "truncate_episodes",
"observation_filter": "NoFilter",
"model": {
"vf_share_layers": True,
"custom_model": "cfc",
"max_seq_len": 20,
"custom_model_config": {
"cell_size": 64,
},
},
"framework": "tf2",
}
algo = PPO(config=config)
os.makedirs(f"rl_ckpt/{args.env}", exist_ok=True)
if args.cont != "":
algo.load_checkpoint(f"rl_ckpt/{args.env}/{args.cont}")
if args.render == 2222:
run_closed_loop(
algo,
config,
)
else:
print("Training STarted")
start_time = time.time()
last_eval = 0
while True:
info = algo.train()
if time.time() - last_eval > 60 * 2: # every 5 minutes print some stats
print(f"Ran {(time.time()-start_time)/60/60:0.1f} hours")
print(
f" sampled {info['info']['num_env_steps_sampled']/1000:0.0f}k steps"
)
print(f" policy reward: {info['episode_reward_mean']:0.1f}")
last_eval = time.time()
ckpt = algo.save_checkpoint(f"rl_ckpt/{args.env}")
print(f" saved checkpoint '{ckpt}'")
elapsed = (time.time() - start_time) / 60 # in minutes
if elapsed > args.hours * 60:
break