Search code examples
pythonnumpypytorch

Convert 'int' to pytorch 'Variable' makes problems


First project with pytorch and I got stuck trying to convert an MNIST label 'int' into a torch 'Variable'. Debugger says it has no dimension?!

# numpy mnist data
X_train, Y_train = read_data("training")
X_test , Y_test  = read_data("testing")

arr = np.zeros(5)
for i in range(5):
    # in your training loop:
    costs_ = 0
    for k in range(10000):
        optimizer.zero_grad()                            # zero the gradient buffers
        a = torch.from_numpy(np.expand_dims(X_train[k].flatten(), axis=0)).float()
        b = torch.from_numpy(np.array(Y_train[k], dtype=np.float)).float()
        input = Variable(a)
        output = net(input)
        target = Variable(b)                             # PROBLEM!!
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()                                 # Does the update

        costs_ += loss.data.numpy()
    arr[i] = costs_ 
    print(i)

Thrown error is: "RuntimeError: input and target have different number of elements: input[1 x 1] has 1 elements, while target[] has 0 elements at /b/wheel/pytorch-src/torch/lib/THNN/generic/MSECriterion.c:12"


Solution

  • The error is telling you exactly what is happening. Your target variable is empty.

    Edit (after the comment below):

    if Y_train[k] = 5, then np.array(Y_train[k], dtype=np.float).shape = (), and in turn Variable(b) becomes a tensor with no dimension.

    In order to fix this you will need to pass a list to np.array() and not a integer or a float.

    Like this:

    b = torch.from_numpy(np.array([Y_train[k]], dtype=np.float)).float()