C++ API parity: at::Tensor::grad

Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/26150

Test Plan: Imported from OSS

Differential Revision: D17427579

Pulled By: pbelevich

fbshipit-source-id: 68d012076aa86dee9f23fad71a2d265d75f56d22
This commit is contained in:
Pavel Belevich
2019-09-18 09:19:00 -07:00
committed by Facebook Github Bot
parent 72aeafd3d0
commit 98ccae09af
6 changed files with 34 additions and 22 deletions
+4 -3
View File
@@ -56,8 +56,9 @@ TEST(NoGradTest, SetsGradModeCorrectly) {
auto y = model->forward(x);
torch::Tensor s = y.sum();
s.backward();
ASSERT_FALSE(model->weight.grad().defined());
// Mimicking python API behavior:
ASSERT_THROWS_WITH(s.backward(),
"element 0 of tensors does not require grad and does not have a grad_fn")
}
struct AutogradTest : torch::test::SeedingFixture {
@@ -70,7 +71,7 @@ struct AutogradTest : torch::test::SeedingFixture {
};
TEST_F(AutogradTest, CanTakeDerivatives) {
z.backward();
z.backward(torch::ones_like(z));
ASSERT_TRUE(x.grad().allclose(y));
}