0

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?

3
  • I tried float as output of the Tensor. BYTE was for filling the image data in. But float shows -2.67, +5.34... Commented Feb 5, 2024 at 8:55
  • One mistake, it should be: x= fc1->forward(x); Commented Feb 6, 2024 at 7:23
  • I changed log_softmax to softmax and torch::nll_loss to torch::mse_loss. But that doesn't work. mse_loss asserts. Commented Feb 6, 2024 at 7:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.