Skip to content

Instantly share code, notes, and snippets.

@yamachu
Last active June 27, 2017 05:48
Show Gist options
  • Save yamachu/c5815d4ca63c712cf916347dbf922f1f to your computer and use it in GitHub Desktop.
Save yamachu/c5815d4ca63c712cf916347dbf922f1f to your computer and use it in GitHub Desktop.
NNablaをmacで動かしてみた
# rpath で実行時解決?だと mac は怒られるからパスをそのまま書き込んであげる
install_name_tool -id /Users/yamachu/.anyenv/envs/pyenv/versions/anaconda3-4.3.1/envs/py27/lib/python2.7/site-packages/nnabla/libnnabla.dylib libnnabla.dylib
for ss in `ls *.so`; do
install_name_tool -change "@rpath/libnnabla.dylib" /Users/yamachu/.anyenv/envs/pyenv/versions/anaconda3-4.3.1/envs/py27/lib/python2.7/site-packages/nnabla/libnnabla.dylib $ss
done
============================= test session starts ==============================
platform darwin -- Python 2.7.13, pytest-3.1.2, py-1.4.34, pluggy-0.4.0
rootdir: /Users/yamachu/tmp/nnabla/python, inifile: pytest.ini
collected 873 items
python/test/test_array_preference.py .
python/test/test_graph.py .....
python/test/test_imperative.py ...
python/test/test_nd_array.py .
python/test/test_parameter.py ..
python/test/test_python_function.py .
python/test/test_save_load_parameters.py .
python/test/test_variable.py .........
python/test/function/test_abs.py .
python/test/function/test_add2.py .......
python/test/function/test_add_scalar.py ...
python/test/function/test_affine.py ....
python/test/function/test_arithmetic_ops.py .............................
python/test/function/test_average_pooling.py ................
python/test/function/test_batch_normalization.py ......
python/test/function/test_binary_activation.py ..
python/test/function/test_binary_connect_affine.py ....
python/test/function/test_binary_connect_convolution.py ....
python/test/function/test_binary_cross_entropy.py .
python/test/function/test_binary_error.py .
python/test/function/test_binary_weight_affine.py ....
python/test/function/test_binary_weight_convolution.py ....
python/test/function/test_broadcast.py ...............................................................
python/test/function/test_categorical_cross_entropy.py ...
python/test/function/test_celu.py .........
python/test/function/test_concatenate.py ............
python/test/function/test_confusion_matrix.py ...
python/test/function/test_convolution.py ........
python/test/function/test_crelu.py ...
python/test/function/test_deconvolution.py ................................................................
python/test/function/test_dropout.py ........
python/test/function/test_elu.py ...
python/test/function/test_embed.py ............
python/test/function/test_exp.py .
python/test/function/test_flip.py ....
python/test/function/test_huber_loss.py ...
python/test/function/test_identity.py .
python/test/function/test_image_augmentation.py .
python/test/function/test_kl_multinomial.py ...
python/test/function/test_log.py .
python/test/function/test_logical.py ..............................
python/test/function/test_max_pooling.py ........
python/test/function/test_maximum_scalar.py ...
python/test/function/test_mean_subtraction.py ...
python/test/function/test_minimum_scalar.py ...
python/test/function/test_mul_scalar.py ...
python/test/function/test_one_hot.py .
python/test/function/test_pow_scalar.py ...
python/test/function/test_prelu.py ..
python/test/function/test_r_div_scalar.py ...
python/test/function/test_r_pow_scalar.py ...
python/test/function/test_r_sub_scalar.py ...
python/test/function/test_random_crop.py ...
python/test/function/test_random_flip.py ...
python/test/function/test_random_shift.py ........
python/test/function/test_reduce.py ..
python/test/function/test_reduction.py ......F......F........................................................
python/test/function/test_relu.py ..
python/test/function/test_reshape.py ..
python/test/function/test_shift.py ........
python/test/function/test_sigmoid.py .
python/test/function/test_sigmoid_cross_entropy.py .
python/test/function/test_sign.py ..
python/test/function/test_slice.py ....
python/test/function/test_softmax.py ...
python/test/function/test_softmax_cross_entropy.py ...
python/test/function/test_split.py ...
python/test/function/test_squared_error.py .
python/test/function/test_stack.py ......
python/test/function/test_sum_pooling.py ........
python/test/function/test_tanh.py .
python/test/function/test_top_n_error.py ......
python/test/function/test_transform_binary.py ..........................................
python/test/function/test_transpose.py ....
python/test/function/test_unlink.py ..
python/test/function/test_unpooling.py ............
python/test/function/test_vat_noise.py ......
python/test/solver/test_adadelta.py ....
python/test/solver/test_adagrad.py ..
python/test/solver/test_adam.py ....
python/test/solver/test_adamax.py ....
python/test/solver/test_momentum.py ....
python/test/solver/test_nesterov.py ....
python/test/solver/test_rmsprop.py ....
python/test/solver/test_sgd.py ..
python/test/utils/test_data_iterator.py ....................................................................................................................................................................................................................................................................
python/test/utils/test_data_source.py ......
=================================== FAILURES ===================================
________ test_reduction_forward_backward[sum-ctx0-Sum-False-axis6-313] _________
op = 'sum', seed = 313, axis = (1, 2, 3), keepdims = False
ctx = Context(backend='cpu', array_class='', device_id='0', compute_backend='default')
func_name = 'Sum'
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("axis", [None, 0, 1, 2, 3, (0, 2), (1, 2, 3)])
@pytest.mark.parametrize("keepdims", [False, True])
@pytest.mark.parametrize("op, ctx, func_name", list_ctx_and_func_name(['sum', 'mean', 'max', 'min', 'prod']))
def test_reduction_forward_backward(op, seed, axis, keepdims, ctx, func_name):
from nbla_test_utils import function_tester
func = getattr(F, op)
ref_func = getattr(np, op)
rng = np.random.RandomState(seed)
inputs = [rng.randn(2, 3, 4, 5).astype(np.float32)]
function_tester(rng, func, ref_func, inputs,
func_args=[axis],
func_kwargs=dict(keepdims=keepdims),
ctx=ctx, func_name=func_name,
> atol_b=3e-3)
python/test/function/test_reduction.py:40:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
rng = <mtrand.RandomState object at 0x10c0fa140>
func = <function sum at 0x10a0fbde8>, ref_func = <function sum at 0x10496a9b0>
inputs = [array([[[[ 0.18527888, -1.2367872 , 0.32600349, 0.99755579, 0.08737291],
...3105891, -0.11142748, -2.01562786, 1.57058263, 1.40829337]]]], dtype=float32)]
func_args = [(1, 2, 3)], func_kwargs = {'keepdims': False}, atol_f = 1e-06
atol_b = 0.003, dstep = 0.001, backward = [True]
ctx = Context(backend='cpu', array_class='', device_id='0', compute_backend='default')
func_name = 'Sum', ref_grad = None
def function_tester(rng, func, ref_func, inputs,
func_args=[], func_kwargs={},
atol_f=1e-6, atol_b=1e-3, dstep=1e-3, backward=None,
ctx=None, func_name=None, ref_grad=None):
""" Automatic testing of forward/backward pass of `func` by comparing it
to the reference implementation in `ref_func`.
Syntax of `ref_func`: inputs, parametes
Syntax of `ref_grad`: inputs, output grads, parameters
"""
if ctx is None:
ctx = nn.Context()
if backward is None:
backward = [True for _ in inputs]
# Create Variables
# print 'create_variable'
def create_variables(inputs, backward):
vinputs = []
for i, b in zip(inputs, backward):
if i is None:
vinputs += [None]
continue
vinputs += [nn.Variable(i.shape, need_grad=b)]
vinputs[-1].data.cast(i.dtype)[...] = i
return vinputs
vinputs = create_variables(inputs, backward)
# Checking forward
# print 'checking forward'
with nn.context_scope(ctx), nn.auto_forward():
o = func(*(vinputs + func_args), **func_kwargs)
rinputs = copy.deepcopy(inputs) # inputs for ref_func
refs = ref_func(*(rinputs + func_args), **func_kwargs)
def force_tuple(x):
if isinstance(x, tuple):
return x
return (x,)
refs = force_tuple(refs)
o = force_tuple(o)
assert len(o) == len(refs)
for i, ref in enumerate(refs):
res = o[i].d
assert np.allclose(ref, res, atol=atol_f)
# Checking function name
# print 'checking function name'
if func_name is not None:
assert o[0].parent.name == func_name
# Checking backward
# print 'checking backward'
if not True in backward:
return
# NNabla backward
for v in vinputs:
if v is None:
continue
if len(v.shape) == 0:
v.g = rng.randn()
continue
v.g = rng.randn(*v.shape).astype(v.data.dtype)
# Verify grad
vinputs = create_variables(inputs, backward)
rinputs = copy.deepcopy(inputs)
rinputs = [rinput if test else None for rinput,
test in zip(rinputs, backward)]
vgrads = [rng.randn(*o_.shape) for o_ in o]
agrads, ngrads = compute_analytical_and_numerical_grad(
o[0].parent, vinputs, o, rinputs, vgrads, epsilon=dstep,
rng=rng)
if ref_grad is not None:
rinputs = copy.deepcopy(inputs)
doutputs = [o_.g for o_ in o]
ngrads = ref_grad(*(rinputs + doutputs + func_args), **func_kwargs)
> assert np.allclose(ngrads, agrads, atol=atol_b)
E AssertionError
python/test/nbla_test_utils.py:249: AssertionError
_________ test_reduction_forward_backward[sum-ctx0-Sum-True-axis6-313] _________
op = 'sum', seed = 313, axis = (1, 2, 3), keepdims = True
ctx = Context(backend='cpu', array_class='', device_id='0', compute_backend='default')
func_name = 'Sum'
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("axis", [None, 0, 1, 2, 3, (0, 2), (1, 2, 3)])
@pytest.mark.parametrize("keepdims", [False, True])
@pytest.mark.parametrize("op, ctx, func_name", list_ctx_and_func_name(['sum', 'mean', 'max', 'min', 'prod']))
def test_reduction_forward_backward(op, seed, axis, keepdims, ctx, func_name):
from nbla_test_utils import function_tester
func = getattr(F, op)
ref_func = getattr(np, op)
rng = np.random.RandomState(seed)
inputs = [rng.randn(2, 3, 4, 5).astype(np.float32)]
function_tester(rng, func, ref_func, inputs,
func_args=[axis],
func_kwargs=dict(keepdims=keepdims),
ctx=ctx, func_name=func_name,
> atol_b=3e-3)
python/test/function/test_reduction.py:40:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
rng = <mtrand.RandomState object at 0x10c177050>
func = <function sum at 0x10a0fbde8>, ref_func = <function sum at 0x10496a9b0>
inputs = [array([[[[ 0.18527888, -1.2367872 , 0.32600349, 0.99755579, 0.08737291],
...3105891, -0.11142748, -2.01562786, 1.57058263, 1.40829337]]]], dtype=float32)]
func_args = [(1, 2, 3)], func_kwargs = {'keepdims': True}, atol_f = 1e-06
atol_b = 0.003, dstep = 0.001, backward = [True]
ctx = Context(backend='cpu', array_class='', device_id='0', compute_backend='default')
func_name = 'Sum', ref_grad = None
def function_tester(rng, func, ref_func, inputs,
func_args=[], func_kwargs={},
atol_f=1e-6, atol_b=1e-3, dstep=1e-3, backward=None,
ctx=None, func_name=None, ref_grad=None):
""" Automatic testing of forward/backward pass of `func` by comparing it
to the reference implementation in `ref_func`.
Syntax of `ref_func`: inputs, parametes
Syntax of `ref_grad`: inputs, output grads, parameters
"""
if ctx is None:
ctx = nn.Context()
if backward is None:
backward = [True for _ in inputs]
# Create Variables
# print 'create_variable'
def create_variables(inputs, backward):
vinputs = []
for i, b in zip(inputs, backward):
if i is None:
vinputs += [None]
continue
vinputs += [nn.Variable(i.shape, need_grad=b)]
vinputs[-1].data.cast(i.dtype)[...] = i
return vinputs
vinputs = create_variables(inputs, backward)
# Checking forward
# print 'checking forward'
with nn.context_scope(ctx), nn.auto_forward():
o = func(*(vinputs + func_args), **func_kwargs)
rinputs = copy.deepcopy(inputs) # inputs for ref_func
refs = ref_func(*(rinputs + func_args), **func_kwargs)
def force_tuple(x):
if isinstance(x, tuple):
return x
return (x,)
refs = force_tuple(refs)
o = force_tuple(o)
assert len(o) == len(refs)
for i, ref in enumerate(refs):
res = o[i].d
assert np.allclose(ref, res, atol=atol_f)
# Checking function name
# print 'checking function name'
if func_name is not None:
assert o[0].parent.name == func_name
# Checking backward
# print 'checking backward'
if not True in backward:
return
# NNabla backward
for v in vinputs:
if v is None:
continue
if len(v.shape) == 0:
v.g = rng.randn()
continue
v.g = rng.randn(*v.shape).astype(v.data.dtype)
# Verify grad
vinputs = create_variables(inputs, backward)
rinputs = copy.deepcopy(inputs)
rinputs = [rinput if test else None for rinput,
test in zip(rinputs, backward)]
vgrads = [rng.randn(*o_.shape) for o_ in o]
agrads, ngrads = compute_analytical_and_numerical_grad(
o[0].parent, vinputs, o, rinputs, vgrads, epsilon=dstep,
rng=rng)
if ref_grad is not None:
rinputs = copy.deepcopy(inputs)
doutputs = [o_.g for o_ in o]
ngrads = ref_grad(*(rinputs + doutputs + func_args), **func_kwargs)
> assert np.allclose(ngrads, agrads, atol=atol_b)
E AssertionError
python/test/nbla_test_utils.py:249: AssertionError
==================== 2 failed, 871 passed in 19.59 seconds =====================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment