RuntimeError: Given groups=1, weight of size [128, 1, 5], expected input[2, 32, 187] to have 1 channels, but got 32 channels instead

hs99: <p>My CNN model is as follows:</p>
<p>class ConvNormPool(nn.Module):<br>
“”“Conv Skip-connection module”""<br>
def <strong>init</strong>(<br>
self,<br>
input_size,<br>
hidden_size,<br>
kernel_size,<br>
norm_type=‘bachnorm’<br>
):<br>
super().<strong>init</strong>()</p>
<pre><code>    self.kernel_size = kernel_size
    self.conv_1 = nn.Conv1d(
        in_channels=input_size,
        out_channels=hidden_size,
        kernel_size=kernel_size
    )
    self.conv_2 = nn.Conv1d(
        in_channels=hidden_size,
        out_channels=hidden_size,
        kernel_size=kernel_size
    )
    self.conv_3 = nn.Conv1d(
        in_channels=hidden_size,
        out_channels=hidden_size,
        kernel_size=kernel_size
    )
    self.swish_1 = Swish()
    self.swish_2 = Swish()
    self.swish_3 = Swish()
    if norm_type == 'group':
        self.normalization_1 = nn.GroupNorm(
            num_groups=8,
            num_channels=hidden_size
        )
        self.normalization_2 = nn.GroupNorm(
            num_groups=8,
            num_channels=hidden_size
        )
        self.normalization_3 = nn.GroupNorm(
            num_groups=8,
            num_channels=hidden_size
        )
    else:
        self.normalization_1 = nn.BatchNorm1d(num_features=hidden_size)
        self.normalization_2 = nn.BatchNorm1d(num_features=hidden_size)
        self.normalization_3 = nn.BatchNorm1d(num_features=hidden_size)
        
    self.pool = nn.MaxPool1d(kernel_size=2)
    
def forward(self, input):
    #print("INPUT SHAPE SKIP")
    #print(input.shape)
    conv1 = self.conv_1(input)
    x = self.normalization_1(conv1)
    x = self.swish_1(x)
    x = F.pad(x, pad=(self.kernel_size - 1, 0))
    
    x = self.conv_2(x)
    x = self.normalization_2(x)
    x = self.swish_2(x)
    x = F.pad(x, pad=(self.kernel_size - 1, 0))
    
    conv3 = self.conv_3(x)
    x = self.normalization_3(conv1+conv3)
    x = self.swish_3(x)
    x = F.pad(x, pad=(self.kernel_size - 1, 0))   
    
    x = self.pool(x)
    return x
</code></pre>
<hr>
<p>class CNN(nn.Module):<br>
def <strong>init</strong>(<br>
self,<br>
input_size = 1,<br>
hid_size = 256,<br>
kernel_size = 5,<br>
num_classes = 5,<br>
):</p>
<pre><code>    super().__init__()
    
    self.conv1 = ConvNormPool(
        input_size=input_size,
        hidden_size=hid_size,
        kernel_size=kernel_size,
    )
    self.conv2 = ConvNormPool(
        input_size=hid_size,
        hidden_size=hid_size//2,
        kernel_size=kernel_size,
    )
    self.conv3 = ConvNormPool(
        input_size=hid_size//2,
        hidden_size=hid_size//4,
        kernel_size=kernel_size,
    )
    self.avgpool = nn.AdaptiveAvgPool1d((1))
    self.fc = nn.Linear(in_features=hid_size//4, out_features=num_classes)
    ***self.softmax = nn.LogSoftmax(dim=1)***
    
def forward(self, input):
    #print("INPUT SHAPE is ")
    #print(input.shape)
    #print("INPUT SHAPE is ")
    #print(input.shape)
    #input = input.unsqueeze(1)
    #input = input.unsqueeze(0)
    x = self.conv1(input)
    x = self.conv2(x)
    x = self.conv3(x)
    x = self.avgpool(x)        
    # print(x.shape) # num_features * num_channels log_
    x = x.view(-1, x.size(1) * x.size(2)) 
    x = F.softmax(self.fc(x), dim=1) #x = F.softmax(self.fc(x), dim=1) x = F.log_softmax(self.fc(x), dim=1)
    return x
</code></pre>
<p>m1=CNN(m1=CNN(num_classes=5, hid_size=128).to(device)<br>
n I call summary(m1,(32,187))<br>
It gives error <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"><br>
RuntimeError: Given groups=1, weight of size [128, 1, 5], expected input[2, 32, 187] to have 1 channels, but got 32 channels instead</p>
<p>How Can I remove this??</p>
<p>However when I use:<br>
summary(m1, (1,187),32):<br>
I get summary as:</p>
<h1>Layer (type)               Output Shape         Param #</h1>
<pre><code>        Conv1d-1             [32, 128, 183]             768
   BatchNorm1d-2             [32, 128, 183]             256
         Swish-3             [32, 128, 183]               0
        Conv1d-4             [32, 128, 183]          82,048
   BatchNorm1d-5             [32, 128, 183]             256
         Swish-6             [32, 128, 183]               0
        Conv1d-7             [32, 128, 183]          82,048
   BatchNorm1d-8             [32, 128, 183]             256
         Swish-9             [32, 128, 183]               0
    MaxPool1d-10              [32, 128, 93]               0
 ConvNormPool-11              [32, 128, 93]               0
       Conv1d-12               [32, 64, 89]          41,024
  BatchNorm1d-13               [32, 64, 89]             128
        Swish-14               [32, 64, 89]               0
       Conv1d-15               [32, 64, 89]          20,544
  BatchNorm1d-16               [32, 64, 89]             128
        Swish-17               [32, 64, 89]               0
       Conv1d-18               [32, 64, 89]          20,544
  BatchNorm1d-19               [32, 64, 89]             128
        Swish-20               [32, 64, 89]               0
    MaxPool1d-21               [32, 64, 46]               0
 ConvNormPool-22               [32, 64, 46]               0
       Conv1d-23               [32, 32, 42]          10,272
  BatchNorm1d-24               [32, 32, 42]              64
        Swish-25               [32, 32, 42]               0
       Conv1d-26               [32, 32, 42]           5,152
  BatchNorm1d-27               [32, 32, 42]              64
        Swish-28               [32, 32, 42]               0
       Conv1d-29               [32, 32, 42]           5,152
  BatchNorm1d-30               [32, 32, 42]              64
        Swish-31               [32, 32, 42]               0
    MaxPool1d-32               [32, 32, 23]               0
 ConvNormPool-33               [32, 32, 23]               0
</code></pre>
<h1>AdaptiveAvgPool1d-34                [32, 32, 1]               0<br>
Linear-35                    [32, 5]             165</h1>
<h2>Total params: 269,061<br>
Trainable params: 269,061<br>
Non-trainable params: 0</h2>
<h2>Input size (MB): 0.02<br>
Forward/backward pass size (MB): 74.56<br>
Params size (MB): 1.03<br>
Estimated Total Size (MB): 75.61</h2>
<p>IS MY MODEL CORRECT??<br>
HAVE I PUT ALL LAYERS IN PLACE?<br>
How do I corect the 1st error?</p>
<p>ALSO, <em><strong>self.softmax = nn.LogSoftmax(dim=1)</strong></em> needs to be inserted??I am using CROSSENTROPYLOSS</p>

Dwight_Foster: <p>Just change your input size to 32.</p>