Wrong demotions in CNN architecture

Nia3324: <p>Im currently trying to implement a CNN for a Alpha Zero game player, using pytorch but im getting an error regarding matrices multiplication. My input consists of 3 channels with 10x10 matrices.</p>
<pre><code class="lang-auto">model = Net(10, 10**2+1)
print(summary(model,(3,10,10)))
</code></pre>
<p>giving me the following error:</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-32-0a101a882eb1&gt; in &lt;cell line: 3&gt;()
      1 model = Net(10, 10**2+1)
      2 
----&gt; 3 print(summary(model,(3,10,10)))

9 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py in forward(self, input)
    112 
    113     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 114         return F.linear(input, self.weight, self.bias)
    115 
    116     def extra_repr(self) -&gt; str:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (40x10 and 2x101)
</code></pre>
<p>This is the current architecture:</p>
<pre><code class="lang-auto">def conv3x3(in_planes, out_planes):
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, padding=1)

def conv1x1(in_planes, out_planes):
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, padding=0)


class Net(nn.Module):
    def __init__(self, board_size, action_size, num_resBlocks=20, num_hidden=128):
        super().__init__()
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        # Initial convolution 
        self.startBlock = nn.Sequential(
            conv3x3(3, num_hidden),
            nn.BatchNorm2d(num_hidden),
            nn.ReLU()
        )
        
        # Loop of all 20 Residual Layers
        self.backBone = nn.ModuleList(
            [ResBlock(num_hidden) for i in range(num_resBlocks)]
        )
        
        
        # Outputs expected value of the state 
        self.valueHead = nn.Sequential(
            conv1x1(num_hidden, 1),
            nn.BatchNorm2d(1),
            nn.ReLU(),

            nn.Linear(in_features=1, out_features=num_hidden),
            nn.ReLU(),

            nn.Linear(in_features=num_hidden, out_features=1),
            nn.Tanh()
        )

    
        # Outputs the probabilities of each possible action 
        self.policyHead = nn.Sequential(
            conv1x1(num_hidden, 2),
            nn.BatchNorm2d(2),
            nn.ReLU(),
            nn.Linear(2, out_features=(action_size)),
            nn.Softmax(dim=1)
        )


        self.to(self.device)

    def forward(self, x):
        x = self.startBlock(x)

        for resBlock in self.backBone:
            x = resBlock(x)

        policy = self.policyHead(x)
        value = self.valueHead(x)

        return policy, value
    

class ResBlock(nn.Module):
    def __init__(self, num_hidden):
        super().__init__()
        self.conv1 = conv3x3(num_hidden, num_hidden)
        self.bn1 = nn.BatchNorm2d(num_hidden)
        self.conv2 = conv3x3(num_hidden, num_hidden)
        self.bn2 = nn.BatchNorm2d(num_hidden)
        self.relu = nn.ReLU()

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        # Skip connections
        out = self.relu(out + identity)

        return out
</code></pre>
<p>Thank you so much for your help, i really appreciate it!</p>

J_Johnson: <p>Your error isn’t complete. But what we can tell is that your convolution layers probably aren’t the issue, because it’s getting all the way to the linear classifier, which is giving the error.</p>
<p>The error is a mismatch of sizes.</p>
<aside class="quote no-group quote-modified" data-username="Nia3324" data-post="1" data-topic="195156">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nia3324/48/66853_2.png" class="avatar"> Nia3324:</div>
<blockquote>
<pre><code class="lang-auto">self.policyHead = nn.Sequential(
            conv1x1(num_hidden, 2),
            nn.BatchNorm2d(2),
            nn.ReLU(),
            nn.Linear(2, out_features=(action_size)), #&lt;--- here
            nn.Softmax(dim=1)
        )
</code></pre>
</blockquote>
</aside>
<p>It looks like the above is where the error is coming from. Your layer only takes 2 features, but the size of the data going through your model is (40, 10) by that point. You will need to either change that layer’s input size to match the data going in, or use additional convolutions to bring the size of the features down further.</p>
<p>On a side note, 2 features as input for that last linear layer is woefully insufficient, especially with an action space of 101. Have a look at AlphaZeros code again, especially at the output.</p>