File size: 6,462 Bytes
484ef92 |
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 |
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"
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_BASE_URL = "https://huggingface.co/datasets/edbeeching/decision_transformer_gym_replay/resolve/main/data"
_URLS = {
"halfcheetah-expert-v2": f"{_BASE_URL}/halfcheetah-expert-v2.pkl",
"halfcheetah-medium-replay-v2": f"{_BASE_URL}/halfcheetah-medium-replay-v2.pkl",
"halfcheetah-medium-v2": f"{_BASE_URL}/halfcheetah-medium-v2.pkl",
"hopper-expert-v2": f"{_BASE_URL}/hopper-expert-v2.pkl",
"hopper-medium-replay-v2": f"{_BASE_URL}/hopper-medium-replay-v2.pkl",
"hopper-medium-v2": f"{_BASE_URL}/hopper-medium-v2.pkl",
"walker2d-expert-v2": f"{_BASE_URL}/walker2d-expert-v2.pkl",
"walker2d-medium-replay-v2": f"{_BASE_URL}/walker2d-medium-replay-v2.pkl",
"walker2d-medium-v2": f"{_BASE_URL}/walker2d-medium-v2.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")
# This is an example of a dataset with multiple configurations.
# If you don't want/need to define several sub-sets in your dataset,
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
# If you need to make complex sub-parts in the datasets with configurable options
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
# BUILDER_CONFIG_CLASS = MyBuilderConfig
# You will be able to load one or the other configurations in the following list with
# data = datasets.load_dataset('my_dataset', 'first_domain')
# data = datasets.load_dataset('my_dataset', 'second_domain')
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")),
# These are the features of your dataset like images, labels ...
}
)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
# Here we define them above because they are different between the two configurations
features=features,
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
# supervised_keys=("sentence", "label"),
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
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,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_dir,
"split": "train",
},
)
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
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),
}
|