Add backward() to Tensor and Variable (#7774)

* Add backward() to Tensor and Variable

* Add at:: in front of Tensor

* Trying to not move optional to appease windows?

* Move implementation into cpp file

* Undo some formatting changes
This commit is contained in:
Peter Goldsborough
2018-05-24 17:31:41 -07:00
committed by GitHub
parent 147cc05cf5
commit 28b1a3852c
19 changed files with 128 additions and 67 deletions
+8 -1
View File
@@ -47,7 +47,7 @@ TEST_CASE("module/zero-grad") {
auto module = Linear(3, 4).build();
auto weight = Var(at::ones(at::CPU(at::kFloat), {8, 3}));
auto loss = module->forward({weight}).front().sum();
backward(loss);
loss.backward();
for (auto& parameter : module->parameters()) {
Variable grad = parameter.second.grad();
REQUIRE(grad.defined());
@@ -169,6 +169,13 @@ TEST_CASE("module/clone") {
}
SECTION("Cloning preserves external references") {
struct TestModel : public CloneableModule<TestModel> {
void reset() override {
weight =
register_parameter("weight", at::ones(at::CPU(at::kFloat), {4, 4}));
}
Variable weight;
};
auto module = TestModule().build();
module->weight.data() += 1;
REQUIRE(pointer_equal(module->weight, module->param("weight")));