SimplE¶
- class pybind11_ke.module.model.SimplE(*args: Any, **kwargs: Any)[源代码]¶
SimplE[KP18] 提出于 2018 年,简单的双线性模型,能够为头实体和尾实体学习不同的嵌入向量。评分函数为:
\[1/2(<\mathbf{h}_{i}, \mathbf{v}_r, \mathbf{t}_{j}> + <\mathbf{h}_{j}, \mathbf{v}_{r^{-1}}, \mathbf{t}_{i}>)\]\(< \mathbf{a}, \mathbf{b}, \mathbf{c} >\) 为逐元素多线性点积(element-wise multi-linear dot product)。
正三元组的评分函数的值越大越好,负三元组越小越好,如果想获得更详细的信息请访问 SimplE。
例子:
from pybind11_ke.config import Trainer, Tester from pybind11_ke.module.model import SimplE from pybind11_ke.module.loss import SoftplusLoss from pybind11_ke.module.strategy import NegativeSampling # define the model simple = SimplE( ent_tol = train_dataloader.get_ent_tol(), rel_tol = train_dataloader.get_rel_tol(), dim = config.dim ) # define the loss function model = NegativeSampling( model = simple, loss = SoftplusLoss(), batch_size = train_dataloader.get_batch_size(), regul_rate = config.regul_rate ) # dataloader for test test_dataloader = TestDataLoader(in_path = config.in_path) # test the model tester = Tester(model = simple, data_loader = test_dataloader, use_gpu = config.use_gpu, device = config.device) # train the model trainer = Trainer(model = model, data_loader = train_dataloader, epochs = config.epochs, lr = config.lr, opt_method = config.opt_method, use_gpu = config.use_gpu, device = config.device, tester = tester, test = config.test, valid_interval = config.valid_interval, log_interval = config.log_interval, save_interval = config.save_interval, save_path = config.save_path, use_wandb = True) trainer.run()
- __weakref__¶
list of weak references to the object (if defined)
- ent_h_embeddings: torch.nn.Embedding¶
根据实体个数,创建的头实体嵌入
- ent_t_embeddings: torch.nn.Embedding¶
根据实体个数,创建的尾实体嵌入
- forward(data: dict[str, Union[torch.Tensor, str]]) torch.Tensor[源代码]¶
定义每次调用时执行的计算。
torch.nn.Module子类必须重写torch.nn.Module.forward()。 利用torch.clamp()裁剪最后的的得分,防止遇到 NaN 问题。- 参数:
data (dict[str, Union[torch.Tensor, str]]) – 数据。
- 返回:
三元组的得分
- 返回类型:
- get_parameters(mode: str = 'numpy', param_dict: dict[str, Any] | None = None) dict[str, numpy.ndarray] | dict[str, list] | dict[str, torch.Tensor]¶
获得模型权重。
- pi_const: torch.nn.parameter.Parameter¶
常数 pi
- predict(data: dict[str, Union[torch.Tensor, str]]) numpy.ndarray[源代码]¶
SimplE 的推理方法。
- 参数:
data (dict[str, Union[torch.Tensor,str]]) – 数据。
- 返回:
三元组的得分
- 返回类型:
- regularization(data: dict[str, Union[torch.Tensor, str]]) torch.Tensor[源代码]¶
L2 正则化函数(又称权重衰减),在损失函数中用到。
- 参数:
data (dict[str, Union[torch.Tensor, str]]) – 数据。
- 返回:
模型参数的正则损失
- 返回类型:
- rel_embeddings: torch.nn.Embedding¶
根据关系个数,创建的关系嵌入
- rel_inv_embeddings: torch.nn.Embedding¶
根据关系个数,创建的逆关系嵌入
- zero_const: torch.nn.parameter.Parameter¶
常数 0