Skip to content

Commit

Permalink
sym: handy macro mx.var for creating mx.Variable (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin authored and pluskid committed Nov 22, 2017
1 parent 91a410e commit f126482
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

* `mx.get_children` for exploring the graph programmatically. (#TBD)

* A handy macro `@mx.var` for creating `mx.Variable`. (#TBD)

```julia
julia> x = @mx.var x
MXNet.mx.SymbolicNode x

julia> x, y, z = @mx.var x y z
(MXNet.mx.SymbolicNode x, MXNet.mx.SymbolicNode y, MXNet.mx.SymbolicNode z)
```

# v0.3.0 (2017.11.16)

* Update `libmxnet` to
Expand Down
21 changes: 21 additions & 0 deletions src/symbolic-node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,27 @@ function Variable(name :: Union{Symbol, AbstractString}; attrs = Dict())
node
end

"""
@var <symbols>...
A handy macro for creating `mx.Variable`.
```julia
julia> x = @mx.var x
MXNet.mx.SymbolicNode x
julia> x, y, z = @mx.var x y z
(MXNet.mx.SymbolicNode x, MXNet.mx.SymbolicNode y, MXNet.mx.SymbolicNode z)
```
"""
macro var(n::Symbol)
Expr(:call, :Variable, QuoteNode(n))
end

macro var(names::Symbol...)
Expr(:tuple, map(n -> Expr(:call, :Variable, QuoteNode(n)), names)...)
end

"""
Group(nodes :: SymbolicNode...)
Expand Down
16 changes: 16 additions & 0 deletions test/unittest/symbolic-node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,21 @@ function test_get_name()
@test contains(name, "Ptr")
end # function test_get_name

function test_var()
info("SymbolicNode::var")
x = @mx.var x
@test x isa mx.SymbolicNode

x′ = @mx.var x
@test x.handle != x′.handle

x, y, z = @mx.var x y z
@test x isa mx.SymbolicNode
@test y isa mx.SymbolicNode
@test z isa mx.SymbolicNode
end # test_var


################################################################################
# Run tests
################################################################################
Expand All @@ -529,6 +544,7 @@ end # function test_get_name
test_div()
test_power()
test_get_name()
test_var()
end

end

0 comments on commit f126482

Please sign in to comment.