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

hs99: <h2><a name="i-get-this-error-nd-i-am-new-to-pytorchpls-help-it-will-be-really-grateful-thanks-in-advance-i-shall-copy-paste-the-error-1" class="anchor" href="#i-get-this-error-nd-i-am-new-to-pytorchpls-help-it-will-be-really-grateful-thanks-in-advance-i-shall-copy-paste-the-error-1"></a>I get this error nd I am new to pytorch…Pls help …It will be really grateful …Thanks in Advance!!!<br>
I SHALL COPY PASTE THE ERROR :</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
4 # specify optimizer<br>
5 optimizer = optim.Adam(model_1.parameters(), lr=0.001)<br>
----&gt; 6 model_1_validation_losses = train_by_model_and_custom_loader(model_1, train_loader_1, valid_loader_1, criterion, optimizer, ‘model_ecg_heartbeat_categorization_1.pt’, num_epochs, train_on_gpu)</p>
<p> in train_by_model_and_custom_loader(model, train_loader, valid_loader, criterion, optimizer, best_model_name, n_epochs, train_on_gpu)<br>
32             optimizer.zero_grad()<br>
33             # forward pass: compute predicted outputs by passing inputs to the model<br>
—&gt; 34             output = model(data.float())<br>
35             <span class="hashtag-raw">#print</span>(“o/pT”)(<br>
36             <span class="hashtag-raw">#print</span>(output.shape)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, input)<br>
33         <span class="hashtag-raw">#input</span> = input.view(32, -1, 187)<br>
34         input = input.unsqueeze(0)<br>
—&gt; 35         x = self.conv1(input)<br>
36         x = self.conv2(x)<br>
37         x = self.conv3(x)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, input)<br>
50<br>
51     def forward(self, input):<br>
—&gt; 52         conv1 = self.conv_1(input)<br>
53         x = self.normalization_1(conv1)<br>
54         x = self.swish_1(x)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)<br>
257                             _single(0), self.dilation, self.groups)<br>
258         return F.conv1d(input, self.weight, self.bias, self.stride,<br>
 → 259                         self.padding, self.dilation, self.groups)<br>
260<br>
261</p>
<p><strong>RuntimeError: Given groups=1, weight of size [128, 1, 5], expected input[1, 32, 187] to have 1 channels, but got 32 channels instead</strong></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/8/683fdf7fc9ab5d70aeabcd2fa113e5bee4240487.png" data-download-href="https://discuss.pytorch.org/uploads/default/683fdf7fc9ab5d70aeabcd2fa113e5bee4240487" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/8/683fdf7fc9ab5d70aeabcd2fa113e5bee4240487.png" alt="image" data-base62-sha1="eSevguNwLQcdBfiagqasGYs0ZfN" width="690" height="403" data-dominant-color="272C32"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">832×486 54.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The following is the  code that I used…I called the model using:</p>
<p>model_1 = CNN(num_classes=5, hid_size=128).to(device)</p>
<p>The model code is:</p>
<p>class ConvNormPool(nn.Module):</p>
<pre><code>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):
    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)
    
def forward(self, input):
    #print("INPUT SHAPE is ")
    #print(input.shape)
    #input = input.view(32, -1, 187)
    #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
    x = x.view(-1, x.size(1) * x.size(2))
    x = F.softmax(self.fc(x), dim=1)
    return x
</code></pre>
<p>def train_by_model_and_custom_loader(model, train_loader, valid_loader, criterion, optimizer, best_model_name, n_epochs, train_on_gpu):<br>
model = model.float()<br>
# move tensors to GPU if CUDA is available<br>
if train_on_gpu:<br>
model.cuda()<br>
valid_loss_min = np.Inf # track change in validation loss<br>
valid_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<pre><code>for epoch in range(1, n_epochs+1):

    # keep track of training and validation loss
    train_loss = 0.0
    valid_loss = 0.0

    ###################
    # train the model #
    ###################
    model.train()
    for data, target in train_loader:
        # move tensors to GPU if CUDA is available
        if train_on_gpu:
            data, target = data.cuda(), target.cuda()
            
            ###print(data)
            ###print(data.shape)
            ###print("FLOAT")
            ######print(data.float())
            ###print(data.float().shape)
            ######print(target)
            ###print(target.shape)
        # clear the gradients of all optimized variables
        optimizer.zero_grad()
        # forward pass: compute predicted outputs by passing inputs to the model
        output = model(data.float())
        #print("o/pT")(
        #print(output.shape)
        # calculate the batch loss
        loss = criterion(output, target)
        # backward pass: compute gradient of the loss with respect to model parameters
        loss.backward()
        # perform a single optimization step (parameter update)
        optimizer.step()
        # update training loss
        train_loss += loss.item()*data.size(0)

    ######################    
    # validate the model #
    ######################
    model.eval()
    for data, target in valid_loader:
        # move tensors to GPU if CUDA is available
        if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        # forward pass: compute predicted outputs by passing inputs to the model
        output = model(data.float())
        # calculate the batch loss
        loss = criterion(output, target)
        # update average validation loss 
        valid_loss += loss.item()*data.size(0)

    # calculate average losses
    train_loss = train_loss/len(train_loader.dataset)
    valid_loss = valid_loss/len(valid_loader.dataset)
    
    valid_losses.append(valid_loss)

    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))

    # save model if validation loss has decreased
    if valid_loss &lt;= valid_loss_min:
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        torch.save(model.state_dict(), best_model_name)
        valid_loss_min = valid_loss
        
return valid_losses
</code></pre>

Dwight_Foster: <p>It is because you are reshaping the image into a different shape that is not compatible. What is the original shape of the data and why are you reshaping it?</p>

nicofish: <p>Your code is a bit hard to follow because you did not paste it correctly. That being said I think I found your error. When you define your CNN module you set your input_size parameter to have a default size of 1 as seen below:</p>
<blockquote>
<p>class CNN(nn.Module):<br>
def <strong>init</strong> (<br>
self,<br>
<strong>input_size = 1,</strong><br>
hid_size = 256,<br>
kernel_size = 5,<br>
num_classes = 5,<br>
):</p>
</blockquote>
<p>Then when you initialize your model:</p>
<aside class="quote no-group" data-username="hs99" data-post="1" data-topic="115726">
<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/hs99/48/36177_2.png" class="avatar"> hs99:</div>
<blockquote>
<p>model_1 = CNN(num_classes=5, hid_size=128).to(device)</p>
</blockquote>
</aside>
<p>you don’t tell it what input_size you want so it uses the default, which is 1.<br>
Hence your model expected 1 input channel in the first conv1d, but it got 32.</p>
<p>Change your CNN initialization to</p>
<blockquote>
<p>model_1 = CNN(num_classes=5, hid_size=128, input_size = 32).to(device)</p>
</blockquote>
<p>hope that helps</p>

hs99: <p><a class="mention" href="/u/nicofish">@nicofish</a>  Thanks…That was one reason due to which I could move to the root of this cause!!<br>
Actually the shape of data from dataloader was not correct<br>
Thank you so much!!</p>
<p>One more thing, could you pls tell me from the above code whether my model design is correct or not?Like all layers in place?<br>
Thanks again in advance!</p>

hs99: <p><a class="mention" href="/u/dwight_foster">@Dwight_Foster</a>  Thanks…That was due to shape of data returned from data loader<br>
Actually the shape of data from dataloader was not correct<br>
Thank you so much!!<br>
Also…I hope my model layers are proper in place …I m pretty new to this stuff</p>

nicofish: <p>superficially everything seems fine to me. Hope you were able to get everything working fine</p>

ss_pheonix: <p>Hi! How did you fix this? I am pretty new at GANS and was wondering if we could have a chat if you are free? I am facing a similar error, (RuntimeError: Given transposed=1, weight of size [35, 16, 1], expected input[32, 16, 2] to have 35 channels, but got 16 channels instead)</p>
<p>noise_size = 35 <span class="hashtag">#z_dim</span> should be the num of features of the input data<br>
batch_size = 32</p>
<p>#features_in=35</p>
<p><span class="hashtag">#output</span> data=[] values between=[0,1]</p>
<p>class Generator(nn.Module):<br>
def <strong>init</strong>(self, features_in, features):<br>
super().<strong>init</strong>()<br>
self.features=features<br>
self.gen = nn.Sequential(<br>
<span class="hashtag">#input</span> is features_in=35, going into the first convolution with kernel_size=1, stride=1,padding=0, bias=False</p>
<pre><code>          nn.Conv1d(features_in, 128, kernel_size=1, stride=2, padding=1, bias=False),
          nn.LeakyReLU(0.2, inplace=True),
         
          nn.Conv1d(128, 64, kernel_size=1, stride=2, padding=1, bias=False),
          nn.BatchNorm1d(64),
          nn.LeakyReLU(0.2, inplace=True),
        
          nn.Conv1d(64,32, kernel_size=1,stride=2, padding=1, bias=False),
          nn.BatchNorm1d(32),
          nn.LeakyReLU(0.2, inplace=True),
         
          nn.Conv1d(32,16, kernel_size=1,stride=2, padding=1, bias=False),
          
          )
     
  def forward(self, x):
      x=torch.unsqueeze(x,2)
      return self.gen(x)
</code></pre>
<p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self,features_in, features):<br>
super().<strong>init</strong>()<br>
self.features=features<br>
self.disc = nn.Sequential(</p>
<pre><code>          nn.ConvTranspose1d(features_in, 16, 1, 2, 1, bias=False),
          nn.BatchNorm1d(16),
          nn.ReLU(True),

          nn.ConvTranspose1d(16, 32, 1, 2, 1, bias=False),
          nn.BatchNorm1d(32),
          nn.ReLU(True),

          nn.ConvTranspose1d(32,64, 1, 2, 1, bias=False),
          nn.BatchNorm1d(64),
          nn.ReLU(True),

          nn.ConvTranspose1d(64,128, 1, 2, 1, bias=False),
          nn.BatchNorm1d(128),
          nn.ReLU(True),

          nn.ConvTranspose1d(64, 1, 1, 2, 1, bias=False),
          # nn.Tanh()
      )

  def forward(self, x):
      return self.disc(x)
</code></pre>
<p>This is my model and below is the dataprocessing for it:</p>
<p>f=open(‘GANS-for-MediSci-main\VLOAD_HIV_dataset_10_years.pickle’,‘rb’)<br>
#f=open(‘dynamic_dataset_cd4_7_years (1).pickle’,‘rb’)<br>
dynamic_dataset=pickle.load(f)</p>
<p><span class="hashtag">#procesing</span> for the vload dataset<br>
n_train_data,n_test_data,train_dataloader,test_dataloader,scaler,pred_samples=to_gan_input(dynamic_dataset,batch_size)</p>
<p><span class="hashtag">#Definition</span> of hyperparameters<br>
num_epochs = 50<br>
lr = 0.01<br>
betas = (0.5, 0.999)<br>
features=8<br>
<span class="hashtag">#we</span> take noise inputs to be able to transform them into training data inputs<br>
#len_seq=35<br>
len_seq=dynamic_dataset.shape[-1]</p>
<p><span class="hashtag">#for</span> 1CNN GAN<br>
generator = Generator(len_seq,features)<br>
optimizer_g = torch.optim.Adam(generator.parameters(), lr=lr, betas=betas)<br>
discriminator = Discriminator(len_seq,features)<br>
optimizer_d = torch.optim.Adam(discriminator.parameters(), lr=lr, betas=betas)<br>
criterion = nn.BCELoss()</p>
<p>“”“TRAIN”“”</p>
<p>running_g_loss=0.0<br>
running_d_loss=0.0<br>
currentStep = 0<br>
g_loss_array = []<br>
d_loss_array = []</p>
<p>TRAINING_AMOUNT = 49 # low to test for now<br>
SAVE_STEPS_AMOUNT = 1000 # testing for now</p>
<p>for epoch in range(num_epochs):<br>
for i, data in enumerate(train_dataloader):<br>
real_samples=data<br>
loss_g, loss_d,acc_d = train_batch(real_samples, generator, discriminator, optimizer_g, optimizer_d,criterion,noise_size)<br>
running_g_loss+=loss_g<br>
running_d_loss+=loss_d<br>
aux=len(train_dataloader)</p>
<pre><code>    if((i+1) % len(train_dataloader)==0):
        g_loss_array.append(running_g_loss/aux)
        d_loss_array.append(running_d_loss/aux)
        print(f"\nEpoch: {epoch+1}/{num_epochs}, batch: {i+1}/{len(train_dataloader)}, G_mean_batch_loss: {running_g_loss/aux}, D_mean_batch_loss: {running_d_loss/aux},D_accu:{acc_d}")
        running_g_loss=0.0
        running_d_loss=0.0
</code></pre>