RuntimeError: Expected 4-dimensional input for 4-dimensional weight [256, 1, 3, 3], but got 3-dimensional input of size [64, 1, 786] instead
I'm trying to combine the CausalConv1d with Conv2d as the encoder of my VAE. But I got this error which is produced on Encoder part. The CausalConv1d is implemented by a nn.Conv1d network, So it should only have 3-dimensional weight, but why the error says expected 4-dimensional?  And I have another question, why I can't use a single int but only tuple in Pycharm when I set the &quot;kernel_size&quot;, &quot;stride&quot; etc. parameters in a Convs layer? Although the official document said both int and tuple are valid. Here is the traceback:
Traceback (most recent call last):
  File &quot;training.py&quot;, line 94, in &lt;module&gt;
    training(args)
  File &quot;training.py&quot;, line 21, in training
    summary(model, torch.zeros(64, 1, 784), show_input=True, show_hierarchical=False)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\pytorch_model_summary\model_summary.py&quot;, line 118, in summary
    model(*inputs)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;F:\VAE_reservoir_computing\VAE.py&quot;, line 34, in forward
    mu, log_var = self.encoder.forward(image)
  File &quot;F:\VAE_reservoir_computing\CausalCnn_Reservoir.py&quot;, line 30, in forward
    x = self.conv1d(x.view(x.shape[0], 1, 784))
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\container.py&quot;, line 119, in forward
    input = module(input)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;F:\VAE_reservoir_computing\CausalConv1d.py&quot;, line 21, in forward
    conv1d_out = self.conv1d(x)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\conv.py&quot;, line 263, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File &quot;C:\Anaconda\envs\vae_reservior_computing\lib\site-packages\torch\nn\modules\conv.py&quot;, line 260, in _conv_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [256, 1, 3, 3], but got 3-dimensional input of size [64, 1, 786] instead

Here is my code of the Encoder-Decoder part:
import torch
import torch.nn as nn
from CausalConv1d import CausalConv1d

class CausalReservoirEncoder(nn.Module):
    def __init__(self, in_channels, out_channels, num_filters, z_dim,  *args):
        super(CausalReservoirEncoder, self).__init__()
        self.num_filters = num_filters
        self.z_dim = z_dim
        hidden_filters = num_filters

        self.Conv1d = nn.Sequential(
            CausalConv1d(in_channels,out_channels,kernel_size=(3,3),dilation=1,A=False),
            nn.LeakyReLU()
        )
        for p in self.parameters():
            p.requires_grad = False

        self.encoder = nn.Sequential(
            nn.Conv2d(out_channels, self.num_filters, kernel_size=(4, 4), padding=(1, 1), stride=(2, 2)), # 28x28 -&gt; 14x14
            nn.LeakyReLU(),
            nn.Conv2d(hidden_filters, 2 * hidden_filters, kernel_size=(4, 4), padding=(1, 1), stride=(2, 2)), # 14x14 -&gt; 7x7
            nn.LeakyReLU(),
            nn.Flatten(),
            nn.Linear(2*hidden_filters*7*7, self.z_dim)

        )

    def forward(self, x):
        x = self.Conv1d(x.view(x.shape[0], 1, 784))
        h_e = self.encoder(x.view(x.shape[0], -1, 28, 28))
        mu, log_var = torch.chunk(h_e, 2, dim=1)

        return mu, log_var

class CausalReservoirDecoder(nn.Module):
    def __init__(self, z_dim, out_channels, num_filters, **kwargs):
        super(CausalReservoirDecoder, self).__init__()
        self.z_dim = z_dim
        self.num_filters = num_filters
        hidden_filters = num_filters

        self.linear = nn.Sequential(
            nn.Linear(self.z_dim, 2*hidden_filters*7*7),
            nn.LeakyReLU()
        )
        self.decoder = nn.Sequential(
            nn.ConvTranspose2d(2*hidden_filters, hidden_filters, kernel_size=(4,4), padding=(1,1), stride=(2,2)), #7x7 -&gt; 14x14
            nn.LeakyReLU(),
            nn.ConvTranspose2d(hidden_filters, out_channels, kernel_size=(4,4), padding=(1,1), stride=(2,2)), # 14x14 -&gt; 28x28
            nn.Sigmoid()
        )

    def forward(self, z):
        x = self.linear(z)
        x = x.reshape(x.shape[0], -1, 7, 7)
        x_recon = self.decoder(x)

        return x_recon

Here is the implementation of the CausalConv1d:
import torch.nn as nn
import torch.nn.functional as F

class CausalConv1d(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, dilation, A=False, *args, **kwargs):
        super(CausalConv1d, self).__init__()
        self.kernel_size = kernel_size
        self.dilation = dilation
        self.A = A

        self.padding = (kernel_size[0] - 1) * dilation + A * 1

        self.conv1d = nn.Conv1d(in_channels,out_channels,self.kernel_size,stride=(1,1),padding=(0,0),dilation=dilation,**kwargs)

    def forward(self, x):
        x = F.pad(x, (self.padding, 0))
        conv1d_out = self.conv1d(x)
        if self.A:
            return conv1d_out[:,:,: -1]
        else:
            return conv1d_out

So anyone can give me some suggestions?

I know this may not be intuitive, but when you use a kernel_size with 2-dim (e.g., (3,3)), then your Conv1d has 4-dim weights. Therefore, to solve your issue, you must change from:
CausalConv1d(in_channels,out_channels,kernel_size=(3,3),dilation=1,A=False),

to:
CausalConv1d(in_channels, out_channels, kernel_size=3, dilation=1, A=False),

