RGCNSampling¶
- class pybind11_ke.module.strategy.RGCNSampling(*args: Any, **kwargs: Any)[源代码]¶
将模型和损失函数封装到一起,方便模型训练,用于
R-GCN[SKB+18]。例子:
from pybind11_ke.data import GraphDataLoader from pybind11_ke.module.model import RGCN from pybind11_ke.module.loss import RGCNLoss from pybind11_ke.module.strategy import RGCNSampling from pybind11_ke.config import Trainer, GraphTester dataloader = GraphDataLoader( in_path = "../../benchmarks/FB15K237/", batch_size = 60000, neg_ent = 10, test = True, test_batch_size = 100, num_workers = 16 ) # define the model rgcn = RGCN( ent_tol = dataloader.train_sampler.ent_tol, rel_tol = dataloader.train_sampler.rel_tol, dim = 500, num_layers = 2 ) # define the loss function model = RGCNSampling( model = rgcn, loss = RGCNLoss(model = rgcn, regularization = 1e-5) ) # test the model tester = GraphTester(model = rgcn, data_loader = dataloader, use_gpu = True, device = 'cuda:0') # train the model trainer = Trainer(model = model, data_loader = dataloader.train_dataloader(), epochs = 10000, lr = 0.0001, use_gpu = True, device = 'cuda:0', tester = tester, test = True, valid_interval = 500, log_interval = 500, save_interval = 500, save_path = '../../checkpoint/rgcn.pth' ) trainer.run()
- __init__(model: Model | None = None, loss: Loss | None = None)[源代码]¶
创建 RGCNSampling 对象。
- 参数:
model (
pybind11_ke.module.model.RGCN) – R-GCN 模型loss (
pybind11_ke.module.loss.Loss) – 损失函数。
- __weakref__¶
list of weak references to the object (if defined)
- forward(data: dict[str, Union[dgl.DGLGraph, torch.Tensor]]) torch.Tensor[源代码]¶
计算最后的损失值。定义每次调用时执行的计算。
torch.nn.Module子类必须重写torch.nn.Module.forward()。- 参数:
data (dict[str, Union[dgl.DGLGraph, torch.Tensor]]) – 数据
- 返回:
损失值
- 返回类型:
- get_parameters(mode: str = 'numpy', param_dict: dict[str, Any] | None = None) dict[str, numpy.ndarray] | dict[str, list] | dict[str, torch.Tensor]¶
获得模型权重。
- loss: Loss¶
损失函数,即
pybind11_ke.module.loss.Loss
- model: Model¶
R-GCN 模型,即
pybind11_ke.module.model.RGCN
- pi_const: torch.nn.parameter.Parameter¶
常数 pi
- zero_const: torch.nn.parameter.Parameter¶
常数 0