torch_geometric.nn.models.MLP
- class MLP(channel_list: Optional[Union[int, List[int]]] = None, *, in_channels: Optional[int] = None, hidden_channels: Optional[int] = None, out_channels: Optional[int] = None, num_layers: Optional[int] = None, dropout: Union[float, List[float]] = 0.0, act: Optional[Union[str, Callable]] = 'relu', act_first: bool = False, act_kwargs: Optional[Dict[str, Any]] = None, norm: Optional[Union[str, Callable]] = 'batch_norm', norm_kwargs: Optional[Dict[str, Any]] = None, plain_last: bool = True, bias: Union[bool, List[bool]] = True, **kwargs)[source]
Bases:
Module一个多层感知器(MLP)模型。
存在两种实例化
MLP的方法:通过指定明确的通道大小,例如,
mlp = MLP([16, 32, 64, 128])
创建一个具有不同大小隐藏层的三层MLP。
通过在多个层中指定固定的隐藏通道大小,例如,
mlp = MLP(in_channels=16, hidden_channels=32, out_channels=128, num_layers=3)
创建一个具有相同大小隐藏层的三层MLP。
- Parameters:
channel_list (List[int] 或 int, 可选) – 输入、中间和输出通道的列表,使得
len(channel_list) - 1表示MLP的层数(默认值:None)in_channels (int, 可选) – 每个输入样本的大小。 将覆盖
channel_list。(默认值:None)hidden_channels (int, optional) – 每个隐藏样本的大小。 将覆盖
channel_list。(默认值:None)out_channels (int, optional) – 每个输出样本的大小。 将覆盖
channel_list。(默认值:None)num_layers (int, optional) – 层数。 将覆盖
channel_list。(默认值:None)dropout (float 或 List[float], 可选) – 每个隐藏嵌入的丢弃概率。如果提供了列表,则为每层设置丢弃值。(默认值:
0.)act (str or Callable, optional) – The non-linear activation function to use. (default:
"relu")act_first (bool, optional) – If set to
True, activation is applied before normalization. (default:False)act_kwargs (Dict[str, Any], optional) – Arguments passed to the respective activation function defined by
act. (default:None)norm (str or Callable, optional) – The normalization function to use. (default:
"batch_norm")norm_kwargs (Dict[str, Any], optional) – Arguments passed to the respective normalization function defined by
norm. (default:None)plain_last (bool, 可选) – 如果设置为
False,将对最后一层应用非线性、批量归一化和dropout。(默认值:True)bias (bool 或 List[bool], 可选) – 如果设置为
False,模块 将不会学习加性偏差。如果提供了一个列表,则为 每一层设置偏差。(默认值:True)**kwargs (可选) – MLP层的额外已弃用参数。
- forward(x: Tensor, batch: Optional[Tensor] = None, batch_size: Optional[int] = None, return_emb: Optional[Tensor] = None) Tensor[source]
前向传播。
- Parameters:
x (torch.Tensor) – The source tensor.
batch (torch.Tensor, optional) – The batch vector \(\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N\), which assigns each element to a specific example. Only needs to be passed in case the underlying normalization layers require the
batchinformation. (default:None)batch_size (int, optional) – The number of examples \(B\). Automatically calculated if not given. Only needs to be passed in case the underlying normalization layers require the
batchinformation. (default:None)return_emb (bool, 可选) – 如果设置为
True,将 额外返回在执行最终输出层之前的嵌入。(默认值:False)
- Return type: