Compute logits for intersection. A representation of intersection is the sum of representations of sets that are being intersected. Args: features: features -- candidate_set1: a k-hot vector of length num_entities representing the entities in th
(self, features,
params)
| 207 | return loss, {'logits': logits, 'labels': labels} |
| 208 | |
| 209 | def model_intersection(self, features, |
| 210 | params): |
| 211 | """Compute logits for intersection. |
| 212 | |
| 213 | A representation of intersection is the sum of representations of sets |
| 214 | that are being intersected. |
| 215 | |
| 216 | Args: |
| 217 | features: features |
| 218 | -- candidate_set1: a k-hot vector of length num_entities representing |
| 219 | the entities in the first set. |
| 220 | e.g. [1, 0, 1, 0, 1, 1, 0, 0] |
| 221 | -- candidate_set2: a k-hot vector of length num_entities representing |
| 222 | the entities in the first set. |
| 223 | e.g. [1, 1, 0, 0, 1, 0, 0, 0] |
| 224 | -- labels: a k-hot vector of length num_entities representing |
| 225 | the entities in the intersection of two sets. |
| 226 | e.g. [1, 0, 0, 0, 1, 0, 0, 0] |
| 227 | params: hyper-parameters |
| 228 | |
| 229 | Returns: |
| 230 | predictions and losses |
| 231 | """ |
| 232 | candidate_set1, candidate_set2, labels = features |
| 233 | candidate_set1_mask = tf.expand_dims(candidate_set1, axis=2) |
| 234 | # batch_size, num_candidate, 1 |
| 235 | candidate_set2_mask = tf.expand_dims(candidate_set2, axis=2) |
| 236 | # batch_size, num_candidate, 1 |
| 237 | candidate_embs = self.entity_embeddings_mat |
| 238 | # batch_size, num_candidate, hidden_size |
| 239 | set1_embs_query = tf.reduce_sum( |
| 240 | candidate_embs * candidate_set1_mask, axis=1, keepdims=True) \ |
| 241 | / tf.reduce_sum(candidate_set1_mask, axis=1, keepdims=True) |
| 242 | # batch_size, 1, hidden_size |
| 243 | set2_embs_query = tf.reduce_sum( |
| 244 | candidate_embs * candidate_set2_mask, axis=1, keepdims=True) \ |
| 245 | / tf.reduce_sum(candidate_set2_mask, axis=1, keepdims=True) |
| 246 | # batch_size, 1, hidden_size |
| 247 | intersection_embs_query = set1_embs_query + set2_embs_query |
| 248 | # batch_size, 1, hidden_size |
| 249 | logits, _ = self.compute_logits(candidate_embs, intersection_embs_query, |
| 250 | params['entity_emb_size']) |
| 251 | loss = self.compute_tf_loss(logits, labels) |
| 252 | return loss, {'logits': logits, 'labels': labels} |
| 253 | |
| 254 | def model_follow(self, features, |
| 255 | params): |