门控组合
- class GatedCombination(entity_dim: int = 32, literal_dim: int | None = None, input_dropout: float = 0.0, gate_activation: str | ~torch.nn.modules.module.Module | type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.activation.Sigmoid'>, gate_activation_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None, hidden_activation: str | ~torch.nn.modules.module.Module | type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.activation.Tanh'>, hidden_activation_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None)[source]
基础类:
Combination一个实现门控线性变换的模块,用于实体和字面量的组合。
与其他组合相比,这种组合利用了RNN中常见的门控机制。该门控机制的主要目标是学习附加文字信息中哪些部分是有用的,哪些部分是无用的,并通过将它们纳入新的组合嵌入或丢弃它们来相应地采取行动。
对于给定的实体表示 \(\mathbf{x}_e \in \mathbb{R}^{d_e}\) 和字面表示 \(\mathbf{x}_l \in \mathbb{R}^{d_l}\),该模块计算
\[z = f_{gate}(\mathbf{W}_e x_e + \mathbf{W}_l x_l + \mathbf{b}) h = f_{hidden}(\mathbf{W} [x_e; x_l]) y = Dropout(z \odot h + (1 - z) \odot x)\]其中 \(\mathbf{W}_e \in \mathbb{R}^{d_e \times d_e}\), :math:mathbf{W}_l in mathbb{R}^{d_l times d_e}, \(\mathbf{W} \in \mathbb{R}^{(d_e + d_l) \ times d_e}\), 和 \(\mathbf{b} \in \mathbb{R}^{d_e}\) 是可训练的参数, \(f_{gate}\) 和 \(f_{hidden}\) 是激活函数,默认为 sigmoid 和 tanh,\(\odot\) 表示 元素乘法,\([x_e; x_l]\) 表示连接操作。
注意
我们也可以用另一种方式来表达这个门
\[z = f_{gate}(\mathbf{W}_e x_e + \mathbf{W}_l x_l + \mathbf{b})\]作为
\[z = f_{gate}(\mathbf{W}_{el} [x_e; x_l] + \mathbf{b})\]使用 \(\mathbf{W}_{el} \in \mathbb{R}^{(d_e + d_l) \times d_e}\).
实现基于https://github.com/SmartDataAnalytics/LiteralE/blob/master/model.py Gate类。
实例化模块。
- Parameters:
entity_dim (int) – 实体表示的维度。
literal_dim (int | None) – 字面量的维度;默认为entity_dim
input_dropout (float) – 使用的dropout
gate_activation (str | Module | type[Module] | None) – 用于门的激活函数,或其提示
gate_activation_kwargs (Mapping[str, Any] | None) – 如果给定的是类或名称而不是预先实例化的激活模块,则用于实例化gate_activation的关键字参数
hidden_activation (str | Module | type[Module] | None) – 在隐藏层中使用的激活函数,或其提示
hidden_activation_kwargs (Mapping[str, Any] | None) – 如果给定的是类或名称而不是预先实例化的激活模块,则用于实例化隐藏激活的关键字参数
方法总结
forward(xs)组合一系列单独的表示。
方法文档