ValueError: expected 2D or 3D input (got 1D input) PyTorch
class VAE(torch.nn.Module):

 def __init__(self, input_size, hidden_sizes, batch_size):

    super(VAE, self).__init__()

    self.input_size = input_size
    self.hidden_sizes = hidden_sizes
    self.batch_size = batch_size
    self.fc = torch.nn.Linear(input_size, hidden_sizes[0])
    self.BN = torch.nn.BatchNorm1d(hidden_sizes[0])
    self.fc1 = torch.nn.Linear(hidden_sizes[0], hidden_sizes[1])
    self.BN1 = torch.nn.BatchNorm1d(hidden_sizes[1])
    self.fc2 = torch.nn.Linear(hidden_sizes[1], hidden_sizes[2])
    self.BN2 = torch.nn.BatchNorm1d(hidden_sizes[2])
    self.fc3_mu = torch.nn.Linear(hidden_sizes[2], hidden_sizes[3])
    self.fc3_sig = torch.nn.Linear(hidden_sizes[2], hidden_sizes[3])

    self.fc4 = torch.nn.Linear(hidden_sizes[3], hidden_sizes[2])
    self.BN4 = torch.nn.BatchNorm1d(hidden_sizes[2])
    self.fc5 = torch.nn.Linear(hidden_sizes[2], hidden_sizes[1])
    self.BN5 = torch.nn.BatchNorm1d(hidden_sizes[1])
    self.fc6 = torch.nn.Linear(hidden_sizes[1], hidden_sizes[0])
    self.BN6 = torch.nn.BatchNorm1d(hidden_sizes[0])
    self.fc7 = torch.nn.Linear(hidden_sizes[0], input_size)

def sample_z(self, x_size, mu, log_var):

     eps = torch.randn(x_size, self.hidden_sizes[-1])
     return(mu + torch.exp(log_var/2) * eps)

 def forward(self, x):

    ###########
    # Encoder #
    ###########

    out1 = self.fc(x)
    out1 = nn.relu(self.BN(out1))
    out2 = self.fc1(out1)
    out2 = nn.relu(self.BN1(out2))
    out3 = self.fc2(out2)
    out3 = nn.relu(self.BN2(out3))
    mu = self.fc3_mu(out3)
    sig = nn.softplus(self.fc3_sig(out3))

    ###########
    # Decoder  #
    ###########

    # sample from the distro
    sample = self.sample_z(x.size(0), mu, sig)
    out4 = self.fc4(sample)
    out4 = nn.relu(self.BN4(out4))
    out5 = self.fc5(out4)
    out5 = nn.relu(self.BN5(out5))
    out6 = self.fc6(out5)
    out6 = nn.relu(self.BN6(out6))
    out7 = nn.sigmoid(self.fc7(out6))

    return(out7, mu, sig)

vae = VAE(input_size, hidden_sizes, batch_size)

vae.eval()

x_sample, z_mu, z_var = vae(X)


The error is:


File "VAE_LongTensor.py", line 200, in &lt;module&gt;    
x_sample, z_mu, z_var = vae(X)      
ValueError: expected 2D or 3D input (got 1D input)



When you build a nn.Module in pytorch for processing 1D signals, pytorch actually expects the input to be 2D: first dimension is the "mini batch" dimension.
Thus you need to add a singleton dimesion to your X:

x_sample, z_mu, z_var = vae(X[None, ...])

