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

**URL:** <https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-of-size-128-1-5-expected-input-2-32-187-to-have-1-channels-but-got-32-channels-instead/116068>\
**Category:** vision\
**Created:** [March 26, 2021, 11:15am UTC](https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-of-size-128-1-5-expected-input-2-32-187-to-have-1-channels-but-got-32-channels-instead/116068 "2021-03-26T11:15:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hs99](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hs99/32/36177_2.png) [@hs99](https://discuss.pytorch.org/u/hs99)\
**Post date:** [March 26, 2021, 11:15am UTC](https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-of-size-128-1-5-expected-input-2-32-187-to-have-1-channels-but-got-32-channels-instead/116068/1 "2021-03-26T11:15:59Z")

</div>

My CNN model is as follows:

class ConvNormPool(nn.Module):  
“”“Conv Skip-connection module”""  
def **init** (  
self,  
input\_size,  
hidden\_size,  
kernel\_size,  
norm\_type=‘bachnorm’  
):  
super(). **init** ()

```
    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

```

* * *

class CNN(nn.Module):  
def **init** (  
self,  
input\_size = 1,  
hid\_size = 256,  
kernel\_size = 5,  
num\_classes = 5,  
):

```
    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

```

m1=CNN(m1=CNN(num\_classes=5, hid\_size=128).to(device)  
n I call summary(m1,(32,187))  
It gives error 🙂  
RuntimeError: Given groups=1, weight of size [128, 1, 5], expected input[2, 32, 187] to have 1 channels, but got 32 channels instead

How Can I remove this??

However when I use:  
summary(m1, (1,187),32):  
I get summary as:

# Layer (type) Output Shape Param #

```
        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

```

# AdaptiveAvgPool1d-34 [32, 32, 1] 0 Linear-35 [32, 5] 165

## Total params: 269,061 Trainable params: 269,061 Non-trainable params: 0

## Input size (MB): 0.02 Forward/backward pass size (MB): 74.56 Params size (MB): 1.03 Estimated Total Size (MB): 75.61

IS MY MODEL CORRECT??  
HAVE I PUT ALL LAYERS IN PLACE?  
How do I corect the 1st error?

ALSO, _**self.softmax = nn.LogSoftmax(dim=1)**_ needs to be inserted??I am using CROSSENTROPYLOSS

---

<div class="post-metadata">

**Author:** ![Dwight\_Foster](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dwight_foster/32/18182_2.png) [@Dwight\_Foster](https://discuss.pytorch.org/u/Dwight_Foster)\
**Post date:** [March 26, 2021, 12:55pm UTC](https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-of-size-128-1-5-expected-input-2-32-187-to-have-1-channels-but-got-32-channels-instead/116068/2 "2021-03-26T12:55:56Z")

</div>

Just change your input size to 32.
