RESCAL¶
- class pybind11_ke.module.model.RESCAL(*args: Any, **kwargs: Any)[源代码]¶
RESCAL[NTK11] 提出于 2011 年,是很多张量分解模型的基石,模型较复杂。评分函数为:
\[-\mathbf{h}^T \mathbf{M}_r \mathbf{t}\]正三元组的评分函数的值越小越好,如果想获得更详细的信息请访问 RESCAL。
例子:
from pybind11_ke.utils import WandbLogger from pybind11_ke.data import KGEDataLoader, BernSampler, TradTestSampler from pybind11_ke.module.model import RESCAL from pybind11_ke.module.loss import MarginLoss from pybind11_ke.module.strategy import NegativeSampling from pybind11_ke.config import Trainer, Tester wandb_logger = WandbLogger( project="pybind11-ke", name="RESCAL-FB15K237", config=dict( in_path = '../../benchmarks/FB15K237/', batch_size = 2048, neg_ent = 25, test = True, test_batch_size = 10, num_workers = 16, dim = 50, margin = 1.0, use_gpu = True, device = 'cuda:0', epochs = 1000, lr = 0.1, opt_method = 'adagrad', valid_interval = 100, log_interval = 100, save_interval = 100, save_path = '../../checkpoint/rescal.pth' ) ) config = wandb_logger.config # dataloader for training dataloader = KGEDataLoader( in_path = config.in_path, batch_size = config.batch_size, neg_ent = config.neg_ent, test = config.test, test_batch_size = config.test_batch_size, num_workers = config.num_workers, train_sampler = BernSampler, test_sampler = TradTestSampler ) # define the model rescal = RESCAL( ent_tol = dataloader.get_ent_tol(), rel_tol = dataloader.get_rel_tol(), dim = config.dim ) # define the loss function model = NegativeSampling( model = rescal, loss = MarginLoss(margin = config.margin) ) # test the model tester = Tester(model = rescal, data_loader = dataloader, use_gpu = config.use_gpu, device = config.device) # train the model trainer = Trainer(model = model, data_loader = dataloader.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[源代码]¶
计算 RESCAL 的评分函数。
- 参数:
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[源代码]¶
RESCAL 的推理方法。
- 参数:
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_matrices: torch.nn.Embedding¶
根据关系个数,创建的关系矩阵
- tri2emb(triples: torch.Tensor, negs: torch.Tensor | None = None, mode: str = 'single') tuple[torch.Tensor, torch.Tensor][源代码]¶
返回三元组对应的嵌入向量。
- 参数:
triples (str) – 正确的三元组
negs (torch.Tensor) – 负三元组类别
mode – 模式
- 返回:
头实体和尾实体的嵌入向量
- 返回类型:
- zero_const: torch.nn.parameter.Parameter¶
常数 0