Hello! this graphormer_dgl code.
import torch
import torch.nn as nn
import torch.nn.functional as F
import dgl.function as fn
import dgl
from functools import partial
from dgl.nn import GraphormerLayer
class Graphormer(nn.Module):
def init(self, gnn_layers, in_dim, num_classes, hidden_dimensions, activation, number_heads, final_activation, feat_drop):
super(Graphormer, self).init()
self.gnn_layers = gnn_layers
self.in_dim = in_dim
self.num_classes = num_classes
self.hidden_dimensions = hidden_dimensions
self.activation = activation
self.number_heads = number_heads,
self.final_activation = final_activation
self.feat_drop = feat_drop
self.build_model()
def build_model(self):
self.layers = nn.ModuleList()
# input to hidden
i2h = self.build_input_layer()
self.layers.append(i2h)
# hidden to hidden
for i in range(self.gnn_layers-2):
h2h = self.build_hidden_layer(i)
self.layers.append(h2h)
# hidden to output
h2o = self.build_output_layer()
self.layers.append(h2o)
def build_input_layer(self):
print('Building an INPUT layer of {}x{}'.format(self.in_dim, self.hidden_dimensions[0]))
return GraphormerLayer(self.in_dim, self.hidden_dimensions[0], self.number_heads, self.feat_drop, activation=self.activation)
def build_hidden_layer(self, i):
print('Building an HIDDEN layer of {}x{}'.format(self.hidden_dimensions[i], self.hidden_dimensions[i+1]))
return GraphormerLayer(self.hidden_dimensions[i], self.hidden_dimensions[i+1], self.number_heads, self.feat_drop, activation=self.activation)
def build_output_layer(self):
print('Building an OUTPUT layer of {}x{}'.format(self.hidden_dimensions[-1], self.num_classes))
return GraphormerLayer(self.hidden_dimensions[-1], self.num_classes, self.number_heads, self.feat_drop, activation=self.final_activation)
# def forward(self, features, etypes):
# h = features
# for layer in self.layers:
# h = layer(self.g, h, etypes)
# return h
def set_g(self, g):
self.g = g
for l in range(self.gnn_layers):
self.layers[l].g = g
def forward(self, inputs, g):
self.set_g(g)
# h = inputs.reshape(inputs.shape[0], inputs.shape[1])
h = inputs
if h.dim() == 2:
h = h.unsqueeze(0)
print(h.shape)
print(h.shape)
print(h.shape)
for l in range(self.gnn_layers-1):
h = self.layers[l](h).flatten(1)
h = self.activation(h)
h = self.layers[self.gnn_layers-1](h)
if self.final_activation is not None:
logits = self.final_activation(h)
else:
logits = h
return logits
And I encountered this error.
torch.Size([1, 5793, 42])
torch.Size([1, 5793, 42])
torch.Size([1, 5793, 42])
Traceback (most recent call last):
File “/sung/sung/sngnnv2-graphormer/train.py”, line 342, in
best_loss = main(‘train_set.txt’, ‘dev_set.txt’, ‘test_set.txt’,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/sung/sung/sngnnv2-graphormer/train.py”, line 226, in main
logits = model(feats.float(), subgraph, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/sung/sung/sngnnv2-graphormer/select_gnn.py”, line 80, in forward
x = self.gnn_object(data, g)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/sung/sung/sngnnv2-graphormer/nets/graphormer_dgl.py”, line 75, in forward
h = self.layersl.flatten(1)
^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/dgl/nn/pytorch/gt/graphormer.py”, line 116, in forward
nfeat = self.attn(nfeat, attn_bias, attn_mask)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/dgl/nn/pytorch/gt/biased_mha.py”, line 122, in forward
q_h = self.q_proj(ndata).transpose(0, 1)
^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/module.py”, line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/sung/miniconda3/lib/python3.11/site-packages/torch/nn/modules/linear.py”, line 114, in forward
return F.linear(input, self.weight, self.bias)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x243306 and 28x28)
Could someone indicate what might be triggering this code? Thank you.