ComplEx¶
- class pybind11_ke.module.model.ComplEx(*args: Any, **kwargs: Any)[源代码]¶
ComplEx[TWR+16] 提出于 2016 年,第一个真正意义上复数域模型,简单而且高效。复数版本的pybind11_ke.module.model.DistMult。评分函数为:
\[<\operatorname{Re}(h),\operatorname{Re}(r),\operatorname{Re}(t)> +<\operatorname{Re}(h),\operatorname{Im}(r),\operatorname{Im}(t)> +<\operatorname{Im}(h),\operatorname{Re}(r),\operatorname{Im}(t)> -<\operatorname{Im}(h),\operatorname{Im}(r),\operatorname{Re}(t)>\]\(h, r, t \in \mathbb{C}^n\) 是复数向量,\(< \mathbf{a}, \mathbf{b}, \mathbf{c} >=\sum_{i=1}^{n}a_ib_ic_i\) 为逐元素多线性点积(element-wise multi-linear dot product)。
正三元组的评分函数的值越大越好,负三元组越小越好,如果想获得更详细的信息请访问 ComplEx。
例子:
from pybind11_ke.config import Trainer, Tester from pybind11_ke.module.model import ComplEx from pybind11_ke.module.loss import SoftplusLoss from pybind11_ke.module.strategy import NegativeSampling # define the model complEx = ComplEx( ent_tol = train_dataloader.get_ent_tol(), rel_tol = train_dataloader.get_rel_tol(), dim = config.dim ) # define the loss function model = NegativeSampling( model = complEx, loss = SoftplusLoss(), batch_size = train_dataloader.get_batch_size(), regul_rate = config.regul_rate ) # test the model tester = Tester(model = complEx, 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)
- _calc(h: torch.Tensor, r: torch.Tensor, t: torch.Tensor) torch.Tensor[源代码]¶
计算 ComplEx 的评分函数。
- 参数:
h (torch.Tensor) – 头实体的向量。
r (torch.Tensor) – 关系的向量。
t (torch.Tensor) – 尾实体的向量。
- 返回:
三元组的得分
- 返回类型:
- ent_embeddings: torch.nn.Embedding¶
根据实体个数,创建的实体嵌入
- forward(triples: torch.Tensor, negs: torch.Tensor | None = None, mode: str = 'single') torch.Tensor[源代码]¶
定义每次调用时执行的计算。
torch.nn.Module子类必须重写torch.nn.Module.forward()。- 参数:
triples (str) – 正确的三元组
negs (torch.Tensor) – 负三元组类别
mode – 模式
- 返回:
三元组的得分
- 返回类型:
- 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]], mode) torch.Tensor[源代码]¶
ComplEx 的推理方法。
- 参数:
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¶
根据关系个数,创建的关系嵌入
- tri2emb(triples: torch.Tensor, negs: torch.Tensor | None = None, mode: str = 'single') tuple[torch.Tensor, torch.Tensor, torch.Tensor]¶
返回三元组对应的嵌入向量。
- 参数:
triples (str) – 正确的三元组
negs (torch.Tensor) – 负三元组类别
mode – 模式
- 返回:
头实体、关系和尾实体的嵌入向量
- 返回类型:
- zero_const: torch.nn.parameter.Parameter¶
常数 0