CompGCNSampling¶
- class unike.module.strategy.CompGCNSampling(*args: Any, **kwargs: Any)[源代码]¶
将模型和损失函数封装到一起,方便模型训练,用于
CompGCN[VSNT20]。例子:
from unike.module.model import CompGCN from unike.module.loss import CompGCNLoss from unike.module.strategy import CompGCNSampling from unike.config import Trainer, GraphTester # define the model compgcn = CompGCN( ent_tol = dataloader.train_sampler.ent_tol, rel_tol = dataloader.train_sampler.rel_tol, dim = 100 ) # define the loss function model = CompGCNSampling( model = compgcn, loss = CompGCNLoss(model = compgcn), ent_tol = dataloader.train_sampler.ent_tol ) # test the model tester = GraphTester(model = compgcn, data_loader = dataloader, use_gpu = True, device = 'cuda:0', prediction = "tail") # train the model trainer = Trainer(model = model, data_loader = dataloader.train_dataloader(), epochs = 2000, lr = 0.0001, use_gpu = True, device = 'cuda:0', tester = tester, test = True, valid_interval = 50, log_interval = 50, save_interval = 50, save_path = '../../checkpoint/compgcn.pth' ) trainer.run()
- __call__(*args: Any, **kwargs: Any) Any¶
Call self as a function.
- __init__(model: CompGCN | None = None, loss: Loss | None = None, smoothing: float = 0.1, ent_tol: int | None = None)[源代码]¶
创建 CompGCNSampling 对象。
- 参数:
model (
unike.module.model.CompGCN) – CompGCN 模型loss (
unike.module.loss.Loss) – 损失函数。smoothing (float) – smoothing
ent_tol (int) – 实体个数
- static __new__(cls, *args: Any, **kwargs: Any) Any¶
- __repr__() str¶
Return repr(self).
- __weakref__¶
list of weak references to the object (if defined)
- ent_tol: int¶
实体个数
- 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]]) – 数据
- 返回:
损失值
- 返回类型:
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]¶
获得模型权重。
- 参数:
mode – 模型保存的格式,可以选择
numpy、list和Tensor。param_dict (dict[str, Any] | None) – 可以选择从哪里获得模型权重。
- 返回:
模型权重字典。
- 返回类型:
dict[str, numpy.ndarray] | dict[str, list] | dict[str, torch.Tensor]
- load_checkpoint(path: str)¶
加载模型权重。
- 参数:
path (str) – 模型保存的路径
- load_parameters(path: str)¶
加载模型权重。
- 参数:
path (str) – 模型保存的路径
- loss: Loss¶
损失函数,即
unike.module.loss.Loss
- model: CompGCN¶
CompGCN 模型,即
unike.module.model.CompGCN
- pi_const: torch.nn.parameter.Parameter¶
常数 pi
- save_checkpoint(path: str)¶
保存模型权重。
- 参数:
path (str) – 模型保存的路径
- save_parameters(path: str)¶
用 json 格式保存模型权重。
- 参数:
path (str) – 模型保存的路径
- set_parameters(parameters: dict[str, Any])¶
加载模型权重。
- 参数:
parameters (dict[str, Any]) – 模型权重字典。
- smoothing: float¶
smoothing
- zero_const: torch.nn.parameter.Parameter¶
常数 0