Skip to content

Commit 8418d92

Browse files
authored
QDT update (bug fix) (#453)
* added compile flag to CQL/IQL/DT configs. added actor_optim_factory setting to IQL config. added code for reverting reward scaling in relabel_dataset_rtg(). * bug fix in qdt.py (fit_iql) * remove unused import
1 parent c1603ce commit 8418d92

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

reproductions/offline/qdt.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import gym
66
import numpy as np
7-
from torch.optim.lr_scheduler import CosineAnnealingLR
87

98
import d3rlpy
109
from d3rlpy.algos import CQL, IQL
@@ -26,6 +25,7 @@ def main() -> None:
2625
parser.add_argument("--seed", type=int, default=1)
2726
parser.add_argument("--num_action_samples", type=int, default=10)
2827
parser.add_argument("--gpu", type=int)
28+
parser.add_argument("--compile", action="store_true")
2929
args = parser.parse_args()
3030

3131
dataset, env = d3rlpy.datasets.get_dataset(args.dataset)
@@ -54,13 +54,15 @@ def main() -> None:
5454
dataset=dataset,
5555
env=env,
5656
gpu=args.gpu,
57+
compile=args.compile,
5758
log_postfix=log_postfix,
5859
)
5960
elif args.q_learning_type == "iql":
6061
q_algo = fit_iql(
6162
dataset=dataset,
6263
env=env,
6364
gpu=args.gpu,
65+
compile=args.compile,
6466
log_postfix=log_postfix,
6567
)
6668
else:
@@ -82,6 +84,7 @@ def main() -> None:
8284
env=env,
8385
context_size=args.context_size,
8486
gpu=args.gpu,
87+
compile=args.compile,
8588
log_postfix=log_postfix,
8689
)
8790

@@ -115,8 +118,10 @@ def relabel_dataset_rtg(
115118
values = []
116119
for _ in range(num_action_samples):
117120
sampled_actions = q_algo.sample_action(episode.observations)
121+
v = q_algo.predict_value(episode.observations, sampled_actions)
118122
values.append(
119-
q_algo.predict_value(episode.observations, sampled_actions)
123+
v if q_algo.reward_scaler is None
124+
else q_algo.reward_scaler.reverse_transform(v)
120125
)
121126
value = np.array(values).mean(axis=0)
122127
rewards = np.squeeze(episode.rewards, axis=1)
@@ -149,6 +154,7 @@ def fit_cql(
149154
dataset: ReplayBuffer,
150155
env: gym.Env[NDArray, int],
151156
gpu: Optional[int],
157+
compile: bool,
152158
log_postfix: str,
153159
) -> CQL:
154160
"""
@@ -180,6 +186,7 @@ def fit_cql(
180186
n_action_samples=10,
181187
alpha_learning_rate=0.0,
182188
conservative_weight=conservative_weight,
189+
compile_graph=compile,
183190
).create(device=gpu)
184191

185192
cql.fit(
@@ -199,6 +206,7 @@ def fit_iql(
199206
dataset: ReplayBuffer,
200207
env: gym.Env[NDArray, int],
201208
gpu: Optional[int],
209+
compile: bool,
202210
log_postfix: str,
203211
) -> IQL:
204212
"""
@@ -221,31 +229,24 @@ def fit_iql(
221229
iql = d3rlpy.algos.IQLConfig(
222230
actor_learning_rate=3e-4,
223231
critic_learning_rate=3e-4,
232+
actor_optim_factory=d3rlpy.optimizers.AdamFactory(
233+
lr_scheduler_factory=d3rlpy.optimizers.CosineAnnealingLRFactory(
234+
T_max=500000
235+
),
236+
),
224237
batch_size=256,
225-
gamma=0.99,
226238
weight_temp=3.0,
227239
max_weight=100.0,
228240
expectile=0.7,
229241
reward_scaler=reward_scaler,
242+
compile_graph=compile,
230243
).create(device=gpu)
231244

232-
# workaround for learning scheduler
233-
iql.build_with_dataset(dataset)
234-
assert iql.impl
235-
scheduler = CosineAnnealingLR(
236-
iql.impl._modules.actor_optim, # pylint: disable=protected-access
237-
500000,
238-
)
239-
240-
def callback(algo: d3rlpy.algos.IQL, epoch: int, total_step: int) -> None:
241-
scheduler.step()
242-
243245
iql.fit(
244246
dataset,
245247
n_steps=500000,
246248
n_steps_per_epoch=1000,
247249
save_interval=10,
248-
callback=callback,
249250
evaluators={
250251
"environment": d3rlpy.metrics.EnvironmentEvaluator(env, n_trials=10)
251252
},
@@ -261,6 +262,7 @@ def fit_dt(
261262
env: gym.Env[NDArray, int],
262263
context_size: int,
263264
gpu: Optional[int],
265+
compile: bool,
264266
log_postfix: str,
265267
) -> None:
266268
"""
@@ -303,6 +305,7 @@ def fit_dt(
303305
num_heads=1,
304306
num_layers=3,
305307
max_timestep=1000,
308+
compile_graph=compile,
306309
).create(device=gpu)
307310

308311
dt.fit(

0 commit comments

Comments
 (0)