def _get_triple_score(self, head: BoxTensor, tail: BoxTensor, relation: BoxTensor, label: Tensor) -> torch.Tensor: """ Gets score using conditionals. :: note: We do not need to worry about the dimentions of the boxes. If it can sensibly broadcast it will. """ head.data[..., 0, :] = head.data[..., 0, :] % 1 head.data[..., 1, :] = torch.sigmoid(head.data[..., 1, :]) tail.data[..., 0, :] = tail.data[..., 0, :] % 1 tail.data[..., 1, :] = torch.sigmoid(tail.data[..., 1, :]) triple = torch.stack((head.data, tail.data), dim=-3) int_lengths = head.per_dim_int_length(triple) triple = torch.stack((head.data, tail.data), dim=-3) head_tail_box_vol = head._intersection_volume(triple, True, eps=1e-20) # score = tail_head_relation_box_vol - tail_relation_box.log_soft_volume( # temp=self.softbox_temp) tail_data = tail.data.view(-1, 1, 2, self.embedding_dim) int_lengths_tail = tail.per_dim_int_length(tail_data, True) tail_volume = torch.sum(torch.log(int_lengths_tail.clamp_min(1e-20)), dim=-1) score = head_tail_box_vol - tail_volume triple = torch.stack((head.data, tail.data), dim=-3) target_probs = label if not self.is_eval(): self.surr_loss = self.pull_loss(triple, target_probs, int_lengths) else: self.surr_loss = 0 return score
def _get_triple_score(self, head: BoxTensor, tail: BoxTensor, relation: BoxTensor, triple_type: str = 'pos') -> torch.Tensor: """ Gets score using conditionals. :: note: We do not need to worry about the dimentions of the boxes. If it can sensibly broadcast it will. """ triple = torch.stack((head.data, tail.data), dim=-3) int_lengths = head.per_dim_int_length(triple) head_tail_box_vol = torch.sum( torch.log(int_lengths.clamp_min(0) + 1e-8), dim=-1) # score = tail_head_relation_box_vol - tail_relation_box.log_soft_volume( # temp=self.softbox_temp) tail_data = tail.data.view(-1, 1, 2, self.embedding_dim) score = head_tail_box_vol - tail._intersection_volume(tail_data, True) if triple_type == 'pos': triple = torch.stack((head.data, tail.data), dim=-3) target_probs = torch.ones_like(score) if not self.is_eval(): self.surr_loss = self.pull_loss(triple, target_probs, int_lengths) else: self.surr_loss = 0 return score