|
import pickle |
|
|
|
import datasets |
|
import numpy as np |
|
|
|
_DESCRIPTION = """\ |
|
A subset of the D4RL dataset, used for training Decision Transformers |
|
""" |
|
|
|
_HOMEPAGE = "https://github.com/rail-berkeley/d4rl" |
|
|
|
_LICENSE = "Apache-2.0" |
|
|
|
|
|
|
|
_BASE_URL = "https://huggingface.co/datasets/TobiTob/CityLearn/resolve/main" |
|
_URLS = { |
|
"halfcheetah-expert-v2": f"{_BASE_URL}/halfcheetah-expert-v2.pkl", |
|
"halfcheetah-medium-replay-v2": f"{_BASE_URL}/halfcheetah-medium-replay-v2.pkl", |
|
"test": f"{_BASE_URL}/test.pkl", |
|
"sequences": f"{_BASE_URL}/sequences.pkl", |
|
} |
|
|
|
|
|
class DecisionTransformerGymDataset(datasets.GeneratorBasedBuilder): |
|
"""The dataset comprises of tuples of (Observations, Actions, Rewards, Dones) sampled |
|
by an expert policy for various continuous control tasks (halfcheetah, hopper, walker2d)""" |
|
|
|
VERSION = datasets.Version("1.1.0") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name="halfcheetah-expert-v2", |
|
version=VERSION, |
|
description="Data sampled from an expert policy in the halfcheetah Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="halfcheetah-medium-replay-v2", |
|
version=VERSION, |
|
description="Data sampled from an medium policy in the halfcheetah Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="halfcheetah-medium-v2", |
|
version=VERSION, |
|
description="Data sampled from an medium policy in the halfcheetah Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="hopper-expert-v2", |
|
version=VERSION, |
|
description="Data sampled from an expert policy in the hopper Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="hopper-medium-replay-v2", |
|
version=VERSION, |
|
description="Data sampled from an medium policy in the hopper Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="hopper-medium-v2", |
|
version=VERSION, |
|
description="Data sampled from an medium policy in the hopper Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="walker2d-expert-v2", |
|
version=VERSION, |
|
description="Data sampled from an expert policy in the halfcheetah Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="walker2d-medium-replay-v2", |
|
version=VERSION, |
|
description="Data sampled from an medium policy in the halfcheetah Mujoco environment", |
|
), |
|
datasets.BuilderConfig( |
|
name="walker2d-medium-v2", |
|
version=VERSION, |
|
description="Data sampled from an medium policy in the halfcheetah Mujoco environment", |
|
), |
|
] |
|
|
|
def _info(self): |
|
|
|
features = datasets.Features( |
|
{ |
|
"observations": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))), |
|
"actions": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))), |
|
"rewards": datasets.Sequence(datasets.Value("float32")), |
|
"dones": datasets.Sequence(datasets.Value("bool")), |
|
|
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
|
|
features=features, |
|
|
|
|
|
|
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
urls = _URLS[self.config.name] |
|
data_dir = dl_manager.download_and_extract(urls) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"filepath": data_dir, |
|
"split": "train", |
|
}, |
|
) |
|
] |
|
|
|
|
|
def _generate_examples(self, filepath, split): |
|
with open(filepath, "rb") as f: |
|
trajectories = pickle.load(f) |
|
|
|
for idx, traj in enumerate(trajectories): |
|
yield idx, { |
|
"observations": traj["observations"], |
|
"actions": traj["actions"], |
|
"rewards": np.expand_dims(traj["rewards"], axis=1), |
|
"dones": np.expand_dims(traj.get("dones", traj.get("terminals")), axis=1), |
|
} |
|
|