def create_ones_tensor(shape, dtype, name='ones_tensor'): """Create ones Tensor for test in current scope""" if luchador.get_nn_backend() == 'theano': import theano.tensor as be else: import tensorflow as be tensor = be.ones(shape, dtype=dtype) return nn.Tensor(tensor, shape=shape, name=name)
def _write_data_to_file(file_, data): for key, value in data.items(): _LG.debug(' Saving: %10s %24s %s', value.dtype, value.shape, key) if key in file_: del file_[key] chunks = None if value.size == 1 else True file_.create_dataset(key, data=value, chunks=chunks) if 'LUCHADOR_NN_BACKEND' not in file_: data = np.string_(luchador.get_nn_backend()) file_.create_dataset('LUCHADOR_NN_BACKEND', data=data, dtype='S10') if 'LUCHADOR_NN_CONV_FORMAT' not in file_: data = np.string_(luchador.get_nn_conv_format()) file_.create_dataset('LUCHADOR_NN_CONV_FORMAT', data=data, dtype='S4') if 'LUCHADOR_NN_DTYPE' not in file_: data = np.string_(luchador.get_nn_dtype()) file_.create_dataset('LUCHADOR_NN_DTYPE', data=data, dtype='S10') if 'LUCHADOR_VERSION' not in file_: data = np.string_(luchador.__version__) file_.create_dataset('LUCHADOR_VERSION', data=data) file_.flush()
from __future__ import division from __future__ import absolute_import # import theano # theano.config.optimizer = 'None' # theano.config.exception_verbosity = 'high' import numpy as np import luchador from luchador import nn from tests.unit import fixture BE = luchador.get_nn_backend() # pylint: disable=too-many-locals, invalid-name def _get_y_equals_x_squared(scope, x_init): with nn.variable_scope(scope): x = nn.get_variable( name='x', shape=(), trainable=True, initializer=nn.initializer.ConstantInitializer(x_init)) y = x * x return x, y def _get_slot_var(optimizer, slot_name, var_name=None):
"""Unit test for luchador.nn.ops module""" from __future__ import absolute_import import numpy as np import luchador from luchador import nn from tests.unit import fixture _BACKEND = luchador.get_nn_backend() # pylint: disable=invalid-name ############################################################################### # Test gradients class TestComputeGradiens(fixture.TestCase): """Test gradient computation""" def test_compute_gradients(self): """compute_gradients returns None for non-trainable wrt""" with nn.variable_scope(self.get_scope()): xs = [nn.make_variable( name='x_{}'.format(i), shape=(), trainable=bool(i % 2), ) for i in range(5)] y = xs[0] + xs[1] + xs[2] + xs[3] + xs[4] grads_and_vars = nn.ops.compute_gradient(loss=y, wrt=xs) self.assertEqual(len(xs), len(grads_and_vars)) for i, (grad, var) in enumerate(grads_and_vars): self.assertIs(xs[i], var) if i % 2: self.assertIsNotNone(grad) else:
"""Test Layer behaviors""" from __future__ import division from __future__ import absolute_import import numpy as np # import theano # theano.config.optimizer = 'None' # theano.config.exception_verbosity = 'high' import luchador from luchador import nn from tests.unit.fixture import TestCase _BE = luchador.get_nn_backend() _FMT = luchador.get_nn_conv_format() class Conv2DTransposeTest(TestCase): """Test for Conv2DTranspose class""" def _check(self, input_var, output_var): session = nn.Session() session.initialize() input_val = np.random.randn(*input_var.shape) output_val = session.run( outputs=output_var, inputs={input_var: input_val}) self.assertEqual(output_var.shape, input_var.shape) self.assertEqual(output_var.dtype, input_var.dtype) self.assertEqual(output_var.shape, output_val.shape)
"""Initialize scope module Scoping is a fundamental mechanism which must be defined at the lowest level. Therefore, unlike other backend module, this is placed under base module. """ from __future__ import absolute_import import luchador # pylint: disable=wildcard-import if luchador.get_nn_backend() == 'tensorflow': from .tensorflow import * # noqa else: from .theano import * # noqa
self.assertEqual( expected, found, 'Variable scope was not properly closed in the last test. ') def _check_scope(self, expected, found=None): found = found or be._get_scope() self.assertEqual(expected, found, 'Failed to update current variable scope. ') def _check_reuse(self, expected, found=None): found = found or be._get_flag() self.assertEqual(expected, found, 'Failed to update current reuse flag. ') @unittest.skipUnless(luchador.get_nn_backend() == 'theano', 'Theano backend') class TestVariableScopeClass(_ScopeTestCase): """Test if VariableScope correctly change global flags""" def test_update_scope(self): """VariableScope updates current variable scope""" scopes = [self.get_scope(suffix) for suffix in ['aaa', 'bbb', 'ccc']] with nn.VariableScope(reuse=False, name=scopes[0]): self._check_scope(scopes[0]) with nn.VariableScope(reuse=False, name=scopes[1]): self._check_scope(scopes[1]) with nn.VariableScope(reuse=False, name=scopes[2]): self._check_scope(scopes[2]) def test_update_reuse_flag(self): """VariableScope updates current reuse flag""" reuse = False
def _check_scope(self, expected, found=None): found = found or be._get_scope() self.assertEqual( expected, found, 'Failed to update current variable scope. ' ) def _check_reuse(self, expected, found=None): found = found or be._get_flag() self.assertEqual( expected, found, 'Failed to update current reuse flag. ' ) @unittest.skipUnless(luchador.get_nn_backend() == 'theano', 'Theano backend') class TestVariableScopeClass(_ScopeTestCase): """Test if VariableScope correctly change global flags""" def test_update_scope(self): """VariableScope updates current variable scope""" scopes = [self.get_scope(suffix) for suffix in ['aaa', 'bbb', 'ccc']] with nn.VariableScope(reuse=False, name=scopes[0]): self._check_scope(scopes[0]) with nn.VariableScope(reuse=False, name=scopes[1]): self._check_scope(scopes[1]) with nn.VariableScope(reuse=False, name=scopes[2]): self._check_scope(scopes[2]) def test_update_reuse_flag(self): """VariableScope updates current reuse flag""" reuse = False
"""Initialize Neural Network module and load backend""" from __future__ import absolute_import import logging # pylint: disable=wildcard-import, redefined-builtin import luchador from .core import * # noqa from .util import * # noqa _LG = logging.getLogger(__name__) _LG.info('Luchador Version: %s', luchador.__version__) _LG.info('Luchador NN backend: %s', luchador.get_nn_backend())