Input.size(-1) must be equal to input_size. Expected 128, got 1

aleemhussam: <p>Before posting a query, check the <a href="https://discuss.pytorch.org/c/faq/30">FAQs</a> - it might already be answered!</p>
<p>Hi everyone,</p>
<p>I am trying to build a bidirectional LSTM encoder decoder to predict RUL for CMAPPS dataset, while executing the code i am getting this error. and i cant figure it out. I have tried batch_first=True but it doesnt work. can anyone help? Here is my code:</p>
<h1>
<a name="coding-utf-8-1" class="anchor" href="#coding-utf-8-1"></a>-<em>- coding: utf-8 -</em>-</h1>
<p>import torch<br>
import torch.nn as nn</p>
<p>device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<p>class Encoder_LSTM(nn.Module):<br>
“”"<br>
Class for implementing the Bidirectional LSTM Encoder Structure.</p>
<pre><code>Parameters
----------
input_dim : int
    No. of features in Input Data    
enc_dim : int
    Hidden Dimension Size for Encoder Cell.
dec_dim : int
    Hidden Dimension Size for Decoder Cell.
ablation : str
    Specifies the type of LSTM Encoder-Decoder architecture.
bidirectional : bool, optional
    If True, implements a Bidirectional LSTM Structure. Else, a Unidirectional LSTM.
    Default : True
    
Returns
-------
output : torch.Tensor
    Encoder Hidden Representations.
hidden : torch.Tensor
    Hidden State of Final Encoder LSTM Cell.
cell : torch.Tensor
    Cell State of Final Encoder LSTM Cell.
    
"""    
def __init__(self, input_dim, enc_dim,dec_dim, num_layers=2, ablation=None, bidirectional=True):     
    super(Encoder_LSTM, self).__init__()
    self.input_dim = input_dim
    self.enc_dim= enc_dim
    self.dec_dim = dec_dim
    self.num_layers = num_layers

    # define LSTM layer
    self.lstm = nn.LSTM(input_size = input_dim, hidden_size = enc_dim, num_layers=num_layers, batch_first=True)          
    self.fc_hidden = nn.Linear(enc_dim*2, dec_dim)
    self.fc_cell = nn.Linear(enc_dim*2, dec_dim)
def forward(self, src):
    output, (hidden,cell) = self.lstm(src)
    hidden = self.fc_hidden(torch.cat((hidden[0:1],hidden[1:2]),dim=2))
    cell = self.fc_cell(torch.cat((cell[0:1],cell[1:2]),dim=2))
    return output, (hidden, cell)        
</code></pre>
<p>class Decoder_LSTM(nn.Module):<br>
“”"<br>
Class for implementing a Unidirectional LSTM Decoder Cell.</p>
<pre><code>Parameters
----------
output_dim : int
    No. of features in Output Data from instantaneous Decoder Cell.\n   
    Default : 1 for RUL.
enc_dim : int
    Hidden Dimension Size for Encoder Cell.
dec_dim : int
    Hidden Dimension Size for Decoder Cell.
    
Returns
-------
final_prediction : torch.Tensor
    Output from LSTM Decoder Cell.
hidden : torch.Tensor
    Hidden State of instantaneous Decoder LSTM Cell.
cell : torch.Tensor
    Cell State of instantaneous Decoder LSTM Cell.
    
"""    
def __init__(self, output_dim, enc_dim, dec_dim):
    super(Decoder_LSTM, self).__init__()
    self.output_dim= output_dim
    self.dec_dim = dec_dim
    self.lstm = nn.LSTM(input_size= enc_dim*2, hidden_size = dec_dim, batch_first=True)
    self.relu = nn.ReLU()
    self.linear = nn.Linear(dec_dim, output_dim) 

def forward(self, dec_input, hidden,cell): 

    output, (hidden,cell) = self.lstm(dec_input, (hidden,cell))
    out = self.relu(output)
    final_prediction = self.linear(out)
                                
    return final_prediction, hidden, cell</code></pre>

aleemhussam: <p>2021-11-14 19:41:27,826] Trial 0 failed because of the following error: RuntimeError(‘input.size(-1) must be equal to input_size. Expected 128, got 1’)<br>
Traceback (most recent call last):<br>
File “/opt/miniconda3/lib/python3.9/site-packages/optuna/study/_optimize.py”, line 213, in _run_trial<br>
value_or_values = func(trial)<br>
File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/main_trial.py”, line 108, in objective<br>
pred_l, actual_l, trn_loss = train_test.training_loop(training_data, model, device,<br>
File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/train_test.py”, line 65, in training_loop<br>
y_pred = model(s, label, use_teacher_forcing)<br>
File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/models.py”, line 135, in forward<br>
output = ablation(src, trg, use_teacher_forcing)<br>
File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/models.py”, line 97, in ablation_1<br>
out, hidden, cell = self.decoder_lstm(decoder_input, hidden, cell)<br>
File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/modules/encoder_decoder.py”, line 91, in forward<br>
output, (hidden,cell) = self.lstm(dec_input, (hidden,cell))<br>
File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/rnn.py”, line 689, in forward<br>
File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/rnn.py”, line 632, in check_forward_args<br>
):<br>
File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/rnn.py”, line 205, in check_input<br>
if self.input_size != input.size(-1):<br>
RuntimeError: input.size(-1) must be equal to input_size. Expected 128, got 1<br>
Traceback (most recent call last):</p>
<p>File “/var/folders/r5/qgh43p4j4dqgtxt1w69n502r0000gn/T/ipykernel_17568/1817966163.py”, line 1, in <br>
runfile(’/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/main_trial.py’, wdir=’/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder’)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/debugpy/_vendored/pydevd/_pydev_bundle/pydev_umd.py”, line 167, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/debugpy/_vendored/pydevd/_pydev_imps/_pydev_execfile.py”, line 25, in execfile<br>
exec(compile(contents + “\n”, file, ‘exec’), glob, loc)</p>
<p>File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/main_trial.py”, line 140, in <br>
study.optimize(objective, n_trials=200)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/optuna/study/study.py”, line 400, in optimize<br>
_optimize(</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/optuna/study/_optimize.py”, line 66, in _optimize<br>
_optimize_sequential(</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/optuna/study/_optimize.py”, line 163, in _optimize_sequential<br>
trial = _run_trial(study, func, catch)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/optuna/study/_optimize.py”, line 264, in _run_trial<br>
raise func_err</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/optuna/study/_optimize.py”, line 213, in _run_trial<br>
value_or_values = func(trial)</p>
<p>File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/main_trial.py”, line 108, in objective<br>
pred_l, actual_l, trn_loss = train_test.training_loop(training_data, model, device,</p>
<p>File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/train_test.py”, line 65, in training_loop<br>
y_pred = model(s, label, use_teacher_forcing)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)</p>
<p>File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/models.py”, line 135, in forward<br>
output = ablation(src, trg, use_teacher_forcing)</p>
<p>File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/models.py”, line 97, in ablation_1<br>
out, hidden, cell = self.decoder_lstm(decoder_input, hidden, cell)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)</p>
<p>File “/Users/hussamaleem/Documents/SEEM Assignment/Shared Temporal Attention augmented LSTM Encoder Decoder/modules/encoder_decoder.py”, line 91, in forward<br>
output, (hidden,cell) = self.lstm(dec_input, (hidden,cell))</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/rnn.py”, line 689, in forward</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/rnn.py”, line 632, in check_forward_args<br>
):</p>
<p>File “/opt/miniconda3/lib/python3.9/site-packages/torch/nn/modules/rnn.py”, line 205, in check_input<br>
if self.input_size != input.size(-1):</p>
<p>RuntimeError: input.size(-1) must be equal to input_size. Expected 128, got 1</p>
<p>This is the complete error.</p>