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
+44
View File
@@ -1593,6 +1593,40 @@ TEST_F(ModulesTest, TripletMarginLoss) {
ASSERT_EQ(anchor.sizes(), anchor.grad().sizes());
}
TEST_F(ModulesTest, NLLLoss) {
NLLLoss loss;
auto input = torch::tensor({{-0.1315, -3.1315, -2.5315},
{-3.7038, -0.1038, -2.6038},
{-2.3422, -1.3422, -0.4422}},
torch::dtype(torch::kFloat).requires_grad(true));
auto target = torch::tensor({1, 0, 2}, torch::kLong);
auto output = loss->forward(input, target);
auto expected = torch::tensor(2.4258, torch::kFloat);
auto s = output.sum();
s.backward();
ASSERT_TRUE(output.allclose(expected, 1e-04));
ASSERT_TRUE(
NLLLoss(NLLLossOptions().ignore_index(-100).reduction(torch::kMean))
->forward(input, target).allclose(expected, 1e-04));
}
TEST_F(ModulesTest, CrossEntropyLoss) {
CrossEntropyLoss loss;
auto input = torch::tensor({{3., 3.}, {2., 2.}}, torch::dtype(torch::kFloat).requires_grad(true));
auto target = torch::tensor({0, 1}, torch::kLong);
auto output = loss->forward(input, target);
auto expected = torch::tensor(0.6931, torch::kFloat);
auto s = output.sum();
s.backward();
ASSERT_TRUE(output.allclose(expected, 1e-04));
ASSERT_EQ(input.sizes(), input.grad().sizes());
ASSERT_TRUE(
CrossEntropyLoss(CrossEntropyLossOptions().ignore_index(-100).reduction(torch::kMean))
->forward(input, target).allclose(expected, 1e-04));
}
TEST_F(ModulesTest, CosineSimilarity) {
CosineSimilarity cos(CosineSimilarityOptions().dim(1));
auto input1 = torch::tensor({{1, 2, 3}, {4, 5, 6}}, torch::dtype(torch::kFloat).requires_grad(true));
@@ -2988,6 +3022,16 @@ TEST_F(ModulesTest, PrettyPrintTripletMarginLoss) {
"torch::nn::TripletMarginLoss(margin=3, p=2, eps=1e-06, swap=false)");
}
TEST_F(ModulesTest, PrettyPrintNLLLoss) {
ASSERT_EQ(
c10::str(NLLLoss()), "torch::nn::NLLLoss()");
}
TEST_F(ModulesTest, PrettyPrinCrossEntropyLoss) {
ASSERT_EQ(
c10::str(CrossEntropyLoss()), "torch::nn::CrossEntropyLoss()");
}
TEST_F(ModulesTest, PrettyPrintMultiLabelMarginLoss) {
ASSERT_EQ(c10::str(MultiLabelMarginLoss()), "torch::nn::MultiLabelMarginLoss()");
}