Divide a normal graph into a train split and several test splits for link prediction use. Each test split contains half true and half false edges. Parameters: graph_file (str): graph file files (list of str): file names, the first fil
(self, graph_file, files, portions)
| 316 | file.close() |
| 317 | |
| 318 | def link_prediction_split(self, graph_file, files, portions): |
| 319 | """ |
| 320 | Divide a normal graph into a train split and several test splits for link prediction use. |
| 321 | Each test split contains half true and half false edges. |
| 322 | |
| 323 | Parameters: |
| 324 | graph_file (str): graph file |
| 325 | files (list of str): file names, |
| 326 | the first file is treated as train file |
| 327 | portions (list of float): split portions |
| 328 | """ |
| 329 | assert len(files) == len(portions) |
| 330 | logger.info("splitting graph %s into %s" % |
| 331 | (self.relpath(graph_file), ", ".join([self.relpath(file) for file in files]))) |
| 332 | np.random.seed(1024) |
| 333 | |
| 334 | nodes = set() |
| 335 | edges = set() |
| 336 | portions = np.cumsum(portions, dtype=np.float32) / np.sum(portions) |
| 337 | files = [open(file, "w") for file in files] |
| 338 | num_edges = [0] * len(files) |
| 339 | with open(graph_file, "r") as fin: |
| 340 | for line in fin: |
| 341 | u, v = line.split()[:2] |
| 342 | nodes.update([u, v]) |
| 343 | edges.add((u, v)) |
| 344 | i = np.searchsorted(portions, np.random.rand()) |
| 345 | if i == 0: |
| 346 | files[i].write(line) |
| 347 | else: |
| 348 | files[i].write("%s\t%s\t1\n" % (u, v)) |
| 349 | num_edges[i] += 1 |
| 350 | |
| 351 | nodes = list(nodes) |
| 352 | for file, num_edge in zip(files[1:], num_edges[1:]): |
| 353 | for _ in range(num_edge): |
| 354 | valid = False |
| 355 | while not valid: |
| 356 | u = nodes[int(np.random.rand() * len(nodes))] |
| 357 | v = nodes[int(np.random.rand() * len(nodes))] |
| 358 | valid = u != v and (u, v) not in edges and (v, u) not in edges |
| 359 | file.write("%s\t%s\t0\n" % (u, v)) |
| 360 | for file in files: |
| 361 | file.close() |
| 362 | |
| 363 | def image_feature_data(self, dataset, model="resnet50", batch_size=128): |
| 364 | """ |