libtorch forward result unexpected

27 Views Asked by At

I use a small net. The net converges and the accuracy is 1.0 after 400 iterations. So far, so good.

conv1(torch::nn::Conv2dOptions(1, 15, /*kernel_size=*/3)),
    conv2(torch::nn::Conv2dOptions(15, 30, /*kernel_size=*/3)),
    conv3(torch::nn::Conv2dOptions(30, 60, /*kernel_size=*/3)),
    conv4(torch::nn::Conv2dOptions(60, 120, /*kernel_size=*/3)),
    conv5(torch::nn::Conv2dOptions(120, 240, /*kernel_size=*/3)),
    fc1(240, 2)

I expected the result tensor after forward to hold two values, but is shows 240.

 x = torch::relu(torch::max_pool2d(conv1->forward(x), 2)); // 15 x 86x86
    x = torch::relu(torch::max_pool2d(conv2->forward(x), 2)); // 30 x 42x42
    x = torch::relu(torch::max_pool2d(conv3->forward(x), 2)); // 60 x 20x20
    x = torch::relu(torch::max_pool2d(conv4->forward(x), 2)); // 120 x 9x9
    x = torch::relu(conv5->forward(x)); // 240 x 7x7
    x = torch::avg_pool2d(x, 7); // 240 x 1
    //x = torch::mean(x); // 240 x 1
    // flatten
    x = x.view({ -1, 240 });
    fc1->forward(x);
    x= torch::log_softmax(x, /*dim=*/1);
    return x;

If I inspect the tensor

BYTE* dataptr = (BYTE*)x.data_ptr();

I get values like 5, 170..., nothing I would expect after softmax.

How to get the raw output of forward?

0

There are 0 best solutions below