Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dgl feature bugfix #116

Merged
merged 8 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion photonai_graph/GraphConversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def dense_to_dgl(graphs, adjacency_axis=None, feature_axis=None):
for graph in range(graphs.shape[0]):
src, dst = np.nonzero(graphs[graph, :, :, adjacency_axis])
g = dgl.graph((src, dst), num_nodes=graphs[graph, ...].shape[1])
feat = torch.tensor(graphs[graph, :, :, feature_axis])
feat = torch.tensor(graphs[graph, :, :, feature_axis]).float()
g.ndata['feat'] = feat
graph_list.append(g)
return graph_list
Expand Down
7 changes: 4 additions & 3 deletions photonai_graph/NeuralNets/GATModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, in_dim, hidden_dim, num_heads, n_classes, hidden_layers,
def forward(self, bg):
# For undirected graphs, in_degree is the same as
# out_degree.
h = bg.in_degrees().view(-1, 1).float()
h = bg.ndata['feat']
for i, gnn in enumerate(self.layers):
h = gnn(bg, h)
if self.agg_mode == 'flatten':
Expand Down Expand Up @@ -121,7 +121,8 @@ def __init__(self,
self.gpu = gpu

def _init_model(self, X=None, y=None):
self.model = GATModel(self.in_dim, self.hidden_dim, self.heads,
self.model = GATModel(X.shape[1] if isinstance(X, (np.ndarray, np.array)) else X[0].num_nodes(),
self.hidden_dim, self.heads,
len(np.unique(y)), self.hidden_layers, self.agg_mode,
allow_zero_in_degree=self.allow_zero_in_degree)

Expand Down Expand Up @@ -201,5 +202,5 @@ def __init__(self,
self.gpu = gpu

def _init_model(self, X=None, y=None):
self.model = GATModel(self.in_dim, self.hidden_dim, self.heads, 1, self.hidden_layers,
self.model = GATModel(X.shape[1], self.hidden_dim, self.heads, 1, self.hidden_layers,
allow_zero_in_degree=self.allow_zero_in_degree, agg_mode=self.agg_mode).float()
6 changes: 3 additions & 3 deletions photonai_graph/NeuralNets/GCNModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, in_dim, hidden_dim, n_classes, hidden_layers, allow_zero_in_d
def forward(self, g):
# Use node degree as the initial node feature. For undirected graphs, the in-degree
# is the same as the out_degree.
h = g.in_degrees().view(-1, 1).float()
h = g.ndata['feat']
# Perform graph convolution and activation function.
for i, gnn in enumerate(self.layers):
h = F.relu(gnn(g, h))
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(self,
self.gpu = gpu

def _init_model(self, X=None, y=None):
self.model = GCNClassifier(self.in_dim,
self.model = GCNClassifier(X.shape[1],
self.hidden_dim,
len(np.unique(y)),
self.hidden_layers,
Expand Down Expand Up @@ -172,7 +172,7 @@ def __init__(self,
self.gpu = gpu

def _init_model(self, X=None, y=None):
self.model = GCNClassifier(self.in_dim,
self.model = GCNClassifier(X.shape[1] if isinstance(X, (np.ndarray, np.array)) else X[0].num_nodes(),
self.hidden_dim, 1,
self.hidden_layers,
allow_zero_in_degree=self.allow_zero_in_degree).float()
8 changes: 4 additions & 4 deletions photonai_graph/NeuralNets/SGCModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, in_dim, hidden_dim, n_classes, hidden_layers, allow_zero_in_d
self.classify = nn.Linear(hidden_dim, n_classes)

def forward(self, bg):
h = bg.in_degrees().view(-1, 1).float()
h = bg.ndata['feat']
for lr, layer in enumerate(self.layers):
h = layer(bg, h)
bg.ndata['h'] = h
Expand Down Expand Up @@ -103,8 +103,8 @@ def __init__(self,
self.gpu = gpu

def _init_model(self, X=None, y=None):
self.model = SGConvClassifier(self.in_dim, self.hidden_dim,
len(np.unique(y)), self.hidden_layers,
self.model = SGConvClassifier(X.shape[1] if isinstance(X, (np.ndarray, np.array)) else X[0].num_nodes(),
self.hidden_dim, len(np.unique(y)), self.hidden_layers,
allow_zero_in_degree=self.allow_zero_in_degree)


Expand Down Expand Up @@ -174,5 +174,5 @@ def __init__(self,
self.gpu = gpu

def _init_model(self, X=None, y=None):
self.model = SGConvClassifier(self.in_dim, self.hidden_dim, 1, self.hidden_layers,
self.model = SGConvClassifier(X.shape[1], self.hidden_dim, 1, self.hidden_layers,
allow_zero_in_degree=self.allow_zero_in_degree).float()
2 changes: 1 addition & 1 deletion test/conversion_tests/test_dense_to_dgl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def test_disconnected_graph(self):
dgl_graph = dense_to_dgl(in_graph, adjacency_axis=0, feature_axis=1)[0]
self.assertEqual(dgl_graph.num_nodes(), 3)
self.assertEqual(dgl_graph.num_edges(), 2)
self.assertTrue(np.array_equal(dgl_graph.ndata['feat'].numpy(), in_feats[0, ..., 0]))
self.assertTrue(np.array_equal(dgl_graph.ndata['feat'].numpy(), in_feats[0, ..., 0].astype("float32")))
6 changes: 0 additions & 6 deletions test/nn_tests/test_gat_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,3 @@ def test_gat_classifier_output_hidden_dim(self):
gat_clf.fit(self.Xrandom4d, self.y)
output = gat_clf.predict(self.Xrandom4d)
self.assertEqual(output.shape, self.y.shape)

def test_gat_classifier_dgl(self):
gat_clf = GATClassifierModel(nn_epochs=20)
gat_clf.fit(self.X_dgl, self.y)
output = gat_clf.predict(self.X_dgl)
self.assertEqual(output.shape, self.y.shape)
6 changes: 0 additions & 6 deletions test/nn_tests/test_gcn_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,3 @@ def test_gcn_classifier_output_hidden_dim(self):
gcn_clf.fit(self.Xrandom4d, self.y)
output = gcn_clf.predict(self.Xrandom4d)
self.assertTrue(np.array_equal(output.shape, self.y.shape))

def test_gat_classifier_dgl(self):
gat_clf = GCNClassifierModel(nn_epochs=20)
gat_clf.fit(self.X_dgl, self.y)
output = gat_clf.predict(self.X_dgl)
self.assertTrue(np.array_equal(np.array(output.shape), self.y.shape))