LSTM error: AttributeError: 'tuple' object has no attribute 'dim'
I have the following code:
import torch
import torch.nn as nn

model = nn.Sequential(
          nn.LSTM(300, 300),
          nn.Linear(300, 100),
          nn.ReLU(),
          nn.Linear(300, 7),
          )

s = torch.ones(1, 50, 300)
a = model(s)

And I get:
My-MBP:Desktop myname$ python3 testmodel.py 
Traceback (most recent call last):
  File &quot;testmodel.py&quot;, line 12, in &lt;module&gt;
    a = model(s)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/container.py&quot;, line 117, in forward
    input = module(input)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/linear.py&quot;, line 93, in forward
    return F.linear(input, self.weight, self.bias)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/functional.py&quot;, line 1688, in linear
    if input.dim() == 2 and bias is not None:
AttributeError: 'tuple' object has no attribute 'dim'

Why? The dimensions should be fine. I saw related fixes to this issue when *input is defined in model.forward, but I don't even have anything implemented yet.
/edit: WAIT, there IS a *input!? How can I override this?

You won't be able to use a nn.RNN inside a nn.Sequential since nn.LSTM layers will output a tuple containing (1) the output features and (2) hidden states and cell states.
The output must first be unpacked in order to use the output features in your subsequent layer: nn.Linear. Something as, if your interested in the hidden states and cell states:
rnn = nn.LSTM(300, 300)
output, (h_n, c_n) = rnn(x)

You could define a custom nn.Module and implement a simple forward function:
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        self.rnn = nn.LSTM(300, 300)
        
        self.body = nn.Sequential(
          nn.Linear(300, 100),
          nn.ReLU(),
          nn.Linear(100, 7)) # &lt;- had it set to in_features=300

    def forward(self, x):
        x, _ = self.rnn(x) # &lt;- ignore second output
        x = self.body(x)
        return x

Such that:
&gt;&gt;&gt; model = Model()
&gt;&gt;&gt; s = torch.ones(1, 50, 300)

&gt;&gt;&gt; model(s).shape
torch.Size([1, 50, 7])

