ValueError: Expected input batch_size (440) to match target batch_size (40)

safakhan: <p>Hello!</p>
<p>I have created dataset for 4 diff functions. My dataset is one-hot-encoded. Basically just 40 data elements, each being a one-hot-encoded string of 11 characters. There are total of 31 mappings.</p>
<p>So my dataset looks like tensor of (40,11,31)</p>
<p>My output is a label, with one one-hot-encoded letter. e.g. h,f<br>
so label tensor is of the size (40,1,31)</p>
<p>My code looks like this</p>
<p>Error occurs on line<br>
loss = criterion(outputs, labels)</p>
<pre><code class="lang-auto"> ```python
import torch
import torch.nn as nn
from torch.autograd import Variable


###################### define parameters for RNN #############

inputs = Variable(torch.Tensor(input_tensors_one_hot))
labels = Variable(torch.LongTensor(tensor_of_labels))

# print(f'im inputs size {inputs.size()}' )
print(f'im labels size {labels}' )


num_classes = 31
input_size = 31  # one-hot size
hidden_size = 31  # output from the LSTM. 31 to directly predict one-hot
batch_size = 1   # one string
sequence_length = 11  # string length == 11
num_layers = 1  # one-layer rnn
# print(inputs[0])

################ Define RNN #################

class RNN(nn.Module):

    def __init__(self, num_classes, input_size, hidden_size, num_layers):
        super(RNN, self).__init__()

        self.num_classes = num_classes
        self.num_layers = num_layers
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.sequence_length = sequence_length
        self.rnn = nn.RNN(input_size=31, hidden_size=31, batch_first=True)


    def forward(self, x):
        # Initialize hidden and cell states
        # (num_layers * num_directions, batch, hidden_size) for batch_first=True
        h_0 = Variable(torch.zeros(
            self.num_layers, x.size(0), self.hidden_size))

        # Reshape input
        x.view(x.size(0), self.sequence_length, self.input_size)    
# Propagate input through RNN
        # Input: (batch, seq_len, input_size)
        # h_0: (num_layers * num_directions, batch, hidden_size)

        out, _ = self.rnn(x, h_0)
        print(f'im hidden size shape {out.size()}')

        # dense_outputs = self.fc(out)
        # print(f'im dense outputs {dense_outputs}')
        return out.reshape(-1, num_classes)
        # return out.view(-1, num_classes)


# Instantiate RNN model
rnn = RNN(num_classes, input_size, hidden_size, num_layers)
print(rnn)

# Set loss and optimizer function
# CrossEntropyLoss = LogSoftmax + NLLLoss
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.1)

# Train the model
for epoch in range(100):
    outputs = rnn(inputs)
    print('Im output size ', outputs.size())
    optimizer.zero_grad()
    print('Im label size ', labels.size())
    loss = criterion(outputs, labels)  
    loss.backward()
    optimizer.step()
    _, idx = outputs.max(1)
    idx = idx.data.numpy()
    # result_str = [idx2char[c] for c in idx.squeeze()]
    # print("epoch: %d, loss: %1.3f" % (epoch + 1, loss.data[0]))
    # print("Predicted string: ", ''.join(result_str))
    print("Predicted string: ", ''.join(idx))

print("Learning finished!")
</code></pre>
<pre><code class="lang-auto"></code></pre>

vdw: <p>This is the problem:</p>
<aside class="quote no-group" data-username="safakhan" data-post="1" data-topic="112123">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/f9ae1b/48.png" class="avatar"> safakhan:</div>
<blockquote>
<p><code>out.reshape(-1, num_classes)</code></p>
</blockquote>
</aside>
<p><code>out</code> has shape of <code>(seq_len, batch, num_directions * hidden_size)</code>. You cannot simply reshape it like you do. It completely messes up you tensor.</p>
<p>By the way, this is a problem as well:</p>
<pre><code>x.view(x.size(0), self.sequence_length, self.input_size)
</code></pre>
<p>See <a href="https://discuss.pytorch.org/t/for-beginners-do-not-use-view-or-reshape-to-swap-dimensions-of-tensors/75524">this post</a>.</p>

safakhan: <p>Hi Chris! Thanks for the reply.</p>
<p>Basically my output tensor in<br>
out, _ = self.rnn(x, h_0)</p>
<p>gives (40,11,31) dimen</p>
<p>which after the line reshape becomes (440,31)</p>
<p>However, my label tensor has the shape (40,1,31),</p>
<p>how do I use them to calculate the loss. My input and label have diff dimensions. I am quite new at this. So, a more detailed reply is appreciated</p>
<p>I know that in CrossEntropyLoss()<br>
Input has dim (N,C)<br>
Target has dim N</p>
<p>How do I change my input and target to be compatible with the loss function while calculating correct loss</p>

vdw: <p>As you seem you go for a basic classification task, I would do the following</p>
<p>Use the only the last hidden state, and not <code>out</code> which gives you the hidden states of each time step</p>
<pre><code class="lang-auto">out, h = self.rnn(x, h_0)
</code></pre>
<p><code>h</code> will have a shape of <code>(num_layers * num_directions, batch, hidden_size)</code>, but since you don’t use a bidirectional RNN, it’s just <code>(num_layers, batch, hidden_size)</code> So to get the hidden state if the last layer you just need</p>
<pre><code class="lang-auto">h = h[-1]  # Consider only the last layer
</code></pre>
<p>Now the shape of <code>h</code> is <code>(batch, hidden_size)</code>. Usually you now push this through at least one linear layer, say, <code>self.fc = nn.Linear(hidden_size, num_classes</code>:</p>
<pre><code class="lang-auto">output = self.fc(h)
</code></pre>
<p><code>output</code> will have the shape <code>(batch, num_classes)</code>, what is most likely what you want.</p>
<p>You probably still have to fix</p>
<pre><code class="lang-auto">x.view(x.size(0), self.sequence_length, self.input_size)
</code></pre>
<p>using <code>permute()</code> or <code>transpose()</code></p>

safakhan: <p>Well thakyou Chris. Your answer helped alot <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>