-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
224 lines (185 loc) · 7.29 KB
/
utils.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
from __future__ import division
import gym
import numpy as np
from collections import deque
from gym.spaces.box import Box
from cv2 import resize
import torch
from collections import OrderedDict
class Adam_Optim(object):
def __init__(self, model, lr=0.01, beta1=0.9, beta2=0.999, epsilon=1e-8):
params = model.meta_named_parameters()
self.m_t = dict()
self.v_t = dict()
for name, param in params:
self.m_t[name] = 0
self.v_t[name] = 0
self.t = 0
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = epsilon
self.lr = lr
def update(self, model, loss):
self.t += 1
# loss.backward()
params = OrderedDict(model.meta_named_parameters())
grads = torch.autograd.grad(loss, params.values(), create_graph=True)
with torch.no_grad():
for (name, param), grad in zip(params.items(), grads):
self.m_t[name] = self.beta1 * self.m_t[name] + (1 - self.beta1) * grad
self.v_t[name] = self.beta2 * self.v_t[name] + (1 - self.beta2) * (grad ** 2)
m_cap = self.m_t[name] / (1 - self.beta1 ** self.t)
v_cap = self.v_t[name] / (1 - self.beta2 ** self.t)
new_val = param - self.lr * (m_cap / (torch.sqrt(v_cap) + self.epsilon))
param.copy_(new_val)
def reset(self):
for m, v in zip(self.m_t, self.v_t):
m = 0
v = 0
self.t = 0
def process_frame(frame, crop=34):
frame = frame[crop:crop + 160, :160]
frame = frame.mean(2)
frame = frame.astype(np.float32)
frame *= (1.0 / 255.0)
frame = resize(frame, (80, 80))
frame = np.reshape(frame, [1, 80, 80])
return frame
def atari_env(env_id):
env = gym.make(env_id)
if 'NoFrameskip' in env_id:
assert 'NoFrameskip' in env.spec.id
env._max_episode_steps = 10000 * 4
env = NoopResetEnv(env, noop_max=30)
env = MaxAndSkipEnv(env, skip=4)
else:
env._max_episode_steps = 10000
env = EpisodicLifeEnv(env)
if 'FIRE' in env.unwrapped.get_action_meanings():
env = FireResetEnv(env)
env._max_episode_steps = 10000
env = AtariRescale(env)
env = NormalizedEnv(env)
return env
class AtariRescale(gym.ObservationWrapper):
def __init__(self, env):
gym.ObservationWrapper.__init__(self, env)
self.observation_space = Box(0.0, 1.0, [1, 80, 80], dtype=np.uint8)
def observation(self, observation):
return process_frame(observation)
class NormalizedEnv(gym.ObservationWrapper):
def __init__(self, env=None):
gym.ObservationWrapper.__init__(self, env)
self.state_mean = 0
self.state_std = 0
self.alpha = 0.9999
self.num_steps = 0
def observation(self, observation):
self.num_steps += 1
self.state_mean = self.state_mean * self.alpha + \
observation.mean() * (1 - self.alpha)
self.state_std = self.state_std * self.alpha + \
observation.std() * (1 - self.alpha)
unbiased_mean = self.state_mean / (1 - pow(self.alpha, self.num_steps))
unbiased_std = self.state_std / (1 - pow(self.alpha, self.num_steps))
return (observation - unbiased_mean) / (unbiased_std + 1e-8)
class NoopResetEnv(gym.Wrapper):
def __init__(self, env, noop_max=30):
"""Sample initial states by taking random number of no-ops on reset.
No-op is assumed to be action 0.
"""
gym.Wrapper.__init__(self, env)
self.noop_max = noop_max
self.override_num_noops = None
self.noop_action = 0
assert env.unwrapped.get_action_meanings()[0] == 'NOOP'
def reset(self, **kwargs):
""" Do no-op action for a number of steps in [1, noop_max]."""
self.env.reset(**kwargs)
if self.override_num_noops is not None:
noops = self.override_num_noops
else:
noops = self.unwrapped.np_random.randint(1, self.noop_max + 1) #pylint: disable=E1101
assert noops > 0
obs = None
for _ in range(noops):
obs, _, done, _ = self.env.step(self.noop_action)
if done:
obs = self.env.reset(**kwargs)
return obs
def step(self, ac):
return self.env.step(ac)
class FireResetEnv(gym.Wrapper):
def __init__(self, env):
"""Take action on reset for environments that are fixed until firing."""
gym.Wrapper.__init__(self, env)
assert env.unwrapped.get_action_meanings()[1] == 'FIRE'
assert len(env.unwrapped.get_action_meanings()) >= 3
def reset(self, **kwargs):
self.env.reset(**kwargs)
obs, _, done, _ = self.env.step(1)
if done:
self.env.reset(**kwargs)
obs, _, done, _ = self.env.step(2)
if done:
self.env.reset(**kwargs)
return obs
def step(self, ac):
return self.env.step(ac)
class EpisodicLifeEnv(gym.Wrapper):
def __init__(self, env):
"""Make end-of-life == end-of-episode, but only reset on true game over.
Done by DeepMind for the DQN and co. since it helps value estimation.
"""
gym.Wrapper.__init__(self, env)
self.lives = 0
self.was_real_done = True
def step(self, action):
obs, reward, done, info = self.env.step(action)
self.was_real_done = done
# check current lives, make loss of life terminal,
# then update lives to handle bonus lives
lives = self.env.unwrapped.ale.lives()
if lives < self.lives and lives > 0:
# for Qbert sometimes we stay in lives == 0 condtion for a few frames
# so its important to keep lives > 0, so that we only reset once
# the environment advertises done.
done = True
self.lives = lives
return obs, reward, done, self.was_real_done
def reset(self, **kwargs):
"""Reset only when lives are exhausted.
This way all states are still reachable even though lives are episodic,
and the learner need not know about any of this behind-the-scenes.
"""
if self.was_real_done:
obs = self.env.reset(**kwargs)
else:
# no-op step to advance from terminal/lost life state
obs, _, _, _ = self.env.step(0)
self.lives = self.env.unwrapped.ale.lives()
return obs
class MaxAndSkipEnv(gym.Wrapper):
def __init__(self, env=None, skip=4):
"""Return only every `skip`-th frame"""
super(MaxAndSkipEnv, self).__init__(env)
# most recent raw observations (for max pooling across time steps)
self._obs_buffer = deque(maxlen=3)
self._skip = skip
def step(self, action):
total_reward = 0.0
done = None
for _ in range(self._skip):
obs, reward, done, info = self.env.step(action)
self._obs_buffer.append(obs)
total_reward += reward
if done:
break
max_frame = np.max(np.stack(self._obs_buffer), axis=0)
return max_frame, total_reward, done, info
def reset(self, **kwargs):
"""Clear past frame buffer and init. to first obs. from inner env."""
self._obs_buffer.clear()
obs = self.env.reset(**kwargs)
self._obs_buffer.append(obs)
return obs