Conv1d: Expected 3-dimensional input for 3-dimensional weight, but got 2-dimensional input instead

The following code results in an error when I try to use a 1d tensor:

import math
import torch
import torch.nn as nn


class GaussianBlur(nn.Module):
    def __init__(self, input, k_size, sigma):
        super(GaussianBlur, self).__init__()
        
        if input.dim() == 4:
            d_val = 2
        elif input.dim() == 2:
            d_val = 1
            
        self.groups = input.size(1)
        k_size, sigma = [k_size] * d_val, [sigma] * d_val
        kernel = 1
        
        meshgrid_tensor = torch.meshgrid([torch.arange(size, dtype=torch.float32 ) for size in k_size])
        
        for size, std, mgrid in zip(k_size, sigma, meshgrid_tensor):
            kernel *= 1 / (std * math.sqrt(2 * math.pi)) * \
            torch.exp(-((mgrid - ((size - 1) / 2)) / std) ** 2 / 2)

        kernel = (kernel / torch.sum(kernel)).view(1, 1, * kernel.size())
        kernel = kernel.repeat(self.groups, * [1] * (kernel.dim() - 1))
        
        self.register_buffer('weight', kernel)
        
        if input.dim() == 4:
            self.conv = torch.nn.functional.conv2d
        elif input.dim() == 2:
            self.conv = torch.nn.functional.conv1d

    def forward(self, input, pad_mode='reflect'):
        if input.dim() > 2:
            input = torch.nn.functional.pad(input, (2, 2, 2, 2), mode=pad_mode)
        input = self.conv(input, weight=self.weight, groups=self.groups)
        return input
		
		
# Works
input = torch.randn(3,64,78).unsqueeze(0)	
gauss_blur = GaussianBlur(input.clone(), 5, 3)
x = gauss_blur(input)

# Doesn't work
input = torch.randn(78).unsqueeze(0)	
gauss_blur = GaussianBlur(input.clone(), 5, 8)
x = gauss_blur(input)

Results in this error message:

Traceback (most recent call last):
  File "f.py", line 57, in <module>
    x = gauss_blur(input)
  File "...", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "f.py", line 42, in forward
    input = self.conv(input, weight=self.weight, groups=self.groups)
RuntimeError: Expected 3-dimensional input for 3-dimensional weight 78 1 5, but got 2-dimensional input of size [1, 78] instead

How do I resolve this issue? The input is an output from nn.Linear()

I think that I can fix it like this:

class GaussianBlur(nn.Module):
    def __init__(self, input, k_size, sigma):
        super(GaussianBlur, self).__init__()
        
        if input.dim() == 4:
            d_val = 2
            self.groups = input.size(1)
        elif input.dim() == 2:
            d_val = 1
            self.groups = input.size(0)			
            
        k_size, sigma = [k_size] * d_val, [sigma] * d_val
        kernel = 1
        
        meshgrid_tensor = torch.meshgrid([torch.arange(size, dtype=torch.float32, \
        device=input.get_device()) for size in k_size])
        
        for size, std, mgrid in zip(k_size, sigma, meshgrid_tensor):
            kernel *= 1 / (std * math.sqrt(2 * math.pi)) * \
            torch.exp(-((mgrid - ((size - 1) / 2)) / std) ** 2 / 2)

        kernel = (kernel / torch.sum(kernel)).view(1, 1, * kernel.size())
        kernel = kernel.repeat(self.groups, * [1] * (kernel.dim() - 1))
        self.register_buffer('weight', kernel)
        
        if d_val == 2:
            self.conv = torch.nn.functional.conv2d
        elif d_val == 1:
            self.conv = torch.nn.functional.conv1d

    def forward(self, input, pad_mode='reflect'):
        d_val = input.dim()
        if input.dim() > 2:
            input = torch.nn.functional.pad(input, (2, 2, 2, 2), mode=pad_mode)
        else:
            input = input.view(1,1,1000)
        input = self.conv(input, weight=self.weight, groups=self.groups)
        if d_val == 2:
            p1d = nn.ConstantPad1d(2, 0)
            input = p1d(input).view(1,1000)
        return input

Though, I’m not sure if there’s anything wrong with using input.view(1,1,1000) to make a 2 dimension input (size: 1,1000) compatible? It looks like it works in my Laplacian pyramid, but I’m not 100% sure.