0

I want to train "PROTEINS" dataset from the DGL library. In order to do it, I chose a network that involved two SupreGAT layers. When The training process was implemented, I faced the following error. It is my pleasure to help me fix this error.

# Generate a synthetic dataset with 10000 graphs, ranging from 10 to 500 nodes.
dataset = dgl.data.GINDataset("PROTEINS", self_loop=True)

# Defining Data Loader
num_examples = len(dataset)
num_train = int(num_examples * 0.8)

train_sampler = SubsetRandomSampler(torch.arange(num_train))
test_sampler = SubsetRandomSampler(torch.arange(num_train, num_examples))

train_dataloader = GraphDataLoader(
    dataset, sampler=train_sampler, batch_size=5, drop_last=False
)
test_dataloader = GraphDataLoader(
    dataset, sampler=test_sampler, batch_size=5, drop_last=False
)

# Define model
class Net(nn.Module):
    def __init__(self, in_channels, out_channels, heads):
        super(Net, self).__init__()
        # First layer of SuperGATConv
        self.conv1 = SuperGATConv(in_channels, out_channels, heads)
        # Second layer of SuperGATConv
        self.conv2 = SuperGATConv(out_channels * heads, out_channels, heads)
        # Linear layer for classification
        self.linear = nn.Linear(out_channels * heads, dataset.num_classes)

    def forward(self, g, in_channels):
        # Apply the first layer and activation function
        x = F.relu(self.conv1(g, in_channels))
        # Apply the second layer and activation function
        x = F.relu(self.conv2(x, in_channels))
        # Apply the linear layer and return the output
        x = self.linear(x)
        return x
    
# Create the model with given dimensions
model_GAT = Net(in_channels=dataset.dim_nfeats,out_channels=16,heads=4)
optimizer = torch.optim.Adam(model_GAT.parameters(), lr=0.001)


epoch_losses = []
for epoch in range(100):
    epoch_loss = 0
    #for iter, (batched_graph, labels) in enumerate(train_dataloader):
    for batched_graph, labels in train_dataloader:
        pred = model_GAT(dataset, dataset.dim_nfeats)
        loss = F.cross_entropy(pred, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    epoch_loss += loss.detach().item()
    epoch_loss /= (iter + 1)
    print('Epoch {}, loss {:.4f}'.format(epoch, epoch_loss))
    epoch_losses.append(epoch_loss)

### This is the error



AttributeError                            Traceback (most recent call last)
Input In [9], in <cell line: 8>()
     10 #for iter, (batched_graph, labels) in enumerate(train_dataloader):
     11 for batched_graph, labels in train_dataloader:
---> 12     pred = model_GAT(dataset, dataset.dim_nfeats)
     13     loss = F.cross_entropy(pred, labels)
     14     optimizer.zero_grad()

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py:1501, in Module._call_impl(self, *args, **kwargs)
   1496 # If we don't have any hooks, we want to skip the rest of the logic in
   1497 # this function, and just call forward.
   1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1499         or _global_backward_pre_hooks or _global_backward_hooks
   1500         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501     return forward_call(*args, **kwargs)
   1502 # Do not call functions when jit is used
   1503 full_backward_hooks, non_full_backward_hooks = [], []

Input In [6], in Net.forward(self, g, in_channels)
     11 def forward(self, g, in_channels):
     12     # Apply the first layer and activation function
---> 13     x = F.relu(self.conv1(g, in_channels))
     14     # Apply the second layer and activation function
     15     x = F.relu(self.conv2(x, in_channels))

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py:1501, in Module._call_impl(self, *args, **kwargs)
   1496 # If we don't have any hooks, we want to skip the rest of the logic in
   1497 # this function, and just call forward.
   1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1499         or _global_backward_pre_hooks or _global_backward_hooks
   1500         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501     return forward_call(*args, **kwargs)
   1502 # Do not call functions when jit is used
   1503 full_backward_hooks, non_full_backward_hooks = [], []

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\torch_geometric\nn\conv\supergat_conv.py:190, in SuperGATConv.forward(self, x, edge_index, neg_edge_index, batch)
    180 def forward(self, x: Tensor, edge_index: Adj,
    181             neg_edge_index: OptTensor = None,
    182             batch: OptTensor = None) -> Tensor:
    183     r"""Runs the forward pass of the module.
    184 
    185     Args:
   (...)
    188             calculate negative edges. (default: :obj:`None`)
    189     """
--> 190     N, H, C = x.size(0), self.heads, self.out_channels
    192     if self.add_self_loops:
    193         if isinstance(edge_index, SparseTensor):

AttributeError: 'GINDataset' object has no attribute 'size'

    

I aim to understand what step was wrong, and how to fix this error

AttributeError: 'GINDataset' object has no attribute 'size'

0 Answers0