def test_APPNP_linkmodel_apply_sparse(): G, features = create_graph_features() adj = G.to_adjacency_matrix() features, adj = GCN_Aadj_feats_op(features, adj) adj = adj.tocoo() A_indices = np.expand_dims(np.hstack((adj.row[:, None], adj.col[:, None])), 0) A_values = np.expand_dims(adj.data, 0) generator = FullBatchLinkGenerator(G, sparse=True, method="gcn") appnpnModel = APPNP( layer_sizes=[3], activations=["relu"], generator=generator, dropout=0.5 ) x_in, x_out = appnpnModel.in_out_tensors() model = keras.Model(inputs=x_in, outputs=x_out) # Check fit method out_indices = np.array([[[0, 1], [1, 2]]], dtype="int32") preds_1 = model.predict([features[None, :, :], out_indices, A_indices, A_values]) assert preds_1.shape == (1, 2, 2, 3) # Check fit method preds_2 = model.predict(generator.flow([("a", "b"), ("b", "c")])) assert preds_2.shape == (1, 2, 2, 3) assert preds_1 == pytest.approx(preds_2)
def test_APPNP_linkmodel_apply_dense(): G, features = create_graph_features() adj = nx.to_numpy_array(G)[None, :, :] n_nodes = features.shape[0] nodes = G.nodes() node_features = pd.DataFrame.from_dict( {n: f for n, f in zip(nodes, features)}, orient="index") G = StellarGraph(G, node_features=node_features) generator = FullBatchLinkGenerator(G, sparse=False, method="none") appnpnModel = APPNP([3], generator, activations=["relu"], dropout=0.5) x_in, x_out = appnpnModel.build() model = keras.Model(inputs=x_in, outputs=x_out) # Check fit method out_indices = np.array([[[0, 1], [1, 2]]], dtype="int32") preds_1 = model.predict([features[None, :, :], out_indices, adj]) assert preds_1.shape == (1, 2, 2, 3) # Check fit_generator method preds_2 = model.predict_generator(generator.flow([("a", "b"), ("b", "c")])) assert preds_2.shape == (1, 2, 2, 3) assert preds_1 == pytest.approx(preds_2)
def test_generator_flow_targets_as_list(self): generator = FullBatchLinkGenerator(self.G) link_ids = list(self.G.edges())[:3] link_targets = [1] * len(link_ids) gen = generator.flow(link_ids, link_targets) inputs, y = gen[0] assert y.shape == (1, 3) assert np.sum(y) == 3
def generator_flow( self, G, link_ids, link_targets, sparse=False, method="none", k=1, teleport_probability=0.1, ): generator = FullBatchLinkGenerator( G, sparse=sparse, method=method, k=k, teleport_probability=teleport_probability, ) n_nodes = G.number_of_nodes() gen = generator.flow(link_ids, link_targets) if sparse: [X, tind, A_ind, A_val], y = gen[0] A_sparse = sps.coo_matrix( (A_val[0], (A_ind[0, :, 0], A_ind[0, :, 1])), shape=(n_nodes, n_nodes)) A_dense = A_sparse.toarray() else: [X, tind, A], y = gen[0] A_dense = A[0] assert np.allclose(X, gen.features) # X should be equal to gen.features assert isinstance(tind, np.ndarray) assert tind.ndim == 3 assert tind.shape[1] == len(link_ids) assert tind.shape[2] == 2 if link_targets is not None: assert np.allclose(y, link_targets) # Check that the diagonals are one if method == "self_loops": assert np.allclose(A_dense.diagonal(), 1) return A_dense, tind, y
def test_APPNP_linkmodel_apply_dense(): G, features = create_graph_features() adj = G.to_adjacency_matrix() adj = np.array(adj.todense()[None, :, :]) generator = FullBatchLinkGenerator(G, sparse=False, method="none") appnpnModel = APPNP([3], generator, activations=["relu"], dropout=0.5) x_in, x_out = appnpnModel.in_out_tensors() model = keras.Model(inputs=x_in, outputs=x_out) # Check fit method out_indices = np.array([[[0, 1], [1, 2]]], dtype="int32") preds_1 = model.predict([features[None, :, :], out_indices, adj]) assert preds_1.shape == (1, 2, 2, 3) # Check fit method preds_2 = model.predict(generator.flow([("a", "b"), ("b", "c")])) assert preds_2.shape == (1, 2, 2, 3) assert preds_1 == pytest.approx(preds_2)
def test_APPNP_linkmodel_apply_sparse(): G, features = create_graph_features() adj = nx.to_scipy_sparse_matrix(G) features, adj = GCN_Aadj_feats_op(features, adj) adj = adj.tocoo() A_indices = np.expand_dims(np.hstack((adj.row[:, None], adj.col[:, None])), 0) A_values = np.expand_dims(adj.data, 0) nodes = G.nodes() node_features = pd.DataFrame.from_dict( {n: f for n, f in zip(nodes, features)}, orient="index") G = StellarGraph(G, node_features=node_features) generator = FullBatchLinkGenerator(G, sparse=True, method="gcn") appnpnModel = APPNP(layer_sizes=[3], activations=["relu"], generator=generator, dropout=0.5) x_in, x_out = appnpnModel.build() model = keras.Model(inputs=x_in, outputs=x_out) # Check fit method out_indices = np.array([[[0, 1], [1, 2]]], dtype="int32") preds_1 = model.predict( [features[None, :, :], out_indices, A_indices, A_values]) assert preds_1.shape == (1, 2, 2, 3) # Check fit_generator method preds_2 = model.predict_generator(generator.flow([("a", "b"), ("b", "c")])) assert preds_2.shape == (1, 2, 2, 3) assert preds_1 == pytest.approx(preds_2)
# reduced graph G_test with the sampled links removed: G_test, edge_ids_test, edge_labels_test = edge_splitter_test.train_test_split( p=0.1, method="global", keep_connected=True) # Define an edge splitter on the reduced graph G_test: edge_splitter_train = EdgeSplitter(G_test) # Randomly sample a fraction p=0.1 of all positive links, and same number of negative links, from G_test, and obtain the # reduced graph G_train with the sampled links removed: G_train, edge_ids_train, edge_labels_train = edge_splitter_train.train_test_split( p=0.1, method="global", keep_connected=True) epochs = 50 train_gen = FullBatchLinkGenerator(G_train, method="gcn") train_flow = train_gen.flow(edge_ids_train, edge_labels_train) test_gen = FullBatchLinkGenerator(G_test, method="gcn") test_flow = train_gen.flow(edge_ids_test, edge_labels_test) gcn = GCN(layer_sizes=[16, 16], activations=["relu", "relu"], generator=train_gen, dropout=0.3) x_inp, x_out = gcn.in_out_tensors() prediction = LinkEmbedding(activation="relu", method="ip")(x_out) prediction = keras.layers.Reshape((-1, ))(prediction)
def test_generator_flow_targets_not_iterator(self): generator = FullBatchLinkGenerator(self.G) link_ids = list(self.G.edges())[:3] link_targets = 1 with pytest.raises(TypeError): generator.flow(link_ids, link_targets)