C++ API parity: NLLLoss & CrossEntropyLoss (#29812)

Summary:
Hi yf225 , I have added **NLLLoss and CrossEntropyLoss.**
```

Also, while using log_softmax in cross_entropy_loss, I am getting an error
../caffe2/../torch/csrc/api/include/torch/nn/functional/loss.h:537:63: error: no matching function for call to  log_softmax(const at::Tensor&)’
     const Tensor& log_softmax_input = torch::log_softmax(input);

aten/src/ATen/Functions.h:5551:22: note: candidate: at::Tensor at::log_softmax(const at::Tensor&, int64_t, c10::optional<c10::ScalarType>)
 static inline Tensor log_softmax(const Tensor & self, int64_t dim, c10::optional<ScalarType> dtype) {
                      ^~~~~~~~~~~
aten/src/ATen/Functions.h:5551:22: note:   candidate expects 3 arguments, 1 provided
```

I think the other two parameters should be optional as in python frontend(shown in documentation here at https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.log_softmax ). Rest, there were no errors in build and tests have passed
Pull Request resolved: https://github.com/pytorch/pytorch/pull/29812

Differential Revision: D18548249

Pulled By: yf225

fbshipit-source-id: 2ab350abd2a6f498d4dba2345f51ad87471f3038
This commit is contained in:
PyExtreme
2019-11-16 10:46:30 -08:00
committed by Facebook Github Bot
parent 890a3f8b8d
commit e1d13f4f8b
7 changed files with 346 additions and 2 deletions
+24
View File
@@ -620,6 +620,30 @@ TEST_F(FunctionalTest, TripletMarginLoss) {
ASSERT_TRUE(output.allclose(expected, 1e-04));
}
TEST_F(FunctionalTest, NLLLoss) {
auto input = torch::tensor({{-0.1315, -3.1315, -2.5315},
{-3.7038, -0.1038, -2.6038},
{-2.3422, -1.3422, -0.4422}},
torch::kFloat);
auto target = torch::tensor({1, 0, 2}, torch::kLong);
auto output = F::nll_loss(
input, target, F::NLLLossFuncOptions().ignore_index(-100).reduction(torch::kMean));
auto expected = torch::tensor(2.4258, torch::kFloat);
ASSERT_TRUE(output.allclose(expected, 1e-04));
ASSERT_TRUE(F::nll_loss(input, target).allclose(expected, 1e-04));
}
TEST_F(FunctionalTest, CrossEntropy) {
auto input = torch::tensor({{3., 3.}, {2., 2.}}, torch::kFloat);
auto target = torch::tensor({0, 1}, torch::kLong);
auto output = F::cross_entropy(
input, target, F::CrossEntropyFuncOptions().ignore_index(-100).reduction(torch::kMean));
auto expected = torch::tensor(0.6931, torch::kFloat);
ASSERT_TRUE(output.allclose(expected, 1e-04));
ASSERT_TRUE(F::cross_entropy(input, target).allclose(expected, 1e-04));
}
TEST_F(FunctionalTest, MaxUnpool1d) {
auto x = torch::tensor({{{2, 4, 5}}}, torch::dtype(torch::kFloat).requires_grad(true));
auto indices = torch::tensor({{{1, 3, 4}}}, torch::kLong);