Construct pairs of sets from KB with at least one entity in common. Args: train_split: percentage of data for training Returns: a list of train pairs and test pairs
(
self, train_split)
| 305 | return train_data, test_data |
| 306 | |
| 307 | def construct_set_pairs( |
| 308 | self, train_split): |
| 309 | """Construct pairs of sets from KB with at least one entity in common. |
| 310 | |
| 311 | Args: |
| 312 | train_split: percentage of data for training |
| 313 | |
| 314 | Returns: |
| 315 | a list of train pairs and test pairs |
| 316 | """ |
| 317 | random.seed(SEED) |
| 318 | subj2objs = dict() |
| 319 | for subj, facts in self.kb.items(): |
| 320 | all_objs = set() |
| 321 | for unused_rel, objs in facts.items(): |
| 322 | all_objs.update(objs) |
| 323 | subj2objs[subj] = all_objs |
| 324 | |
| 325 | train_pairs = list() |
| 326 | test_pairs = list() |
| 327 | for target, objs in subj2objs.items(): |
| 328 | if len(objs) <= 1: |
| 329 | continue |
| 330 | # Find candidate sets which have at least 2 entities. |
| 331 | candidate_sets = list() |
| 332 | for obj in objs: |
| 333 | for unused_rel, subjs in self.kb[obj].items(): |
| 334 | if len(subjs) > 1 and target in subjs: |
| 335 | candidate_sets.append(subjs) |
| 336 | # Randomly pick some pair of sets. |
| 337 | num_pairs = int(len(candidate_sets) / 2) |
| 338 | for i in range(num_pairs): |
| 339 | set1 = candidate_sets[2 * i] |
| 340 | set2 = candidate_sets[2 * i + 1] |
| 341 | if random.random() < train_split: |
| 342 | train_pairs.append((set1, set2)) |
| 343 | else: |
| 344 | test_pairs.append((set1, set2)) |
| 345 | tf.logging.info( |
| 346 | 'set_pair (intersection & union) examples: %d train, %d test', |
| 347 | len(train_pairs), len(test_pairs)) |
| 348 | |
| 349 | return train_pairs, test_pairs |
| 350 | |
| 351 | def construct_set_follow_facts( |
| 352 | self, train_split): |