-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_preference.py
341 lines (304 loc) · 13.4 KB
/
custom_preference.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from imitation.algorithms import preference_comparisons
from imitation.rewards import reward_nets
"""
for making tensorboard
"""
from typing import Optional, Union, Callable, Mapping, Any
from imitation.util import logger as imit_logger
from stable_baselines3.common import type_aliases
import numpy as np
from imitation.util import util
from imitation.data import rollout
import math
import torch as th
import numpy as np
from imitation.data.types import (
TrajectoryWithRew,
TrajectoryWithRewPair,
)
from typing import (
Callable,
Mapping,
Optional,
Sequence,
List,
Tuple,
Union,
)
from scipy import special
class CustomRandomFragmenter(preference_comparisons.Fragmenter):
def __init__(
self,
rng: np.random.Generator,
warning_threshold: int = 10,
custom_logger: Optional[imit_logger.HierarchicalLogger] = None,
allow_variable_horizon: bool = False
) -> None:
super().__init__(custom_logger)
self.rng = rng
self.warning_threshold = warning_threshold
self.allow_variable_horizon = allow_variable_horizon
def __call__(
self,
trajectories: Sequence[TrajectoryWithRew],
fragment_length: int,
num_pairs: int,
) -> Sequence[TrajectoryWithRewPair]:
fragments: List[TrajectoryWithRew] = []
if self.allow_variable_horizon:
trajectories = [traj for traj in trajectories]
else:
prev_num_trajectories = len(trajectories)
trajectories = [traj for traj in trajectories if len(traj) >= fragment_length]
if len(trajectories) == 0:
raise ValueError(
"No trajectories are long enough for the desired fragment length "
f"of {fragment_length}.",
)
num_discarded = prev_num_trajectories - len(trajectories)
if num_discarded:
self.logger.log(
f"Discarded {num_discarded} out of {prev_num_trajectories} "
"trajectories because they are shorter than the desired length "
f"of {fragment_length}.",
)
weights = [len(traj) for traj in trajectories]
num_transitions = 2 * num_pairs * fragment_length
if sum(weights) < num_transitions:
self.logger.warn(
"Fewer transitions available than needed for desired number "
"of fragment pairs. Some transitions will appear multiple times.",
)
elif (
self.warning_threshold
and sum(weights) < self.warning_threshold * num_transitions
):
self.logger.warn(
f"Samples will contain {num_transitions} transitions in total "
f"and only {sum(weights)} are available. "
f"Because we sample with replacement, a significant number "
"of transitions are likely to appear multiple times.",
)
for _ in range(2 * num_pairs):
traj = self.rng.choice(
trajectories,
p=np.array(weights) / sum(weights),
)
n = len(traj)
if self.allow_variable_horizon:
if n>fragment_length:
start = self.rng.integers(0, n - fragment_length, endpoint=True)
end = start + fragment_length
else:
start = 0
end = n
else:
start = self.rng.integers(0, n - fragment_length, endpoint=True)
end = start + fragment_length
terminal = (end == n) and traj.terminal
fragment = TrajectoryWithRew(
obs=traj.obs[start : end + 1],
acts=traj.acts[start:end],
infos=traj.infos[start:end] if traj.infos is not None else None,
rews=traj.rews[start:end],
terminal=terminal,
)
fragments.append(fragment)
iterator = iter(fragments)
return list(zip(iterator, iterator))
class CustomSyntheticGatherer(preference_comparisons.SyntheticGatherer):
def __init__(
self,
temperature: float = 1,
discount_factor: float = 1,
sample: bool = True,
rng: Optional[np.random.Generator] = None,
threshold: float = 50,
custom_logger: Optional[imit_logger.HierarchicalLogger] = None,
allow_variable_horizon : bool = False
) -> None:
super().__init__(temperature, discount_factor, sample, rng, threshold, custom_logger)
self.allow_variable_horizon = allow_variable_horizon
def __call__(self, fragment_pairs: Sequence[TrajectoryWithRewPair]) -> np.ndarray:
"""Computes probability fragment 1 is preferred over fragment 2."""
returns1, returns2 = self._reward_sums(fragment_pairs)
if self.temperature == 0:
return (np.sign(returns1 - returns2) + 1) / 2
returns1 /= self.temperature
returns2 /= self.temperature
returns_diff = np.clip(returns2 - returns1, -self.threshold, self.threshold)
model_probs = 1 / (1 + np.exp(returns_diff))
entropy = -(
special.xlogy(model_probs, model_probs)
+ special.xlogy(1 - model_probs, 1 - model_probs)
).mean()
self.logger.record("entropy", entropy)
if self.sample:
assert self.rng is not None
return self.rng.binomial(n=1, p=model_probs).astype(np.float32)
return model_probs
def _reward_sums(self, fragment_pairs) -> Tuple[np.ndarray, np.ndarray]:
rews1 = []
rews2 = []
for f1, f2 in fragment_pairs:
if self.allow_variable_horizon:
which_min = min(len(f1.rews),len(f2.rews))
rew_1 =np.array(f1.rews)[:which_min]
rew_2 = np.array(f2.rews)[:which_min]
else:
rew_1 = f1.rews
rew_2 = f2.rews
rews1.append(rollout.discounted_sum(rew_1, self.discount_factor))
rews2.append(rollout.discounted_sum(rew_2, self.discount_factor))
return np.array(rews1, dtype=np.float32), np.array(rews2, dtype=np.float32)
class CustomPreferenceModel(preference_comparisons.PreferenceModel):
def __init__(
self,
model: reward_nets.RewardNet,
noise_prob: float = 0.0,
discount_factor: float = 1.0,
threshold: float = 50,
allow_variable_horizon: bool = False,
) -> None:
self.allow_variable_horizon = allow_variable_horizon
super().__init__(model, noise_prob, discount_factor, threshold)
def rewards(self, transitions) -> th.Tensor:
state = transitions.obs
action = transitions.acts
next_state = transitions.next_obs
done = transitions.dones
if self.ensemble_model is not None:
rews_np = self.ensemble_model.predict_processed_all(
state,
action,
next_state,
done,
)
assert rews_np.shape == (len(state), self.ensemble_model.num_members)
rews = util.safe_to_tensor(rews_np).to(self.ensemble_model.device)
else:
preprocessed = self.model.preprocess(state, action, next_state, done)
rews = self.model(*preprocessed)
assert rews.shape == (len(state),)
return rews
def probability(self, rews1: th.Tensor, rews2: th.Tensor) -> th.Tensor:
expected_dims = 2 if self.ensemble_model is not None else 1
assert rews1.ndim == rews2.ndim == expected_dims
if self.allow_variable_horizon:
which_min = min(len(rews2),len(rews1))
rews2 = rews2[:which_min]
rews1 = rews1[:which_min]
if self.discount_factor == 1:
returns_diff = (rews2 - rews1).sum(axis=0)
else:
device = rews1.device
assert device == rews2.device
discounts = self.discount_factor ** th.arange(len(rews1), device=device)
if self.ensemble_model is not None:
discounts = discounts.reshape(-1, 1)
returns_diff = (discounts * (rews2 - rews1)).sum(axis=0)
returns_diff = th.clip(returns_diff, -self.threshold, self.threshold)
model_probability = 1 / (1 + returns_diff.exp())
probability = self.noise_prob * 0.5 + (1 - self.noise_prob) * model_probability
if self.ensemble_model is not None:
assert probability.shape == (self.model.num_members,)
else:
assert probability.shape == ()
return probability
class CustomPreferenceComparisons(preference_comparisons.PreferenceComparisons):
def __init__(
self,
trajectory_generator: preference_comparisons.TrajectoryGenerator,
reward_model: reward_nets.RewardNet,
num_iterations: int,
fragmenter: Optional[preference_comparisons.Fragmenter] = None,
preference_gatherer: Optional[preference_comparisons.PreferenceGatherer] = None,
reward_trainer: Optional[preference_comparisons.RewardTrainer] = None,
comparison_queue_size: Optional[int] = None,
fragment_length: int = 100,
transition_oversampling: float = 1,
initial_comparison_frac: float = 0.1,
initial_epoch_multiplier: float = 200.0,
custom_logger: Optional[imit_logger.HierarchicalLogger] = None,
allow_variable_horizon: bool = False,
rng: Optional[np.random.Generator] = None,
query_schedule: Union[str, type_aliases.Schedule] = "hyperbolic",
tensorboard = None
) -> None:
self.writer = tensorboard
super().__init__(trajectory_generator,
reward_model,
num_iterations,
fragmenter,
preference_gatherer,
reward_trainer,
comparison_queue_size,
fragment_length,
transition_oversampling,
initial_comparison_frac,
initial_epoch_multiplier,
custom_logger,
allow_variable_horizon,
rng,
query_schedule,
)
def train(
self,
total_timesteps: int,
total_comparisons: int,
callback: Optional[Callable[[int], None]] = None,
) -> Mapping[str, Any]:
initial_comparisons = int(total_comparisons * self.initial_comparison_frac)
total_comparisons -= initial_comparisons
vec_schedule = np.vectorize(self.query_schedule)
unnormalized_probs = vec_schedule(np.linspace(0, 1, self.num_iterations))
probs = unnormalized_probs / np.sum(unnormalized_probs)
shares = util.oric(probs * total_comparisons)
schedule = [initial_comparisons] + shares.tolist()
print(f"Query schedule: {schedule}")
timesteps_per_iteration, extra_timesteps = divmod(
total_timesteps,
self.num_iterations,
)
reward_loss = None
reward_accuracy = None
for i, num_pairs in enumerate(schedule):
num_steps = math.ceil(
self.transition_oversampling * 2 * num_pairs * self.fragment_length,
)
self.logger.log(
f"Collecting {2 * num_pairs} fragments ({num_steps} transitions)",
)
trajectories = self.trajectory_generator.sample(num_steps)
horizons = (len(traj) for traj in trajectories if traj.terminal)
self._check_fixed_horizon(horizons)
self.logger.log("Creating fragment pairs")
fragments = self.fragmenter(trajectories, self.fragment_length, num_pairs)
with self.logger.accumulate_means("preferences"):
self.logger.log("Gathering preferences")
preferences = self.preference_gatherer(fragments)
self.dataset.push(fragments, preferences)
self.logger.log(f"Dataset now contains {len(self.dataset)} comparisons")
epoch_multiplier = 1.0
if i == 0:
epoch_multiplier = self.initial_epoch_multiplier
self.reward_trainer.train(self.dataset, epoch_multiplier=epoch_multiplier)
base_key = self.logger.get_accumulate_prefixes() + "reward/final/train"
assert f"{base_key}/loss" in self.logger.name_to_value
assert f"{base_key}/accuracy" in self.logger.name_to_value
reward_loss = self.logger.name_to_value[f"{base_key}/loss"]
reward_accuracy = self.logger.name_to_value[f"{base_key}/accuracy"]
if self.writer is not None:
self.writer.add_scalar('none_gru/loss_final', reward_loss, i)
self.writer.add_scalar('none_gru/accuracy_final', reward_accuracy, i)
num_steps = timesteps_per_iteration
if i == self.num_iterations - 1:
num_steps += extra_timesteps
with self.logger.accumulate_means("agent"):
self.logger.log(f"Training agent for {num_steps} timesteps")
self.trajectory_generator.train(steps=num_steps)
self.logger.dump(self._iteration)
if callback:
callback(self._iteration)
self._iteration += 1
return {"reward_loss": reward_loss, "reward_accuracy": reward_accuracy}