sd_rescal: add auto initialization
+import math
+
import torch
from kge import Config, Dataset
from kge.model.kge_model import KgeModel, RelationalScorer
@@ -217,6 +219,36 @@ class SparseDiagonalRescal(KgeModel):
config.set(ent_emb_conf_key + ".dim", blocks*block_size, log=True)
config.set(rel_emb_conf_key + ".dim", blocks**2*block_size, log=True)
+ # auto initialize such that scores have unit variance
+ if self.get_option("auto_initialization"):
+ # Var[score] = blocks^2*block_size*var_e^2*var_r, where var_e/var_r are the variances
+ # of the entries
+ #
+ # Thus we set var_e=var_r=(1.0/(blocks^2*block_size))^(1/6)
+ std = math.pow(1.0 / (blocks**2*block_size), 1.0 / 6.0)
+
+ config.set(
+ self.configuration_key + ".entity_embedder.initialize",
+ "normal_",
+ log=True,
+ )
+ config.set(
+ self.configuration_key + ".entity_embedder.initialize_args",
+ {"mean": 0.0, "std": std},
+ log=True,
+ )
+ config.set(
+ self.configuration_key + ".relation_embedder.initialize",
+ "normal_",
+ log=True,
+ )
+ config.set(
+ self.configuration_key + ".relation_embedder.initialize_args",
+ {"mean": 0.0, "std": std},
+ log=True,
+ )
+
+
super().__init__(
config,
dataset,
