2D input get mat1 dim 1 must match mat2 dim 0

username: aysuda
cooked: <p>I have a PPO agent and give it a tensor of size (1, 128, 100) as input (state).<br>
The state of the environment is a numpy array (128, 100) and then convert to tensor.<br>
The neural network:<br>
state_dim = env.observation_space.shape[0]  <span class="hashtag">#128</span><br>
hidden_dim = 64<br>
action_dim = 20 (discrete)</p>
<pre><code class="lang-auto">class Actor(nn.Module):
    def __init__(self, state_dim, hidden_dim, action_dim):
        super(Actor, self).__init__()

        self.action_layer = nn.Sequential(
            nn.Linear(state_dim, hidden_dim),
            nn.Relu(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.Relu(),
            nn.Linear(hidden_dim, action_dim),
            nn.Softmax(dim=-1)).float().to(device)

    def forward(self, state):
        action_probs = self.action_layer(state)
        return action_probs
</code></pre>
<p>I got this error:</p>
<pre><code class="lang-auto">File "main.py", line 167, in &lt;module&gt;                                                                                                                                     PPO.main(args) action_probs = self.actor(state)                                                                                                                                      File "Anaconda\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl  result = self.forward(*input, **kwargs)                                                                                                                               File "PPO.py", line 125, in forward   action_probs = self.action_layer(state)                                                                                                                               File "Anaconda\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl   result = self.forward(*input, **kwargs)                                                                                                                               File "Anaconda\lib\site-packages\torch\nn\modules\container.py", line 119, in forward      input = module(input)                                                                                                                                                 File "Anaconda\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl   result = self.forward(*input, **kwargs)                                                                                                                               File "Anaconda\lib\site-packages\torch\nn\modules\linear.py", line 94, in forward                                                                                      return F.linear(input, self.weight, self.bias)                                                                                                                        File "Anaconda\lib\site-packages\torch\nn\functional.py", line 1753, in linear                                                                                         return torch._C._nn.linear(input, weight, bias)                                                                                                                     RuntimeError: mat1 dim 1 must match mat2 dim 0
</code></pre>
<p>Any advice?<br>
Thanks.</p>

username: KFrank
cooked: <p>Hi Aysuda!</p>
<aside class="quote no-group quote-modified" data-username="aysuda" data-post="1" data-topic="122546" data-full="true">
<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/a/bbe5ce/48.png" class="avatar"> aysuda:</div>
<blockquote>
<p>I have a PPO agent and give it a tensor of size (1, 128, 100) as input (state).<br>
The state of the environment is a numpy array (128, 100) and then convert to tensor.<br>
…<br>
state_dim = env.observation_space.shape[0]  <span class="hashtag-raw">#128</span></p>
<pre><code class="lang-auto">        self.action_layer = nn.Sequential(
            nn.Linear(state_dim, hidden_dim),
            ...
</code></pre>
</blockquote>
</aside>
<p>It’s not clear to me what you’re passing into your model.</p>
<p>Please check the <code>shape</code> of the <code>state</code> that gets passed into your<br>
instance of <code>Actor</code> (so then into <code>action_layer</code>).  <code>state</code> must be<br>
a tensor of shape <code>[nBatch, 128]</code>; it can’t be a 3-d tensor nor a<br>
tensor of shape <code>[128, some_other_dim]</code>.</p>
<p>[Edit:  To clarify:  <code>state</code> could be a 3-d tensor, just not of shape<br>
<code>[1, 128, 100]</code>.  Examples of legal 3-d shapes would be<br>
<code>[1, 100, 128]</code> and <code>[100, 17, 128]</code>.  The requirement is that<br>
the trailing dimension match the value of <code>in_features = 128</code><br>
of the <code>Linear</code>.]</p>
<p>Best.</p>
<p>K. Frank</p>

username: aysuda
cooked: <p>Hi and Thanks for your reply<br>
The state is a numpy array (128, 100) ,and the shape of tensor I’m passing to <em>action_layer</em> is <em>torch.Size([1, 128, 100])</em>.  So the input must be [1, 128]?<br>
How do I do this? With Flatten I got this: torch.Size([1, 12800]) and same error.<br>
With torch.rand(1, 128, 100)[ : , : , -1] I got this: torch.Size([1, 128]) but first state is a (128, 100) array and I think it’s a lot of data lost. Am I wrong?</p>

username: KFrank
cooked: <p>Hi Aysuda!</p>
<aside class="quote no-group" data-username="aysuda" data-post="3" data-topic="122546" data-full="true">
<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/a/bbe5ce/48.png" class="avatar"> aysuda:</div>
<blockquote>
<p>The state is a numpy array (128, 100)</p>
</blockquote>
</aside>
<p>What is the meaning of the value <code>128</code> and the value <code>100</code>?</p>
<p><code>Linear (in_features = 128, out_features = 64)</code> will apply<br>
an affine transformation to a batch of input vectors of length <code>128</code> to<br>
produce a  batch of output vectors of length <code>64</code>.  Is your dimension<br>
of length <code>128</code> the dimension to which you want to apply such a<br>
transformation?</p>
<aside class="quote no-group">
<blockquote>
<p>So the input must be [1, 128]?</p>
</blockquote>
</aside>
<p>No, your input doesn’t have to have shape <code>[1, 128]</code> (although<br>
this would be a valid shape for your input).  But the trailing dimension<br>
of your input  <em>does</em> have to be <code>128</code> so that it will match the value<br>
of <code>in_features</code> in your <code>Linear</code>.</p>
<p>In the typical use case input will have shape <code>[nBatch, 128]</code>, where<br>
<code>nBatch</code> is the number of samples in your batch.</p>
<p>(A <code>Linear</code>, however, can accept multiple “batch” dimensions, so a<br>
tensor of shape <code>[nBatch, nSomethingElse, 128]</code> would be a valid<br>
input for a <code>Linear</code> with <code>in_features = 128</code>.)</p>
<p>Best.</p>
<p>K. Frank</p>