mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Add C++ torch::nn::CosineEmbeddingLoss (#27345)
Summary: Adds `torch::nn::CosineEmbeddingLoss` module and functional support for the C++ API. Issue: https://github.com/pytorch/pytorch/issues/25883 Reviewer: yf225 Pull Request resolved: https://github.com/pytorch/pytorch/pull/27345 Differential Revision: D17801402 Pulled By: yf225 fbshipit-source-id: 0eabe80d7d36397e6667b331c3fa2f56d7a15962
This commit is contained in:
committed by
Facebook Github Bot
parent
e63bfb7877
commit
3b5d40c339
@@ -161,6 +161,17 @@ TEST_F(FunctionalTest, HingeEmbeddingLoss) {
|
||||
ASSERT_TRUE(output.allclose(expected));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, CosineEmbeddingLoss) {
|
||||
auto input1 = torch::tensor({{2, 3, 4}, {6, 2, 4}});
|
||||
auto input2 = torch::tensor({{2, 3, 5}, {9, 12, 0}});
|
||||
auto target = torch::tensor({1, -1});
|
||||
auto output = F::cosine_embedding_loss(
|
||||
input1, input2, target, CosineEmbeddingLossOptions().margin(0.5));
|
||||
auto expected = torch::tensor({0.1004}, torch::kFloat);
|
||||
|
||||
ASSERT_TRUE(output.allclose(expected, 1e-4));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, MaxUnpool1d) {
|
||||
auto x = torch::tensor({{{2, 4, 5}}}, torch::requires_grad());
|
||||
auto indices = torch::tensor({{{1, 3, 4}}}, torch::kLong);
|
||||
|
||||
Reference in New Issue
Block a user