Shape mismatch between logits and labels for computing loss on 3D data in GCN

yari: <blockquote>
<p>RuntimeError: Expected target size [25, 10], got [25].</p>
</blockquote>
<p>I have a sensors data and I want to perform node level classification using GCN. I have <strong>k</strong> nodes (sensors). Each node has <strong>m</strong> observations (node features) and each observation is a <strong>n</strong> dimensional vector. Shape of my dataset is <em>k,m,n</em>, its a 3d dataset. I used DGL to convert data to graph form.</p>
<pre><code class="lang-auto">k =  25
m = 2000
n = 6
num_classes = 10

node_features = torch.randn((k, m, n)) #Generate node features.
node_labels = torch.tensor([i  for i in range (k)]) #Create tensor of labels

#Create graph
nodes= [i for i in range(k)]
edges = []
for node1, node2 in combinations(nodes, 2):
    edges.append([node1, node2])
a = np.array(edges)
src =  a[:,0]
dst = a[:,1]
g = dgl.graph((src , dst)) # create graph

g.ndata['features'] = node_features # Assign node features 
g.ndata['labels'] = node_labels # Assign labels
</code></pre>
<p>Above code creates a graph. Below is graph structure.</p>
<blockquote>
<p>Graph(num_nodes=25, num_edges=300,<br>
ndata_schemes={‘features’: Scheme(shape=(2000, 6), dtype=torch.float32), ‘labels’: Scheme(shape=(), dtype=torch.int64)}<br>
edata_schemes={})</p>
</blockquote>
<p>Next, I build the GCN model.</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F
from dgl.nn import GraphConv

class GCN(nn.Module):
    def __init__(self, in_feats, h_feats, num_classes):
        super(GCN, self).__init__()
        self.conv1 = GraphConv(in_feats, h_feats)
        self.conv2 = GraphConv(h_feats, num_classes)

    def forward(self, g, in_feat):
        h = self.conv1(g, in_feat)
        h = F.relu(h)
        h = self.conv2(g, h)
        return h
</code></pre>
<p>Training of model is:</p>
<pre><code class="lang-auto">num_epochs = 50 
hidden_size = 64
features = g.ndata["features"]
labels = g.ndata["labels"]

model = GCN(n, hidden_size , num_classes)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()


# Train model
for epoch in range(num_epochs):
    logits = model(g, g.ndata['features'])
    loss = criterion(logits, labels)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    print(f"Epoch {epoch + 1}, loss: {loss.item()}")
</code></pre>
<p>On line <code>loss = criterion(logits, labels)</code>, following error occurs.</p>
<blockquote>
<p>RuntimeError: Expected target size [25, 10], got [25].</p>
</blockquote>
<p>How I can solve this problem? If I pass 2 dimensional data to this model, it works fine but my data is 3 dimensional.</p>
<p><code>Note: Shape of logits is torch.Size([k, m, num_classes ])  while shape of labels is torch.Size([k])</code>.</p>

ptrblck: <p><code>nn.CrossEntropyLoss</code> expects a model output containing class logits in the shape <code>[batch_size, nb_classes, *]</code> and a target in the shape <code>[batch_size, *]</code> containing class indices in the range <code>[0, nb_classes-1]</code>, where the <code>*</code> denotes additional dimensions.<br>
Your current output in the shape <code>[k, m, n]</code> indicates you are working with a batch size of <code>k</code>, <code>m</code> classes, and a sequence length of <code>n</code>.<br>
In this case, the target should contain a label for each time step of the sequence <code>n</code> and thus should have a shape of <code>[k, n]</code>.</p>

yari: <p>Sorry, there was a mistake by me previously. I updated my question. Actually shape of <code>logits = [k,m,num_classes ]</code></p>

ptrblck: <p>In that case you would have to <code>permute</code> the output and make sure the dimensions correspond to:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="173130">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>model output containing class logits in the shape <code>[batch_size, nb_classes, *]</code></p>
</blockquote>
</aside>

yari: <p>Can you please provide me some lines of code how to do that? I am new to GNN and its first time I am dealing with 3d data.</p>

ptrblck: <p>Sure, here is an example:</p>
<pre><code class="lang-python">batch_size = 2
nb_classes = 3
seq_len = 4
logits = torch.randn(batch_size, seq_len, nb_classes, requires_grad=True)
targets = torch.randint(0, nb_classes-1, (batch_size, seq_len))

criterion = nn.CrossEntropyLoss()
logits = logits.permute(0, 2, 1)
print(logits.shape)
# torch.Size([2, 3, 4]) # corresponds to [batch_size, nb_classes, seq_len]

loss = criterion(logits, targets)
</code></pre>

yari: <p>For random labels generation, this is good. If my label vector is <code>[0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,] (size = k = 25)</code>, then how to convert that static vector to required shape of <code>(batch_size, seq_len)</code>?</p>

ptrblck: <p>Before trying to convert the target to another format, you should think about what your output represents and how the corresponding targets look like.<br>
As previously described, your model output indicates a temporal dimension (or in a more general sense: an additional dimension). Let’s call it temporal dimension for the sake of this example:<br>
your model thus contains logits for each of the classes for each sample and each timestep.<br>
The corresponding target should thus contain class indices for each sample in the current batch and for each timestep.<br>
If your target only contains labels for each sample in the batch, your model output is most likely wrong.</p>

yari: <p>What I want to do is that I want to predict label for each node in the graph, it is a node level classification problem. There are <code>k = 25</code> nodes so size of labels is also <code>25</code>. Each node contains <code>m=2000</code> samples and the dimensions of each sample is <code>n=6</code>. It looks like model is containing label for each sample instead of only for each node. Is it possible to modify <code>GCN</code> model so that it returns labels for each node? In that case there will no need to change shape of albels.</p>

yari: <p>Yes, that’s correct. The output tensor from the GCN model contains logits for each of the classes for each observation at each node in the graph, across a temporal dimension. In other words, it produces a sequence of logits over time for each node.</p>

yari: <p>Need to modify the model so that it produces logits for each class at each node in the graph, rather than for each class at each observation (sample) at each node. But I have no idea how to do that and modify the model in such a way.</p>

ptrblck: <p>I’m not familiar with your model and GCNs in general, so don’t know which part of the model should be changed. However, a simple approach would be to reduce the temporal dimension, e.g. via <code>torch.mean</code>, or to use only the last “time step” of the sequence of predictions.</p>