def forward(self, data): subgraph_data = subgraph_loader(data, k, super_node_size, num_tours, num_cpus) subgraphs = [ get_subgraph(data[subgraph_data.batch[i].item()], subgraph_data.subgraphs[i].squeeze()) for i in range(len(subgraph_data.subgraphs)) ] subgraphs_lst = [] for i in range(0, len(subgraphs), 500): subgraphs_b = Batch().from_data_list( subgraphs[i:i + min([500, len(subgraphs) - i])]) subgraphs_b = self.gnn_layer(subgraphs_b.x.cuda(), subgraphs_b.edge_index.cuda(), subgraphs_b.batch.cuda()) \ if next(self.parameters()).get_device() != -1 else self.gnn_layer(subgraphs_b.x, subgraphs_b.edge_index, subgraphs_b.batch) subgraphs_lst.append(subgraphs_b) subgraphs = torch.cat(subgraphs_lst, dim=0) subgraphs = self.output_layer(subgraphs) weights = subgraph_data.weights.cuda() if next( self.parameters()).get_device() != -1 else subgraph_data.weights batch = subgraph_data.batch.cuda() if next( self.parameters()).get_device() != -1 else subgraph_data.batch subgraphs = subgraphs * weights norm = global_add_pool(weights, batch) energy = global_add_pool(subgraphs, batch) return energy / norm
""" Creating samplers to parallelize over each minibatch """ for i in range(1,batch_size+1): process = subprocess.Popen("cp external_libs/rgpm/tradicional external_libs/rgpm/tradicional-"+str(i), stdout=subprocess.PIPE, stderr=None, shell=True, stdin=subprocess.PIPE) process.wait() model = Net() model.apply(init_weights) best_model = copy.deepcopy(model) best_loss = float("inf") cn_wait = 0 step = 0 print("===\t RANDOM MODEL RESULTS \t===") print("hyperedge:\t", evaluate.logit(train_x=best_model.embedding([ get_subgraph(graph, train["Hyperedge"]["x"][i].squeeze()) for i in range(len(train["Hyperedge"]["x"])) ]).detach().cpu(), train_y=train["Hyperedge"]["y"], \ test_x=best_model.embedding([ get_subgraph(test_graph, test["Hyperedge"]["x"][i].squeeze()) for i in range(len(test["Hyperedge"]["x"])) ]).detach().cpu(), test_y=test["Hyperedge"]["y"] ) ) print("===\t \t===") if cuda: model = model.cuda() optimizer = torch.optim.Adam(model.parameters(), lr=lr) loss_fn = torch.nn.BCEWithLogitsLoss() random.shuffle(dataset) stime = time.time() for i in range(0, len(dataset), batch_size): model.train() optimizer.zero_grad() y = torch.tensor([graph.y for graph in dataset[i:i+min([batch_size,len(dataset)-i])]]).float().unsqueeze(-1) if cuda: y = y.cuda() energy = model(dataset[i:i+min([batch_size,len(dataset)-i])])
stdout=subprocess.PIPE, stderr=None, shell=True, stdin=subprocess.PIPE) process.wait() model = Net() model.apply(init_weights) best_model = copy.deepcopy(model) best_loss = float("inf") cn_wait = 0 step = 0 print("===\t RANDOM MODEL RESULTS \t===") print("dag:\t", evaluate.logit(train_x=best_model.embedding([ get_subgraph(graph, train["DAG"]["x"][i].squeeze()) for i in range(len(train["DAG"]["x"])) ]).detach().cpu(), train_y=train["DAG"]["y"], \ test_x=best_model.embedding([ get_subgraph(graph, test["DAG"]["x"][i].squeeze()) for i in range(len(test["DAG"]["x"])) ]).detach().cpu(), test_y=test["DAG"]["y"] ), \ "hyperedge:\t", evaluate.logit(train_x=best_model.embedding([ get_subgraph(graph, train["Hyperedge"]["x"][i].squeeze()) for i in range(len(train["Hyperedge"]["x"])) ]).detach().cpu(), train_y=train["Hyperedge"]["y"], \ test_x=best_model.embedding([ get_subgraph(graph, test["Hyperedge"]["x"][i].squeeze()) for i in range(len(test["Hyperedge"]["x"])) ]).detach().cpu(), test_y=test["Hyperedge"]["y"] ) ) print("===\t \t===") if cuda: model = model.cuda() optimizer = torch.optim.Adam(model.parameters(), lr=lr) loss_fn = torch.nn.BCEWithLogitsLoss() train_subsample_ids = list(range(len(train["DAG"]["x"]))) test_subsample_ids = list(range(len(test["DAG"]["x"]))) random.shuffle(train_subsample_ids) random.shuffle(test_subsample_ids) train_subsample_ids = train_subsample_ids[:5000] test_subsample_ids = test_subsample_ids[:5000]