Small changes to ConvE (still broken)
@@ -4,6 +4,7 @@ from kge import Config, Dataset
from kge.model.kge_model import RelationalScorer, KgeModel
from collections import OrderedDict
+
class ConvEScorer(RelationalScorer):
r"""Implementation of the ConvE KGE scorer.
@@ -22,15 +23,20 @@ class ConvEScorer(RelationalScorer):
self.stride = config.get("conve.stride")
self.padding = config.get("conve.padding")
- self.feature_map_dropout = torch.nn.Dropout(config.get("conve.feature_map_dropout"))
+ # TODO remove input dropout, just here for testing
+ # We should use the dropout from the lookup embedders
+ # Also remove bn0 layer, should be in lookup embedder if it makes sense
+ self.input_dropout = torch.nn.Dropout(0.2)
+ self.bn0 = torch.nn.BatchNorm2d(1, affine=False)
+
+ self.feature_map_dropout = torch.nn.Dropout2d(config.get("conve.feature_map_dropout"))
self.projection_dropout = torch.nn.Dropout(config.get("conve.projection_dropout"))
self.convolution = torch.nn.Conv2d(in_channels=1, out_channels=32,
kernel_size=(self.filter_size, self.filter_size),
stride=self.stride, padding=self.padding,
bias=config.get("conve.convolution_bias"))
- self.bn0 = torch.nn.BatchNorm2d(1)
- self.bn1 = torch.nn.BatchNorm2d(32)
- self.bn2 = torch.nn.BatchNorm1d(self.emb_dim)
+ self.bn1 = torch.nn.BatchNorm2d(32, affine=False)
+ self.bn2 = torch.nn.BatchNorm1d(self.emb_dim, affine=False)
conv_output_height = (((self.emb_height * 2) - self.filter_size + (2 * self.padding))/self.stride) + 1
conv_output_width = ((self.emb_width - self.filter_size + (2 * self.padding))/self.stride) + 1
self.projection = torch.nn.Linear(32 * int(conv_output_height * conv_output_width), int(self.emb_dim))
@@ -39,15 +45,21 @@ class ConvEScorer(RelationalScorer):
self.sigmoid = torch.nn.Sigmoid()
def score_emb(self, s_emb, p_emb, o_emb, combine: str):
- n = p_emb.size(0)
+ batch_size = p_emb.size(0)
s_emb_2d = s_emb.view(-1, 1, int(self.emb_height), int(self.emb_width))
p_emb_2d = p_emb.view(-1, 1, int(self.emb_height), int(self.emb_width))
stacked_inputs = torch.cat([s_emb_2d, p_emb_2d], 2)
+
+ # TODO remove input dropout, just here for testing
+ # We should use the dropout from the lookup embedders
+ stacked_inputs = self.bn0(stacked_inputs)
+ stacked_inputs = self.input_dropout(stacked_inputs)
+
out = self.convolution(stacked_inputs)
out = self.bn1(out)
out = self.non_linear(out)
out = self.feature_map_dropout(out)
- out = out.view(n, -1)
+ out = out.view(batch_size, -1)
out = self.projection(out)
out = self.projection_dropout(out)
out = self.bn2(out)
@@ -59,7 +71,7 @@ class ConvEScorer(RelationalScorer):
out += self.entity_bias.expand_as(out)
out = self.sigmoid(out)
- return out.view(n, -1)
+ return out.view(batch_size, -1)
class ConvE(KgeModel):
