[Beginner] My predictions are in the wrong format

im getting a prediction, which makes me question my model. T_T .

I’m getting results/predictions of the form 2280x19, instead of an array of 2280 elements, X_test.size() returns torch.Size([2280, 30])
y_test.size() returns torch.Size([2280])

but my cpu_pred.size() is of torch.Size([2280, 19])

I want the prediction to be of size 2280, so that i can compare with y_test, for validation accuracy.

This is my script:

class DynamicNet(torch.nn.Module):
    def __init__(self, D_in, H, D_out):

        super(DynamicNet, self).__init__()
        self.input_linear = torch.nn.Linear(D_in, H)
#         self.weights_1 = torch.randn(D_in,H)
        self.middle_linear = torch.nn.Linear(H, H)
#         self.weights_2 = torch.randn(H, H)
        self.output_linear = torch.nn.Linear(H, D_out)


    def forward(self, x):

        h_relu = self.input_linear(x).clamp(min=0)
        for _ in range(np.random.randint(0, 3)):
            h_relu = self.middle_linear(h_relu).clamp(min=0)
        y_pred = self.output_linear(h_relu)
        return y_pred
      

N, D_in, H, D_out = 64, 30, 100, 19

# model = torch.nn.Sequential(
#     torch.nn.Linear(D_in, H),
#     torch.nn.ReLU(),
#     torch.nn.Linear(H, D_out),
# )

model = DynamicNet(D_in, H, D_out)
model = model.double()

criterion = torch.nn.CrossEntropyLoss()

learning_rate = 1e-4
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(100):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(train_loader, 0):
        # get the inputs
        inputs, labels = data

        # wrap them in Variable
        inputs, labels = Variable(inputs), Variable(labels)

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.data[0]
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 64))
            running_loss = 0.0

print('Finished Training')

this is how I’m making the predictions,

# pass it through the model
X_test_var = Variable(X_test, volatile=True)
prediction = model(X_test_var)

# get the result out and reshape it
cpu_pred = prediction.cpu()
result = cpu_pred.data.numpy()
# array_res = np.reshape(result, (28,28))
print(result)

I want the result to be a numpy array of shape (2280,). How should I proceed?

Im doing a multiclass classification of 19 classes, and they are not in one_hot_notation.

Neural networks only work with floating point values. How can we predict a class index, then?

Your last layer, output_linear, outputs 19 values; one score for each class. This score is related to the probability that your example is from a particular class.

You need to find the maximum likelihood prediction for each example:

scores = model(X_test_var)
_, preds = torch.max(scores, dim=1)  # please see docs for an explanation

You can then compare preds and y_test.

Thanks Carl, that worked like a charm!