mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
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:
committed by
Facebook Github Bot
parent
72aeafd3d0
commit
98ccae09af
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user