BatchNorm1d needs 2d input?
I want to fix problem in PyTorch.
I wrote the following code that is learning sine functions as tutorial.

import torch
from torch import nn
from torch import optim
from torch.autograd import Variable as V
from torch.utils.data import TensorDataset, DataLoader
import numpy as np

# y=sin(x1)
numTrain = 512
numTest = 128
noiseScale = 0.01
PI2 = 3.1415 * 2
X_train = np.random.rand(numTrain,1) * PI2
y_train = np.sin(X_train) + np.random.randn(numTrain,1) * noiseScale + 1.5
X_test  = np.random.rand(numTest,1) * PI2
y_test  = np.sin(X_test) + np.random.randn(numTest,1) * noiseScale

# Construct DataSet
X_trainT = torch.Tensor(X_train)
y_trainT = torch.Tensor(y_train)
X_testT = torch.Tensor(X_test)
y_testT = torch.Tensor(y_test)
ds_train = TensorDataset(X_trainT, y_trainT)
ds_test = TensorDataset(X_testT, y_testT)

# Construct DataLoader
loader_train = DataLoader(ds_train, batch_size=64, shuffle=True)
loader_test = DataLoader(ds_test, batch_size=64, shuffle=False)

# Construct network
net = nn.Sequential(
    nn.Linear(1,10),
    nn.ReLU(),
    nn.BatchNorm1d(10),
    nn.Linear(10,5),
    nn.ReLU(),
    nn.BatchNorm1d(5),
    nn.Linear(5,1),
)
optimizer = optim.Adam(net.parameters())
loss_fn = nn.SmoothL1Loss()

# Training
losses = []
net.train()
for epoc in range(100):
    for data, target in loader_train:
        y_pred = net(data)
        loss = loss_fn(target,y_pred)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        losses.append(loss.data)


# evaluation
%matplotlib inline
from matplotlib import pyplot as plt

#plt.plot(losses)
plt.scatter(X_train, y_train)

net.eval()
sinsX = []
sinsY = []
for t in range(128):
    x = t/128 * PI2
    output = net(V(torch.Tensor([x])))
    sinsX.append(x)
    sinsY.append(output.detach().numpy())
plt.scatter(sinsX,sinsY)


Training is done without error, But the next line caused an error, "expected 2D or 3D input (got 1D input)"

output = net(V(torch.Tensor([x])))


This error doesn't occur if it is without BatchNorm1d().
I feel strange because the input is 1D.

How to fix it?

Thanks.

Update: How did I fix

arr = np.array([x])
output = net(V(torch.Tensor(arr[None,...])))


When working with 1D signals, pyTorch actually expects a 2D tensors: the first dimension is the "mini-batch" dimension. Therefore, you should evaluate your net on a batch with one 1D signal: 

output - net(V(torch.Tensor([x[None, ...]]))


Make sure you set your net to "eval" mode before evaluating it:

net.eval()

