test_symbol.py 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17
import nnvm.symbol as sym
18
from nnvm import NNVMError
19

tqchen committed
20 21
def test_dense():
    x = sym.Variable('x')
22 23
    y = sym.dense(x, units=30, name="fc")
    assert y.list_input_names() == ["x", "fc_weight", "fc_bias"]
tqchen committed
24

25 26 27 28 29 30 31
def test_batch_norm():
    x = sym.Variable('x')
    y = sym.dense(x, units=30, name="fc")
    z = sym.batch_norm(x, name='bn')
    assert z.list_input_names('aux_state') == ['bn_moving_mean', 'bn_moving_var']
    assert z.list_input_names('read_only') == ['x', 'bn_gamma', 'bn_beta']

32 33 34
def test_compose():
    x = sym.Variable('x')
    z = sym.Variable('z')
35
    y = sym.exp(sym.elemwise_add(x, x, name='add', gpu=2),
36 37
                name='exp', gpu=1, attr={"kk": "1"})

38 39
    assert y.list_input_names() == ['x']
    assert y.list_output_names() == ["exp_output"]
40 41
    assert y.list_attr()['gpu'] == '1'
    z = y.get_internals()
42
    assert z['add_output'].list_output_names() == ['add_output']
43
    assert y.list_attr(recursive=True)['add$gpu'] == '2'
44 45 46

def test_default_input():
    x = sym.Variable('x')
47 48
    y = sym.dense(data=x, units=30, name='fc', use_bias=False)
    assert y.list_input_names() == ['x', 'fc_weight']
49 50
    tname = [z.list_output_names()[0] for z in y.list_input_variables()]
    assert tname == y.list_input_names()
51
    try:
52
        z = sym.elemwise_add(x)
53 54 55 56
        assert False
    except NNVMError:
        pass

57 58 59
def test_copy():
    x = sym.Variable('x')
    z = sym.Variable('z')
60
    y = sym.exp(sym.elemwise_add(x, x, name='add', gpu=2),
61 62
                name='exp', gpu=1, attr={"kk": "1"})
    assert y.__copy__().debug_str() == y.debug_str()
63

64 65 66 67 68 69 70 71

def test_op_name():
    x = sym.Variable('x')
    y = sym.exp(x)
    op_name = y.attr("op_name")
    op_func = sym.__dict__[op_name]
    z = op_func(x)

72
if __name__ == "__main__":
73
    test_op_name()
74
    test_copy()
75 76
    test_default_input()
    test_compose()
77
    test_batch_norm()