Shortcuts

NegativeSampling

class unike.module.strategy.NegativeSampling(*args: Any, **kwargs: Any)[源代码]

将模型和损失函数封装到一起,方便模型训练。

例子:

from unike.module.model import TransE
from unike.module.loss import MarginLoss
from unike.module.strategy import NegativeSampling

# define the model
transe = TransE(
        ent_tol = dataloader.get_ent_tol(),
        rel_tol = dataloader.get_rel_tol(),
        dim = 50, 
        p_norm = 1, 
        norm_flag = True
)

# define the loss function
model = NegativeSampling(
        model = transe, 
        loss = MarginLoss(margin = 1.0),
        regul_rate = 0.01
)
__call__(*args: Any, **kwargs: Any) Any

Call self as a function.

__init__(model: Model | None = None, loss: Loss | None = None, regul_rate: float = 0.0, l3_regul_rate: float = 0.0)[源代码]

创建 NegativeSampling 对象。

参数:
static __new__(cls, *args: Any, **kwargs: Any) Any
__repr__() str

Return repr(self).

__weakref__

list of weak references to the object (if defined)

forward(data: dict[str, Union[torch.Tensor, str]]) torch.Tensor[源代码]

计算最后的损失值。定义每次调用时执行的计算。 torch.nn.Module 子类必须重写 torch.nn.Module.forward()

参数:

data (dict[str, Union[torch.Tensor, str]]) – 数据

返回:

损失值

返回类型:

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 – 模型保存的格式,可以选择 numpylistTensor

  • param_dict (dict[str, Any] | None) – 可以选择从哪里获得模型权重。

返回:

模型权重字典。

返回类型:

dict[str, numpy.ndarray] | dict[str, list] | dict[str, torch.Tensor]

l3_regul_rate: float

l3 正则化系数

load_checkpoint(path: str)

加载模型权重。

参数:

path (str) – 模型保存的路径

load_parameters(path: str)

加载模型权重。

参数:

path (str) – 模型保存的路径

loss: Loss

损失函数,即 unike.module.loss.Loss

model: Model

KGE 模型,即 unike.module.model.Model

pi_const: torch.nn.parameter.Parameter

常数 pi

regul_rate: float

权重衰减系数

save_checkpoint(path: str)

保存模型权重。

参数:

path (str) – 模型保存的路径

save_parameters(path: str)

用 json 格式保存模型权重。

参数:

path (str) – 模型保存的路径

set_parameters(parameters: dict[str, Any])

加载模型权重。

参数:

parameters (dict[str, Any]) – 模型权重字典。

zero_const: torch.nn.parameter.Parameter

常数 0

Docs

Access comprehensive developer documentation for UniKE

View Docs