TypeError: empty(): argument 'size' must be tuple of ints, but found element of type Tensor at pos 1

I try to implement GCN on my custom dataset, but I got error:

my code:

# Define our GCN class as a pytorch Module
class GCN(torch.nn.Module):
    def __init__(self, hidden_channels):
        super(GCN, self).__init__()
        # We inherit from pytorch geometric's GCN class, and we initialize three layers
        self.conv1 = GCNConv(data.num_features, hidden_channels)
        self.conv2 = GCNConv(hidden_channels, hidden_channels)
        self.conv3 = GCNConv(hidden_channels, hidden_channels)
        # Our final linear layer will define our output
        self.lin = Linear(hidden_channels, data.num_classes)
        
    def forward(self, x, edge_index, batch):
      # 1. Obtain node embeddings 
      x = self.conv1(x, edge_index)
      x = x.relu()
      x = self.conv2(x, edge_index)
      x = x.relu()
      x = self.conv3(x, edge_index)   
 
      # 2. Readout layer
      x = global_mean_pool(x, batch)  # [batch_size, hidden_channels]
 
      # 3. Apply a final classifier
      x = F.dropout(x, p=0.5, training=self.training)
      x = self.lin(x)
      return x
    
model = GCN(hidden_channels=16)
print(model)

The error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_59/1705364933.py in <module>
     32       return x
     33 
---> 34 model = GCN(hidden_channels=16)
     35 print(model)

/tmp/ipykernel_59/1705364933.py in __init__(self, hidden_channels)
     14         self.conv3 = GCNConv(hidden_channels, hidden_channels)
     15         # Our final linear layer will define our output
---> 16         self.lin = Linear(hidden_channels, data.num_classes)
     17 
     18     def forward(self, x, edge_index, batch):

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/linear.py in __init__(self, in_features, out_features, bias, device, dtype)
     83         self.in_features = in_features
     84         self.out_features = out_features
---> 85         self.weight = Parameter(torch.empty((out_features, in_features), **factory_kwargs))
     86         if bias:
     87             self.bias = Parameter(torch.empty(out_features, **factory_kwargs))

TypeError: empty(): argument 'size' must be tuple of ints, but found element of type Tensor at pos 1

Could you print out data.num_classes?

tensor([0, 1, 3, 2])

nn.Linear should be defined by two integers.

In short
data.num_classes = n instead of data.num_classes = torch.tensor([0, 1, 3, 2])

1 Like