def _classify_relations(self, entity_spans, size_embeddings, relations, rel_masks, h, chunk_start): batch_size = relations.shape[0] # create chunks if necessary if relations.shape[1] > self._max_pairs: relations = relations[:, chunk_start:chunk_start + self._max_pairs] rel_masks = rel_masks[:, chunk_start:chunk_start + self._max_pairs] h = h[:, :relations.shape[1], :] # get pairs of entity candidate representations entity_pairs = util.batch_index(entity_spans, relations) entity_pairs = entity_pairs.view(batch_size, entity_pairs.shape[1], -1) # get corresponding size embeddings size_pair_embeddings = util.batch_index(size_embeddings, relations) size_pair_embeddings = size_pair_embeddings.view( batch_size, size_pair_embeddings.shape[1], -1) # relation context (context between entity candidate pair) rel_ctx = rel_masks * h rel_ctx = rel_ctx.max(dim=2)[0] # create relation candidate representations including context, max pooled entity candidate pairs # and corresponding size embeddings rel_repr = torch.cat([rel_ctx, entity_pairs, size_pair_embeddings], dim=2) rel_repr = self.dropout(rel_repr) # classify relation candidates chunk_rel_logits = self.rel_classifier(rel_repr) return chunk_rel_logits
def _classify_relations(self, entity_spans, size_embeddings, relations, rel_masks, h, chunk_start): batch_size = relations.shape[0] # create chunks if necessary if relations.shape[1] > self._max_pairs: relations = relations[:, chunk_start:chunk_start + self._max_pairs] rel_masks = rel_masks[:, chunk_start:chunk_start + self._max_pairs] h = h[:, :relations.shape[1], :] # get pairs of entity candidate representations entity_pairs = util.batch_index(entity_spans, relations) entity_pairs = entity_pairs.view(batch_size, entity_pairs.shape[1], -1) # get corresponding size embeddings size_pair_embeddings = util.batch_index(size_embeddings, relations) size_pair_embeddings = size_pair_embeddings.view(batch_size, size_pair_embeddings.shape[1], -1) # relation context (context between entity candidate pair) # mask non entity candidate tokens m = ((rel_masks == 0).float() * (-1e30)).unsqueeze(-1) rel_ctx = m + h # max pooling rel_ctx = rel_ctx.max(dim=2)[0] # set the context vector of neighboring or adjacent entity candidates to zero rel_ctx[rel_masks.to(torch.uint8).any(-1) == 0] = 0 # create relation candidate representations including context, max pooled entity candidate pairs # and corresponding size embeddings rel_repr = torch.cat([rel_ctx, entity_pairs, size_pair_embeddings], dim=2) rel_repr = self.dropout(rel_repr) # classify relation candidates chunk_rel_logits = self.rel_classifier(rel_repr) return chunk_rel_logits