DecisionTransformer¶
- class torchrl.modules.DecisionTransformer(state_dim, action_dim, config: dict | DTConfig = None)[source]¶
在线决策 Transformer。
在 https://arxiv.org/abs/2202.05607 中描述。
如果用户没有提供特定的配置,则 Transformer 将使用默认配置来创建 GPT2 模型。default_config = { … “n_embd”: 256, … “n_layer”: 4, … “n_head”: 4, … “n_inner”: 1024, … “activation”: “relu”, … “n_positions”: 1024, … “resid_pdrop”: 0.1, … “attn_pdrop”: 0.1, }
- 参数:
state_dim (int) – 状态空间的维度
action_dim (int) – 动作空间的维度
config (
DTConfig
或 dict,可选) – Transformer 架构配置,用于从 transformers 创建 GPT2Config。默认值为default_config
.
示例
>>> config = DecisionTransformer.default_config() >>> config.n_embd = 128 >>> print(config) DTConfig(n_embd: 128, n_layer: 4, n_head: 4, n_inner: 1024, activation: relu, n_positions: 1024, resid_pdrop: 0.1, attn_pdrop: 0.1) >>> # alternatively >>> config = DecisionTransformer.DTConfig(n_embd=128) >>> model = DecisionTransformer(state_dim=4, action_dim=2, config=config) >>> batch_size = [3, 32] >>> length = 10 >>> observation = torch.randn(*batch_size, length, 4) >>> action = torch.randn(*batch_size, length, 2) >>> return_to_go = torch.randn(*batch_size, length, 1) >>> output = model(observation, action, return_to_go) >>> output.shape torch.Size([3, 32, 10, 128])