From 148118a34e0e2dae7a50846ef1bdbfb37a95b809 Mon Sep 17 00:00:00 2001 From: Iblis Lin Date: Sat, 7 Oct 2017 23:59:15 +0800 Subject: [PATCH] autograd: rename function - grad() -> getgrad() - get_symbol() -> getsymbol() --- src/autograd.jl | 12 ++++++------ test/unittest/autograd.jl | 36 ++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/autograd.jl b/src/autograd.jl index 08a2f8790..fdae2abcd 100644 --- a/src/autograd.jl +++ b/src/autograd.jl @@ -283,12 +283,12 @@ function backward(heads::Vector{NDArray}, head_grads=Union{Vector, Void}; end """ - grad(arr::NDArray) + getgrad(arr::NDArray) -Returns gradient buffer attached to this `NDArray`. +Returns the gradient buffer attached to this `NDArray`. If the gradient buffer isn't attached yet, return `nothing`. """ -function grad(arr::NDArray) +function getgrad(arr::NDArray) out = Ref{mx.MX_handle}(C_NULL) @mxcall(:MXNDArrayGetGrad, (MX_handle, Ref{MX_handle}), arr.handle, out) (out[] == C_NULL) ? nothing: NDArray(MX_NDArrayHandle(out[])) @@ -311,7 +311,7 @@ The attached gradient buffer ## See also -- [`grad`](@ref) +- [`getgrad`](@ref) """ function attach_grad(arr::NDArray, grad_req::Symbol=:write) # TODO: support storage type (stype in Python) @@ -380,7 +380,7 @@ mark_variables(var::Vector{NDArray}, grads::Vector{NDArray}, grad_reqs=:write) = end """ - get_symbol(arr) + getsymbol(arr) Retrieve recorded computation history as `SymbolicNode`. @@ -392,7 +392,7 @@ Retrieve recorded computation history as `SymbolicNode`. The retrieved `Symbol`. """ -function get_symbol(arr::NDArray) +function getsymbol(arr::NDArray) ref = Ref{MX_handle}(C_NULL) @mxcall(:MXAutogradGetSymbol, (MX_handle, Ref{MX_handle}), arr, ref) SymbolicNode(MX_SymbolHandle(ref[])) diff --git a/test/unittest/autograd.jl b/test/unittest/autograd.jl index 5e04befcf..5c7c5dec1 100644 --- a/test/unittest/autograd.jl +++ b/test/unittest/autograd.jl @@ -5,20 +5,20 @@ using Base.Test using MXNet -function test_grad() - info("AutoGrad::grad") +function test_getgrad() + info("AutoGrad::getgrad") - info("AutoGrad::grad::unattached") - @test nothing == mx.grad(mx.zeros(10)) + info("AutoGrad::getgrad::unattached") + @test nothing == mx.getgrad(mx.zeros(10)) - info("AutoGrad::grad::attached") + info("AutoGrad::getgrad::attached") x = mx.NDArray([1 2; 3 4]) grad = mx.attach_grad(x) @test eltype(grad) ≡ Int @test copy(grad) == [0 0; 0 0] grad[:] = 42 - @test copy(mx.grad(x)) == [42 42; 42 42] + @test copy(mx.getgrad(x)) == [42 42; 42 42] end @@ -32,8 +32,8 @@ function test_mark_variables() ẋ[:] = 42 ẏ[:] = 24 - @test copy(mx.grad(x)) == [42, 42, 42, 42] - @test copy(mx.grad(y)) == [24, 24, 24, 24] + @test copy(mx.getgrad(x)) == [42, 42, 42, 42] + @test copy(mx.getgrad(y)) == [24, 24, 24, 24] info("AutoGrad::mark_variables::invalid grad_reqs") x = mx.zeros(4) @@ -62,11 +62,11 @@ function test_record() mx.backward(y) # gradient is 2x - @test copy(mx.grad(x)) == [2 4; 6 8] + @test copy(mx.getgrad(x)) == [2 4; 6 8] end let x = mx.NDArray([1 2; 3 4]) - info("AutoGrad::record::get_symbol") + info("AutoGrad::record::getsymbol") mx.attach_grad(x) y = mx.record() do @@ -75,7 +75,7 @@ function test_record() @test copy(y) == [1 4; 9 16] - @test isa(mx.get_symbol(y), mx.SymbolicNode) + @test isa(mx.getsymbol(y), mx.SymbolicNode) end let x = mx.NDArray([1 2; 3 4]) @@ -90,28 +90,28 @@ function test_record() mx.backward(y, retain_graph=true) # gradient is 2x - @test copy(mx.grad(x)) == [2 4; 6 8] + @test copy(mx.getgrad(x)) == [2 4; 6 8] - @test isa(mx.get_symbol(y), mx.SymbolicNode) + @test isa(mx.getsymbol(y), mx.SymbolicNode) end end # function test_record() -function test_get_symbol() - info("AutoGrad::get_symbol") +function test_getsymbol() + info("AutoGrad::getsymbol") let x = mx.zeros(4) mx.attach_grad(x) - @test isa(mx.get_symbol(x), mx.SymbolicNode) + @test isa(mx.getsymbol(x), mx.SymbolicNode) end end @testset "AutoGrad Test" begin - test_grad() + test_getgrad() test_mark_variables() test_record() - test_get_symbol() + test_getsymbol() end