New to PyTorch and struggling with tensor shapes

jordanjsg: <p>Hi all,</p>
<p>Im new to PyTorch and neural networks and am having some trouble figuring out tensor shaping.</p>
<p>I have a task to make a neural network that can distingush two intertwined rectangular spirals.</p>
<p>Running x.shape on the provided data gives [2592100, 2] which I am passing to a linear layer, so im not sure how to shape my tensors around this. I have used x = x.view(x.size(0), -1) but this doesnt seem to remedy the issue.</p>
<p>I have seen in another thread (<a href="https://discuss.pytorch.org/t/valueerror-expected-input-batch-size-324-to-match-target-batch-size-4/24498" class="inline-onebox">ValueError: Expected input batch_size (324) to match target batch_size (4)</a>) where the data has 4 variables so I wanted to know if theres a special way to solve this.<br>
Thanks!</p>

jordanjsg: <p>My code for reference:</p>
<pre><code class="lang-auto">class Network(torch.nn.Module):
    def __init__(self, layer, hid):
        super(Network, self).__init__()
        self.layer = layer
        
        self.short = nn.Sequential(
            nn.Linear(2, hid), # Hidden Layer 1
            nn.Tanh(),
            nn.Linear(hid, 2), # Output Layer
            nn.Sigmoid()
        )
        self.long = nn.Sequential(
            nn.Linear(1, hid), # Hidden Layer 1
            nn.Tanh(),
            nn.Linear(hid, hid), # Hidden Layer 2
            nn.Tanh(),
            nn.Linear(hid, 2), # Output Layer
            nn.Sigmoid()
        )
        

    def forward(self, input):
        x = input
        x = x.view(x.size(0), -1)
        
        if (self.layer == 1):
            output = self.short(x)
            return output
        else:
            output = self.long(x)
            return output
</code></pre>

ptrblck: <p>Your model works fine for the mentioned input shape, if <code>layer=1</code> is used:</p>
<pre><code class="lang-python">model = Network(layer=1, hid=16)
x = torch.randn(2592100, 2)
out = model(x)
print(out.shape)
&gt; torch.Size([2592100, 2])

model = Network(layer=0, hid=16)
x = torch.randn(2592100, 1)
out = model(x)
print(out.shape)
&gt; torch.Size([2592100, 2])
</code></pre>
<p>The <code>self.long</code> block expects a single input feature, so an input in the shape <code>[batch_size, 1]</code> would also work.<br>
Since your input has only two dimensions, <code>x.view(x.size(0), -1)</code> won’t change the shape and you could remove it.</p>

jordanjsg: <p>Thanks so much!, so in this case would that be written as,</p>
<pre><code class="lang-auto"> self.short = nn.Sequential(
            nn.Linear(2592100*2, hid), # Hidden Layer 1
            nn.Tanh(),
</code></pre>
<p>and the same input size for self.long?</p>

ptrblck: <p>No, <code>2592100</code> is the batch size and the <code>dim1</code> size defines the input features in your example.<br>
As given in the previous code snippet, your model works fine for an input in the shape <code>[batch_size, 2]</code> assuming the <code>self.short</code> module is used and <code>[batch_size, 1]</code> if the <code>self.long</code> module is used.</p>

jordanjsg: <p>Im sorry if I’m not getting this, Im still encountering errors when trying to run the code, something along the lines of</p>
<pre><code class="lang-auto">RuntimeError: mat1 and mat2 shapes cannot be multiplied (2592100x2 and 2592100x10)
</code></pre>
<p>When running self.long when layer = 2 for example, what exactly would I need to put in for</p>
<pre><code class="lang-auto">nn.Linear(_____, hid), # Hidden Layer 1
</code></pre>
<p>Again sorry if its a stupid question <img src="https://discuss.pytorch.org/images/emoji/apple/sweat.png?v=9" title=":sweat:" class="emoji" alt=":sweat:"></p>

ptrblck: <p>It seems you’ve now changed the <code>in_features</code> of the linear layer in <code>self.long</code> to <code>2592100</code>, which is also wrong.<br>
If your input has a shape of <code>[batch_size, 2]</code>, use <code>nn.Linear(2, hid)</code> for the first linear layer in <code>self.short</code> and <code>self.long</code>.</p>

jordanjsg: <p>That seems to have worked! Cant say how much youve helped me, thank you!</p>