forked from xuwd11/cs294-112_hws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
192 lines (149 loc) · 5.51 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
import numpy as np
import tensorflow as tf
from logger import logger
############
### Data ###
############
class Dataset(object):
def __init__(self):
self._states = []
self._actions = []
self._next_states = []
self._rewards = []
self._dones = []
@property
def is_empty(self):
return len(self) == 0
def __len__(self):
return len(self._states)
##################
### Statistics ###
##################
@property
def state_mean(self):
return np.mean(self._states, axis=0)
@property
def state_std(self):
return np.std(self._states, axis=0)
@property
def action_mean(self):
return np.mean(self._actions, axis=0)
@property
def action_std(self):
return np.std(self._actions, axis=0)
@property
def delta_state_mean(self):
return np.mean(np.array(self._next_states) - np.array(self._states), axis=0)
@property
def delta_state_std(self):
return np.std(np.array(self._next_states) - np.array(self._states), axis=0)
###################
### Adding data ###
###################
def add(self, state, action, next_state, reward, done):
"""
Add (s, a, r, s') to this dataset
"""
if not self.is_empty:
# ensure the state, action, next_state are of the same dimension
assert len(self._states[-1]) == len(np.ravel(state))
assert len(self._actions[-1]) == len(np.ravel(action))
assert len(self._next_states[-1]) == len(np.ravel(next_state))
self._states.append(np.ravel(state))
self._actions.append(np.ravel(action))
self._next_states.append(np.ravel(next_state))
self._rewards.append(reward)
self._dones.append(done)
def append(self, other_dataset):
"""
Append other_dataset to this dataset
"""
if not self.is_empty and not other_dataset.is_empty:
# ensure the state, action, next_state are of the same dimension
assert len(self._states[-1]) == len(other_dataset._states[-1])
assert len(self._actions[-1]) == len(other_dataset._actions[-1])
assert len(self._next_states[-1]) == len(other_dataset._next_states[-1])
self._states += other_dataset._states
self._actions += other_dataset._actions
self._next_states += other_dataset._next_states
self._rewards += other_dataset._rewards
self._dones += other_dataset._dones
############################
### Iterate through data ###
############################
def rollout_iterator(self):
"""
Iterate through all the rollouts in the dataset sequentially
"""
end_indices = np.nonzero(self._dones)[0] + 1
states = np.asarray(self._states)
actions = np.asarray(self._actions)
next_states = np.asarray(self._next_states)
rewards = np.asarray(self._rewards)
dones = np.asarray(self._dones)
start_idx = 0
for end_idx in end_indices:
indices = np.arange(start_idx, end_idx)
yield states[indices], actions[indices], next_states[indices], rewards[indices], dones[indices]
start_idx = end_idx
def random_iterator(self, batch_size):
"""
Iterate once through all (s, a, r, s') in batches in a random order
"""
all_indices = np.nonzero(np.logical_not(self._dones))[0]
np.random.shuffle(all_indices)
states = np.asarray(self._states)
actions = np.asarray(self._actions)
next_states = np.asarray(self._next_states)
rewards = np.asarray(self._rewards)
dones = np.asarray(self._dones)
i = 0
while i < len(all_indices):
indices = all_indices[i:i+batch_size]
yield states[indices], actions[indices], next_states[indices], rewards[indices], dones[indices]
i += batch_size
###############
### Logging ###
###############
def log(self):
end_idxs = np.nonzero(self._dones)[0] + 1
returns = []
start_idx = 0
for end_idx in end_idxs:
rewards = self._rewards[start_idx:end_idx]
returns.append(np.sum(rewards))
start_idx = end_idx
logger.record_tabular('ReturnAvg', np.mean(returns))
logger.record_tabular('ReturnStd', np.std(returns))
logger.record_tabular('ReturnMin', np.min(returns))
logger.record_tabular('ReturnMax', np.max(returns))
##################
### Tensorflow ###
##################
def build_mlp(input_layer,
output_dim,
scope,
n_layers=1,
hidden_dim=500,
activation=tf.nn.relu,
output_activation=None,
reuse=False):
layer = input_layer
with tf.variable_scope(scope, reuse=reuse):
for _ in range(n_layers):
layer = tf.layers.dense(layer, hidden_dim, activation=activation)
layer = tf.layers.dense(layer, output_dim, activation=output_activation)
return layer
def normalize(x, mean, std, eps=1e-8):
return (x - mean) / (std + eps)
def unnormalize(x, mean, std):
return x * std + mean
################
### Policies ###
################
class RandomPolicy(object):
def __init__(self, env):
self._action_space_low = env.action_space.low
self._action_space_high = env.action_space.high
def get_action(self, state):
return np.random.uniform(self._action_space_low, self._action_space_high)