RuntimeError: input.size(-1) must be equal to input_size

carlosherol: <p>Hi everyone, I</p>
<p>import midi<br>
import os<br>
from midi_to_statematrix import *<br>
import glob<br>
import numpy as np</p>
<p>batch_width = 10 # number of sequences in a batch<br>
batch_len = 16*8 # length of each sequence<br>
division_len = 16 # interval between possible start locations</p>
<p>piece = 0</p>
<p>pieces = loadPieces(“MIDIS_train”)<br>
input = torch.FloatTensor(MusicToTensor(pieces))<br>
lstm_input_size = batch_len<br>
h1 = 128<br>
hidden = torch.zeros(65, n_hidden, 2)<br>
num_train = 2<br>
batch_size = 1<br>
num_epochs = 1000<br>
print(np.shape(input))<br>
print(np.shape(hidden))</p>
<p>def loadPieces(dirpath):</p>
<pre><code>pieces = {}

for fname in os.listdir(dirpath):
    if fname[-4:] not in ('.mid','.MID'):
        continue

    name = fname[:-4]

    tensor = midiToNoteStateMatrix(os.path.join(dirpath, fname))
    if len(tensor) &lt; batch_len:
        continue

    pieces[name] = tensor
    print ("Loaded {}".format(name))

return pieces 
</code></pre>
<p>def MusicToTensor(piece):</p>
<pre><code>dirpath = "MIDIS_train"
pieces = {}

for fname in os.listdir(dirpath):
    if fname[-4:] not in ('.mid','.MID'):
        continue

matrix = midiToNoteStateMatrix(os.path.join(dirpath, fname))
tensor = torch.FloatTensor(matrix) 
return tensor
</code></pre>
<p>import torch.nn as nn</p>
<h1>Here we define our model as a class</h1>
<p>class LSTM(nn.Module):</p>
<pre><code>def __init__(self, input_dim, hidden_dim, batch_size, output_dim=1,
                num_layers=2):
    super(LSTM, self).__init__()
    self.input_dim = input_dim
    self.hidden_dim = hidden_dim
    self.batch_size = batch_size
    self.num_layers = num_layers

    # Define the LSTM layer
    self.lstm = nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers)

    # Define the output layer
    self.linear = nn.Linear(self.hidden_dim, output_dim)

def init_hidden(self):
    # This is what we'll initialise our hidden state as
    return (torch.zeros(self.num_layers, self.batch_size, self.hidden_dim),
            torch.zeros(self.num_layers, self.batch_size, self.hidden_dim))

def forward(self, input):
    # Forward pass through LSTM layer
    # shape of lstm_out: [input_size, batch_size, hidden_dim]
    # shape of self.hidden: (a, b), where a and b both 
    # have shape (num_layers, batch_size, hidden_dim).
    lstm_out, self.hidden = self.lstm(input.view(len(input), self.batch_size, -1))
    
    # Only take the output from the final timetep
    # Can pass on the entirety of lstm_out to the next layer if it is a seq2seq prediction
    y_pred = self.linear(lstm_out[-1].view(self.batch_size, -1))
    return y_pred.view(-1)
</code></pre>
<p>model = LSTM(lstm_input_size, h1, batch_size=num_train, output_dim=output_dim, num_layers=num_layers)</p>
<p>loss_fn = torch.nn.MSELoss(size_average=False)</p>
<p>optimiser = torch.optim.Adam(model.parameters(), lr=learning_rate)</p>
<p>#####################</p>
<h1>Train model</h1>
<p>#####################</p>
<p>import torch.optim as optim</p>
<h1>Device</h1>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<h1>Model instance</h1>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<p>def get_accuracy(logit, target, batch_size):<br>
‘’’ Obtain accuracy for training round ‘’’<br>
corrects = (torch.max(logit, 1)[1].view(target.size()).data == target.data).sum()<br>
accuracy = 100.0 * corrects/batch_size<br>
return accuracy.item()</p>
<p>for epoch in range(num_epochs):  # loop over the dataset multiple times<br>
train_running_loss = 0.0<br>
train_acc = 0.0<br>
model.train()</p>
<pre><code>     # zero the parameter gradients
optimizer.zero_grad()
    
    # reset hidden states
model.hidden = model.init_hidden() 
    
    # get the inputs
inputs = input
inputs = inputs.view(batch_size, 65*78*2) 

    # forward + backward + optimize
outputs = model(inputs)

loss = criterion(outputs)
loss.backward()
optimizer.step()

train_running_loss += loss.detach().item()
train_acc += get_accuracy(outputs, labels, batch_size)
     
model.eval()
print('Epoch:  %d | Loss: %.4f | Train Accuracy: %.2f' 
      %(epoch, train_running_loss / i, train_acc/i))
</code></pre>
<p>The input is a matrix of 78 columns, 65 rows and a vector of 2 integers for each element of the matrix. I have tried to adjust all the dimensions but I got the error:<br>
“RuntimeError: input.size(-1) must be equal to input_size. Expected 128, got 5070”</p>

Mazhar_Shaikh: <p>Could you please post the complete error? Or point to which line is generating the error?</p>