TransD¶
- class pybind11_ke.module.model.TransD(*args: Any, **kwargs: Any)[源代码]¶
TransD[JHX+15] 提出于 2015 年,自动生成映射矩阵,简单而且高效,是对 TransR 的改进。评分函数为:
\[\parallel (\mathbf{r}_p \mathbf{h}_p^T + \mathbf{I})\mathbf{h} + \mathbf{r} - (\mathbf{r}_p \mathbf{t}_p^T + \mathbf{I})\mathbf{t} \parallel_{L_1/L_2}\]正三元组的评分函数的值越小越好,如果想获得更详细的信息请访问 TransD。
例子:
from pybind11_ke.config import Trainer, Tester from pybind11_ke.module.model import TransD from pybind11_ke.module.loss import MarginLoss from pybind11_ke.module.strategy import NegativeSampling # define the model transd = TransD( ent_tol = train_dataloader.get_ent_tol(), rel_tol = train_dataloader.get_rel_tol(), dim_e = config.dim_e, dim_r = config.dim_r, p_norm = config.p_norm, norm_flag = config.norm_flag) # define the loss function model = NegativeSampling( model = transd, loss = MarginLoss(margin = config.margin), batch_size = train_dataloader.get_batch_size() ) # dataloader for test test_dataloader = TestDataLoader(in_path = config.in_path) # test the model tester = Tester(model = transd, 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()
- __init__(ent_tol: int, rel_tol: int, dim_e: int = 100, dim_r: int = 100, p_norm: int = 1, norm_flag: bool = True, margin: float | None = None)[源代码]¶
创建 TransD 对象。
- 参数:
ent_tol (int) – 实体的个数
rel_tol (int) – 关系的个数
dim_e (int) – 实体嵌入和实体投影向量的维度
dim_r (int) – 关系嵌入和关系投影向量的维度
p_norm (int) – 评分函数的距离函数, 按照原论文,这里可以取 1 或 2。
norm_flag (bool) – 是否利用
torch.nn.functional.normalize()对实体和关系嵌入的最后一维执行 L2-norm。margin (float) – 当使用
RotatE[SDNT19] 的损失函数pybind11_ke.module.loss.SigmoidLoss,需要提供此参数,将TransE[BUGD+13] 的正三元组的评分由越小越好转化为越大越好,如果想获得更详细的信息请访问 RotatE。
- __weakref__¶
list of weak references to the object (if defined)
- _calc(h: torch.Tensor, t: torch.Tensor, r: torch.Tensor, mode: str) torch.Tensor[源代码]¶
计算 TransD 的评分函数。
- 参数:
h (torch.Tensor) – 头实体的向量。
t (torch.Tensor) – 尾实体的向量。
r (torch.Tensor) – 关系的向量。
mode (str) –
normal表示pybind11_ke.data.TrainDataLoader为训练同时进行头实体和尾实体负采样的数据,head_batch和tail_batch表示为了减少数据传输成本,需要进行广播的数据,在广播前需要 reshape。
- 返回:
三元组的得分
- 返回类型:
- _resize(tensor: torch.Tensor, axis: int, size: int) torch.Tensor[源代码]¶
计算实体向量与单位矩阵的乘法,并返回结果向量。
源代码使用
torch.narrow()进行缩小向量,torch.nn.functional.pad()进行填充向量。- 参数:
tensor (torch.Tensor) – 实体向量。
axis (int) – 在哪个轴上进行乘法运算。
size (int) – 运算结果在
axis上的维度大小,一般为关系向量的维度。
- 返回:
乘法结果的向量
- 返回类型:
- _transfer(e: torch.Tensor, e_transfer: torch.Tensor, r_transfer: torch.Tensor) torch.Tensor[源代码]¶
将头实体或尾实体的向量映射到关系向量空间。
- 参数:
e (torch.Tensor) – 头实体或尾实体向量。
e_transfer (torch.Tensor) – 头实体或尾实体的投影向量
r_transfer (torch.Tensor) – 关系的投影向量
- 返回:
投影后的实体向量
- 返回类型:
- ent_embeddings: torch.nn.Embedding¶
根据实体个数,创建的实体嵌入
- ent_transfer: torch.nn.Embedding¶
根据实体个数,创建的实体投影向量
- forward(data: dict[str, Union[torch.Tensor, str]]) torch.Tensor[源代码]¶
定义每次调用时执行的计算。
torch.nn.Module子类必须重写torch.nn.Module.forward()。- 参数:
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]¶
获得模型权重。
- margin: torch.nn.parameter.Parameter¶
当使用
RotatE[SDNT19] 的损失函数pybind11_ke.module.loss.SigmoidLoss,需要提供此参数,将TransE[BUGD+13] 的正三元组的评分由越小越好转化为越大越好,如果想获得更详细的信息请访问 RotatE。
- norm_flag: bool¶
是否利用
torch.nn.functional.normalize()对实体和关系嵌入向量的最后一维执行 L2-norm。
- pi_const: torch.nn.parameter.Parameter¶
常数 pi
- predict(data: dict[str, Union[torch.Tensor, str]]) numpy.ndarray[源代码]¶
TransD 的推理方法。
- 参数:
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_transfer: torch.nn.Embedding¶
根据关系个数,创建的关系投影向量
- zero_const: torch.nn.parameter.Parameter¶
常数 0