ding.model.template.procedure_cloning¶
ding.model.template.procedure_cloning
¶
PCTransformer
¶
Bases: Module
Overview
The transformer block for neural network of algorithms related to Procedure cloning (PC).
Interfaces:
__init__, forward.
__init__(cnn_hidden, att_hidden, att_heads, drop_p, max_T, n_att, feedforward_hidden, n_feedforward)
¶
Overview
Initialize the procedure cloning transformer model according to corresponding input arguments.
Arguments:
- cnn_hidden (:obj:int): The last channel dimension of CNN encoder, such as 32.
- att_hidden (:obj:int): The dimension of attention blocks, such as 32.
- att_heads (:obj:int): The number of heads in attention blocks, such as 4.
- drop_p (:obj:float): The drop out rate of attention, such as 0.5.
- max_T (:obj:int): The sequence length of procedure cloning, such as 4.
- n_attn (:obj:int): The number of attention layers, such as 4.
- feedforward_hidden (:obj:int):The dimension of feedforward layers, such as 32.
- n_feedforward (:obj:int): The number of feedforward layers, such as 4.
forward(x)
¶
Overview
The unique execution (forward) method of PCTransformer.
Arguments:
- x (:obj:torch.Tensor): Sequential data of several hidden states.
Returns:
- output (:obj:torch.Tensor): A tensor with the same shape as the input.
Examples:
>>> model = PCTransformer(128, 128, 8, 0, 16, 2, 128, 2)
>>> h = torch.randn((2, 16, 128))
>>> h = model(h)
>>> assert h.shape == torch.Size([2, 16, 128])
ProcedureCloningMCTS
¶
Bases: Module
Overview
The neural network of algorithms related to Procedure cloning (PC).
Interfaces:
__init__, forward.
__init__(obs_shape, action_dim, cnn_hidden_list=[128, 128, 256, 256, 256], cnn_activation=nn.ReLU(), cnn_kernel_size=[3, 3, 3, 3, 3], cnn_stride=[1, 1, 1, 1, 1], cnn_padding=[1, 1, 1, 1, 1], mlp_hidden_list=[256, 256], mlp_activation=nn.ReLU(), att_heads=8, att_hidden=128, n_att=4, n_feedforward=2, feedforward_hidden=256, drop_p=0.5, max_T=17)
¶
Overview
Initialize the MCTS procedure cloning model according to corresponding input arguments.
Arguments:
- obs_shape (:obj:SequenceType): Observation space shape, such as [4, 84, 84].
- action_dim (:obj:int): Action space shape, such as 6.
- cnn_hidden_list (:obj:SequenceType): The cnn channel dims for each block, such as [128, 128, 256, 256, 256].
- cnn_activation (:obj:nn.Module): The activation function for cnn blocks, such as nn.ReLU().
- cnn_kernel_size (:obj:SequenceType): The kernel size for each cnn block, such as [3, 3, 3, 3, 3].
- cnn_stride (:obj:SequenceType): The stride for each cnn block, such as [1, 1, 1, 1, 1].
- cnn_padding (:obj:SequenceType): The padding for each cnn block, such as [1, 1, 1, 1, 1].
- mlp_hidden_list (:obj:SequenceType): The last dim for this must match the last dim of cnn_hidden_list, such as [256, 256].
- mlp_activation (:obj:nn.Module): The activation function for mlp layers, such as nn.ReLU().
- att_heads (:obj:int): The number of attention heads in transformer, such as 8.
- att_hidden (:obj:int): The number of attention dimension in transformer, such as 128.
- n_att (:obj:int): The number of attention blocks in transformer, such as 4.
- n_feedforward (:obj:int): The number of feedforward layers in transformer, such as 2.
- drop_p (:obj:float): The drop out rate of attention, such as 0.5.
- max_T (:obj:int): The sequence length of procedure cloning, such as 17.
forward(states, goals, actions)
¶
Overview
ProcedureCloningMCTS forward computation graph, input states tensor and goals tensor, calculate the predicted states and actions.
Arguments:
- states (:obj:torch.Tensor): The observation of current time.
- goals (:obj:torch.Tensor): The target observation after a period.
- actions (:obj:torch.Tensor): The actions executed during the period.
Returns:
- outputs (:obj:Tuple[torch.Tensor, torch.Tensor]): Predicted states and actions.
Examples:
>>> inputs = { 'states': torch.randn(2, 3, 64, 64), 'goals': torch.randn(2, 3, 64, 64), 'actions': torch.randn(2, 15, 9) }
>>> model = ProcedureCloningMCTS(obs_shape=(3, 64, 64), action_dim=9)
>>> goal_preds, action_preds = model(inputs['states'], inputs['goals'], inputs['actions'])
>>> assert goal_preds.shape == (2, 256)
>>> assert action_preds.shape == (2, 16, 9)
BFSConvEncoder
¶
Bases: Module
Overview
The BFSConvolution Encoder used to encode raw 3-dim observations. And output a feature map with the
same height and width as input. Interfaces: __init__, forward.
__init__(obs_shape, hidden_size_list=[32, 64, 64, 128], activation=nn.ReLU(), kernel_size=[8, 4, 3], stride=[4, 2, 1], padding=None)
¶
Overview
Init the BFSConvolution Encoder according to the provided arguments.
Arguments:
- obs_shape (:obj:SequenceType): Sequence of in_channel, plus one or more input size.
- hidden_size_list (:obj:SequenceType): Sequence of hidden_size of subsequent conv layers and the final dense layer.
- activation (:obj:nn.Module): Type of activation to use in the conv layers and ResBlock. Default is nn.ReLU().
- kernel_size (:obj:SequenceType): Sequence of kernel_size of subsequent conv layers.
- stride (:obj:SequenceType): Sequence of stride of subsequent conv layers.
- padding (:obj:SequenceType): Padding added to all four sides of the input for each conv layer. See nn.Conv2d for more details. Default is None.
forward(x)
¶
Overview
Return output tensor of the env observation.
Arguments:
- x (:obj:torch.Tensor): Env raw observation.
Returns:
- outputs (:obj:torch.Tensor): Output embedding tensor.
Examples:
>>> model = BFSConvEncoder([3, 16, 16], [32, 32, 4], kernel_size=[3, 3, 3], stride=[1, 1, 1] , padding=[1, 1, 1])
>>> inputs = torch.randn(3, 16, 16).unsqueeze(0)
>>> outputs = model(inputs)
>>> assert outputs['logit'].shape == torch.Size([4, 16, 16])
ProcedureCloningBFS
¶
Bases: Module
Overview
The neural network introduced in procedure cloning (PC) to process 3-dim observations. Given an input, this model will perform several 3x3 convolutions and output a feature map with the same height and width of input. The channel number of output will be the action_shape.
Interfaces:
__init__, forward.
__init__(obs_shape, action_shape, encoder_hidden_size_list=[128, 128, 256, 256])
¶
Overview
Init the BFSConvolution Encoder according to the provided arguments.
Arguments:
- obs_shape (:obj:SequenceType): Sequence of in_channel, plus one or more input size, such as [4, 84, 84].
- action_dim (:obj:int): Action space shape, such as 6.
- cnn_hidden_list (:obj:SequenceType): The cnn channel dims for each block, such as [128, 128, 256, 256].
forward(x)
¶
Overview
The computation graph. Given a 3-dim observation, this function will return a tensor with the same height and width. The channel number of output will be the action_shape.
Arguments:
- x (:obj:torch.Tensor): The input observation tensor data.
Returns:
- outputs (:obj:Dict): The output dict of model's forward computation graph, only contains a single key logit.
Examples:
>>> model = ProcedureCloningBFS([3, 16, 16], 4)
>>> inputs = torch.randn(16, 16, 3).unsqueeze(0)
>>> outputs = model(inputs)
>>> assert outputs['logit'].shape == torch.Size([16, 16, 4])
Full Source Code
../ding/model/template/procedure_cloning.py