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

Smruti_Prava_Dash: <p>I am trying to generate time series data using convolutional GAN.<br>
My model is here:<br>
seq_size = batch_size<br>
n_features = X2_scaled.shape[2]</p>
<h1><a name="discriminator-1" class="anchor" href="#discriminator-1"></a>Discriminator</h1>
<p>D = nn.Sequential(</p>
<pre><code>nn.Conv1d(32, 64, kernel_size=4, stride=2,padding=2,dilation=1),#32
nn.BatchNorm2d(64),
nn.ReLU(),

nn.Conv1d(64, 64, kernel_size=3, stride=2, padding=1,dilation=2),
nn.BatchNorm2d(128),
nn.ReLU(),

nn.Conv1d(64, 32, kernel_size=1, stride=2, padding=0,dilation=4),
# nn.BatchNorm2d(4),
nn.Sigmoid()    
</code></pre>
<p>)</p>
<h1><a name="generator-2" class="anchor" href="#generator-2"></a>Generator</h1>
<p>G = nn.Sequential(<br>
nn.Linear(4, 7),# as z is of (latent_dim,4)</p>
<pre><code>nn.ConvTranspose1d(7, 64, kernel_size=3, stride=1,padding=1),
nn.BatchNorm1d(64),
nn.ReLU(),

nn.ConvTranspose1d(64, 64, kernel_size=2, stride=1,padding=1),
nn.BatchNorm1d(64),
nn.ReLU(),
        
nn.ConvTranspose1d(64, 32, kernel_size=1, stride=1,padding=1),
# nn.BatchNorm1d(4),
nn.Tanh(),
)
</code></pre>
<p>D = D.to(device)<br>
G = G.to(device)</p>
<h1><a name="training-3" class="anchor" href="#training-3"></a>training</h1>
<p>total_step = len(data_loader)<br>
for epoch in range(num_epochs):<br>
for i, (samples) in enumerate(data_loader):<br>
samples = samples.reshape(batch_size, -1).to(device)<br>
print(samples.shape) # giving original input shape (568,32,4)<br>
 →    outputs = D(samples.float())</p>
<p>Here I am getting the runtime error: Given groups=1, weight of size [64, 32, 4], expected input[1, 64, 128] to have 32 channels, but got 64 channels instead.<br>
I am unable to see my mistake here. please help me. Thanks in advance</p>

ptrblck: <p>The shape in this comment</p>
<aside class="quote no-group" data-username="Smruti_Prava_Dash" data-post="1" data-topic="191908">
<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/smruti_prava_dash/48/65498_2.png" class="avatar"> Smruti_Prava_Dash:</div>
<blockquote>
<p>samples = samples.reshape(batch_size, -1).to(device)<br>
print(samples.shape) # giving original input shape (568,32,4)</p>
</blockquote>
</aside>
<p>cannot be true, since you are reshaping the tensor to two dimensions only.</p>
<p><code>nn.Conv1d</code> expects a batched 3D tensor in the shape <code>[batch_size, channels, seq_len]</code> or an unbatched 2D tensor in the shape <code>[channels, seq_len]</code>. In the latter case the layer will <code>unsqueeze</code> the tensor’s missing batch dimension and will set a sample size of <code>1</code> to it.<br>
I would generally recommend unsqueezing the batch dimension explicitly, as I don’t think this “flexibility” of accepting inputs without a batch dimension is a well designed feature, which mainly creates confusion.<br>
After fixing this issue you would most likely run into a new one since you are using <code>nn.BatchNorm2d</code> while I assume it should be <code>nn.BatchNorm1d</code>.</p>

Smruti_Prava_Dash: <p>Yes when I was not reshaping it, was showing ValueError: expected 4D input (got 3D input).<br>
Now I see why it was there. Had a serious mistake by taking BatchNorm2d().<br>
Thank you so so much. Now I don’t get the same error.</p>