コード例 #1
0
ファイル: test_sign.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Sign')


def ref_func_sign(x, alpha):
    # returns -1 if x < 0, alpha if x==0, 1 if x > 0.
    y = np.sign(x)
    y[x == 0] = alpha
    return y


def ref_grad_sign(x, dy, alpha):
    # pass through gradient from output to input
    return dy.flatten()


@pytest.mark.parametrize("ctx, func_name", ctxs)
コード例 #2
0
ファイル: test_flip.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Flip')


def ref_flip(x, axes):
    if axes is None:
        axes = [len(x.shape) - 1]
    x = x.copy()
    for axis in axes:
        if axis == 0:
            x = x[::-1]
        elif axis == 1:
            x = x[::, ::-1]
        elif axis == 2:
            x = x[::, ::, ::-1]
        else:
            raise
コード例 #3
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('SoftmaxCrossEntropy')


def ref_softmax_cross_entropy(x, l, axis):
    orig_x = x.copy()
    x = x - x.max(axis, keepdims=True)
    x = np.exp(x) / np.exp(x).sum(axis, keepdims=True)
    x = np.rollaxis(x, axis, x.ndim).reshape(-1, x.shape[axis])
    ll = np.rollaxis(l, axis, x.ndim).flatten()
    y = - \
        np.log(
            np.maximum(x[np.arange(x.shape[0]), ll],
                       np.finfo(np.float32).tiny))
    return y.reshape(l.shape)

コード例 #4
0
ファイル: test_max_pooling.py プロジェクト: zwsong/nnabla
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
import refs
from nbla_test_utils import list_context

ctxs = list_context('MaxPooling')


def ref_max_pooling(x, kernel, stride, ignore_border, pad):
    # Only 2d
    y = []
    for xx in x.reshape((-1,) + x.shape[-3:]):
        if xx.ndim == 2:
            xx = xx[np.newaxis]
        y += [refs.pooling_2d(xx, 'max', kernel, stride,
                              pad, ignore_border)[np.newaxis]]
    y = np.vstack(y)
    if x.ndim == 2:
        y = np.squeeze(y, 1)
    return y.reshape(x.shape[:-3] + y.shape[1:])
コード例 #5
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('MinimumScalar')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_minimum_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng, F.minimum_scalar, np.minimum, inputs,
                    func_args=[val],
                    ctx=ctx, func_name=func_name)


@pytest.mark.parametrize("ctx, func_name", ctxs)
コード例 #6
0
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context
from nnabla.testing import assert_allclose

ctxs = list_context('MeanSubtraction')


def ref_mean_subtraction(x, rmean, t, base_axis, batch_stat):
    if batch_stat:
        mean = x.mean(tuple(range(0, base_axis)))
        rmean[...] = rmean + (mean - rmean) / (t + 1)
        t += 1
    return x - rmean


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape", [(2, 3, 4)])
@pytest.mark.parametrize("base_axis", [0, 1, 2])
@pytest.mark.parametrize("ctx, func_name", ctxs)
def test_mean_subtraction_forward_backward(seed, inshape, base_axis, ctx,
コード例 #7
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.

from six.moves import range

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('BatchNormalization')


def ref_batch_normalization(x, beta, gamma, rmean, rvar, axes, decay_rate,
                            eps, batch_stat, output_stat):
    assert len(axes) == 1
    reduc_axes = list(range(x.ndim))
    del reduc_axes[axes[0]]
    reduc_axes = tuple(reduc_axes)
    m = x.size / x.shape[axes[0]]
    if batch_stat:
        mean = x.mean(reduc_axes, keepdims=True)
        var = x.var(reduc_axes, keepdims=True)
        rmean[...] = decay_rate * rmean + (1 - decay_rate) * mean
        rvar[...] = decay_rate * rvar + (1 - decay_rate) * var * m / (m - 1)
    else:
コード例 #8
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('MulScalar')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_mul_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng,
                    F.mul_scalar,
                    lambda x, y: x * y,
                    inputs,
                    func_args=[val],
                    ctx=ctx,
コード例 #9
0
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from test_flip import ref_flip
from nbla_test_utils import list_context

ctxs = list_context('ImageAugmentation')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_image_augmentation_forward(seed, ctx, func_name):
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(16, 3, 8, 8).astype(np.float32)]
    i = nn.Variable(inputs[0].shape)
    # NNabla forward
    with nn.context_scope(ctx), nn.auto_forward():
        o = F.image_augmentation(i)
    assert o.d.shape == inputs[0].shape

    shape = (3, 5, 8)
    with nn.context_scope(ctx), nn.auto_forward():
コード例 #10
0
ファイル: test_constant.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Constant')


def ref_constant(val, shape):
    return np.ones(shape, dtype=np.float32) * val


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("value", [0., 1.5, -2., 1000.])
@pytest.mark.parametrize("shape", [[], [5], [2, 3, 4]])
def test_constant_forward(value, shape, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = None
    inputs = []
    function_tester(rng, F.constant, ref_constant, inputs, func_args=[value, shape],
                    ctx=ctx, func_name=func_name, backward=[])
コード例 #11
0
ファイル: test_transpose.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Transpose')


def ref_transpose(x, axes):
    return x.transpose(axes).copy()


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape, axes",
                         [((11, 13, 7), (2, 1, 0)),
                          ((3, 7, 4, 5), (3, 0, 1, 2)),
                          ((4, 2, 5, 2, 3), (3, 0, 1, 2, 4)),
                          ((4, 2, 3, 2, 3, 3), (5, 3, 0, 1, 2, 4))])
def test_transpose_forward_backward(seed, inshape, axes, ctx, func_name):
    from nbla_test_utils import function_tester
コード例 #12
0
ファイル: test_kl_multinomial.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('KLMultinomial')


def ref_kl_multinomial(p, q, base_axis):
    kl = np.sum(p * (np.log(p + 1.0e-8) - np.log(q + 1.0e-8)),
                axis=tuple(range(base_axis, p.ndim)))
    return kl.reshape(kl.shape + (1,))


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("base_axis, shape", [(1, (3, 6)), (1, (5, 8, 7)), (2, (4, 7, 9))])
def test_kl_multinomial_forward_backward(seed, ctx, base_axis, shape, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    input0 = 1 + rng.rand(*(shape)).astype(np.float32)
コード例 #13
0
ファイル: test_mul_scalar.py プロジェクト: zwsong/nnabla
# 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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('MulScalar')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_mul_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng, F.mul_scalar, lambda x, y: x * y, inputs,
                    func_args=[val],
                    ctx=ctx, func_name=func_name)
コード例 #14
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('CategoricalCrossEntropy')


def ref_categorical_cross_entropy(x, l, axis):
    orig_x = x.copy()
    x = np.rollaxis(x, axis, x.ndim).reshape(-1, x.shape[axis])
    ll = np.rollaxis(l, axis, x.ndim).flatten()
    y = - \
        np.log(
            np.maximum(x[np.arange(x.shape[0]), ll],
                       np.finfo(np.float32).tiny))
    return y.reshape(l.shape)


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
コード例 #15
0
ファイル: test_unpooling.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Unpooling')


def ref_unpooling(x, kernel):
    y = x
    for ind, p in enumerate(kernel[::-1]):
        y = y.repeat(p, axis=y.ndim - (ind + 1))
    return y


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape", [(1, 2, 3), (2, 4, 6), (2, 2, 4, 6), (2, 2, 2, 4, 6)])
@pytest.mark.parametrize("kernel", [(1, 1), (2, 3), (2, 1, 2)])
def test_unpooling_forward_backward(seed, inshape, kernel, ctx, func_name):
    from nbla_test_utils import function_tester
コード例 #16
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Unlink')


def ref_unlink(x):
    return x


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("shape", [(3, 6), (3, 7, 13)])
def test_unlink_forward_backward(seed, shape, ctx, func_name):

    rng = np.random.RandomState(seed)
    x0 = rng.randn(*(shape)).astype(np.float32)
    ref = ref_unlink(x0)
コード例 #17
0
ファイル: test_log.py プロジェクト: zwsong/nnabla
# Licensed 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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Log')


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("ctx, func_name", ctxs)
def test_log_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [
        np.clip(np.abs(rng.randn(2, 3, 4).astype(np.float32)) * 1e10, 1e-6, 1e10)]
    function_tester(rng, F.log, np.log, inputs, atol_f=1e-6,
                    atol_b=1e-2, ctx=ctx, func_name=func_name)
コード例 #18
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Exp')


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("ctx, func_name", ctxs)
def test_exp_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [np.clip(rng.randn(2, 3, 4).astype(np.float32) * 2.0, -2.0, 2.0)]
    function_tester(rng,
                    F.exp,
                    np.exp,
                    inputs,
                    atol_f=1e-6,
                    atol_b=1e-2,
                    ctx=ctx,
コード例 #19
0
ファイル: test_convolution.py プロジェクト: zwsong/nnabla
# 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.

from __future__ import division

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
import refs
from nbla_test_utils import list_context

ctxs = list_context('Convolution')


def ref_convolution(x, w, b, base_axis, pad, stride, dilation, group):
    y = []
    for xx in x.reshape((-1,) + x.shape[base_axis:]):
        y += [refs.convolution_2d(xx, w, b, pad, stride,
                                  dilation, group)[np.newaxis]]
    y = np.vstack(y)
    return y.reshape(x.shape[:base_axis] + y.shape[1:])


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape, kernel, outmaps, pad, stride, dilation",
                         [((2, 2, 10, 10), (3, 2), 4, (3, 0), (1, 2), (2, 1)),
コード例 #20
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('BatchDet')


def ref_det(x):
    y = np.zeros(x.shape[0], dtype=np.float32)
    for i in range(x.shape[0]):
        y[i] = np.linalg.det(x[i])
    return y


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [314])
def test_batch_det_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    # input must be batched square matrix
コード例 #21
0
ファイル: test_momentum.py プロジェクト: zwsong/nnabla
#     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.

import pytest
import nnabla as nn
import nnabla.solvers as S
import numpy as np
from solver_test_utils import solver_tester, RefSolver
from nbla_test_utils import list_context

ctxs = list_context('Momentum')


class RefMomentum(RefSolver):

    def __init__(self, lr, momentum):
        self.lr = lr
        self.momentum = momentum
        self.v = {}

    def _set_state_impl(self, key, param):
        self.v[key] = np.zeros_like(param)

    def _update_impl(self, key, p, g):
        _update_momentum(p, g, self.v[key], self.lr, self.momentum)
コード例 #22
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('RSubScalar')


def ref_r_sub_scalar(x, val):
    return val - x


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [315])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_r_sub_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32)]
    function_tester(rng,
                    F.r_sub_scalar,
コード例 #23
0
ファイル: test_softmax.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Softmax')


def ref_softmax(x, axis):
    x = x - x.max(axis, keepdims=True)
    x = np.exp(x) / np.exp(x).sum(axis, keepdims=True)
    return x


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("axis", [0, 1, 2])
@pytest.mark.parametrize("ctx, func_name", ctxs)
def test_softmax_forward_backward(seed, axis, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
コード例 #24
0
ファイル: test_gru.py プロジェクト: yati989/nnabla
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF
from nbla_test_utils import list_context

ctxs = list_context('GRU')


def gru(x, h, w, b, with_bias):
    hidden_size = h.shape[1]
    xh = F.concatenate(*(x, h), axis=1)
    w0, w1, w2 = F.split(w, axis=0)
    b0 = b1 = b2 = b3 = None
    if with_bias:
        b0, b1, b2, b3 = F.split(b, axis=0)
    r_t = F.sigmoid(F.affine(xh, F.transpose(w0, (1, 0)), b0))
    z_t = F.sigmoid(F.affine(xh, F.transpose(w1, (1, 0)), b1))

    w2_0 = w2[:, :w2.shape[1] - hidden_size]
    w2_1 = w2[:, w2.shape[1] - hidden_size:]
    n_t = F.tanh(
コード例 #25
0
ファイル: test_random_flip.py プロジェクト: zwsong/nnabla
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from test_flip import ref_flip
from nbla_test_utils import list_context

ctxs = list_context('RandomFlip')
"""
if hasattr(nn.extensions, 'cuda'):
    ctxs += [(nn.extensions.cuda.context(), 'RandomFlipCuda')]
"""


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("axes", [None, (1,), (2,)])
def test_random_flip_forward_backward(seed, axes, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32)]
    i = nn.Variable(inputs[0].shape, need_grad=True)
    i.d = inputs[0]
コード例 #26
0
ファイル: test_reduce.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Reduce')


def ref_reduce(x, op):
    return eval('np.%s' % op)(x)


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("op", ['mean', 'sum'])
@pytest.mark.parametrize("ctx, func_name", ctxs)
def test_reduce_forward_backward(seed, ctx, func_name, op):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng, F.reduce, ref_reduce, inputs,
                    func_args=[op], ctx=ctx)
コード例 #27
0
ファイル: test_elu.py プロジェクト: zwsong/nnabla
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F

from nbla_test_utils import list_context

ctxs = list_context('ELU')


def ref_elu(x, alpha):
    return np.maximum(x, 0) + alpha * (np.exp(np.minimum(x, 0)) - 1)


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("alpha", [1.0, 0.5, 0.0])
@pytest.mark.parametrize("seed", [313])
def test_elu_forward_backward(seed, alpha, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng, F.elu, ref_elu, inputs, func_args=[alpha],
                    ctx=ctx, func_name=func_name)
コード例 #28
0
ファイル: test_sgd.py プロジェクト: zwsong/nnabla
#     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.

import pytest
import nnabla as nn
import nnabla.solvers as S
import numpy as np
from solver_test_utils import solver_tester, RefSolver
from nbla_test_utils import list_context

ctxs = list_context('Sgd')


class RefSgd(RefSolver):

    def __init__(self, lr):
        self.lr = lr

    def _set_state_impl(self, key, param):
        pass

    def _update_impl(self, key, p, g):
        p[...] = p - self.lr * g


@pytest.mark.parametrize("ctx, solver_name", ctxs)
コード例 #29
0
ファイル: test_sum_pooling.py プロジェクト: zwsong/nnabla
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
import refs
from nbla_test_utils import list_context

ctxs = list_context('SumPooling')


def ref_sum_pooling(x, kernel, stride, ignore_border, pad):
    # Only 2d
    y = []
    for xx in x.reshape((-1,) + x.shape[-3:]):
        if xx.ndim == 2:
            xx = xx[np.newaxis]
        y += [refs.pooling_2d(xx, 'sum', kernel, stride,
                              pad, ignore_border)[np.newaxis]]
    y = np.vstack(y)
    if x.ndim == 2:
        y = np.squeeze(y, 1)
    return y.reshape(x.shape[:-3] + y.shape[1:])
コード例 #30
0
ファイル: test_constant.py プロジェクト: sony/nnabla
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Constant')


def ref_constant(val, shape):
    return np.ones(shape, dtype=np.float32) * val


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("value", [0., 1.5, -2., 1000.])
@pytest.mark.parametrize("shape", [[], [5], [2, 3, 4]])
def test_constant_forward(value, shape, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = None
    inputs = []
    function_tester(rng,
                    F.constant,
コード例 #31
0
ファイル: test_r_div_scalar.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('RDivScalar')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [314])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_r_div_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import function_tester, cap_ignore_region
    rng = np.random.RandomState(seed)
    inputs = [
        cap_ignore_region(
            rng.randn(2, 3, 4).astype(np.float32) * 3, (-0.5, 0.5))]
    function_tester(rng, F.r_div_scalar, lambda x, y: y / x, inputs,
                    func_args=[val], dstep=1e-4, atol_b=1e-1,
                    ctx=ctx, func_name=func_name)
コード例 #32
0
#
#     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.

import pytest
import numpy as np
import nnabla.functions as F

from nbla_test_utils import list_context

ctxs = list_context('CELU')


def ref_celu(x, alpha, axis):
    elu1 = np.maximum(0., x) + alpha * (np.exp(np.minimum(0, x)) - 1)
    elu2 = np.maximum(0., -x) + alpha * (np.exp(np.minimum(0, -x)) - 1)
    return np.concatenate([elu1, elu2], axis=axis)


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("alpha", [1.0, 0.5, 0.0])
@pytest.mark.parametrize("axis", [0, 1, 2, -1, -2, -3])
@pytest.mark.parametrize("seed", [313])
def test_celu_forward_backward(seed, alpha, axis, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
コード例 #33
0
ファイル: test_vat_noise.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('VATNoise')


def ref_vat_noise(r, base_axis, eps):
    shape = r.shape
    r = r.reshape(shape[0:base_axis] + (np.prod(shape[base_axis:]), ))
    r = r / np.sqrt(np.sum(r**2, axis=base_axis)
                    ).reshape(shape[0:base_axis] + (1,))
    r = r.reshape(shape).astype(np.float32)
    return eps * r


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("base_axis, shape", [(1, (3, 6)), (1, (5, 8)), (2, (3, 7, 13))])
@pytest.mark.parametrize("eps", [10, 1.4])
コード例 #34
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Identity')


def ref_identity(x):
    return x


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_identity_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32)]
    function_tester(rng, F.identity, ref_identity, inputs,
                    ctx=ctx, func_name=func_name, atol_b=1e-3)
コード例 #35
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('MeanSubtraction')


def ref_mean_subtraction(x, rmean, t, base_axis, batch_stat):
    if batch_stat:
        mean = x.mean(tuple(range(0, base_axis)))
        rmean[...] = rmean + (mean - rmean) / (t + 1)
        t += 1
    return x - rmean


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape", [(2, 3, 4)])
@pytest.mark.parametrize("base_axis", [0, 1, 2])
@pytest.mark.parametrize("ctx, func_name", ctxs)
def test_mean_subtraction_forward_backward(seed, inshape, base_axis, ctx, func_name):
コード例 #36
0
ファイル: test_reduction.py プロジェクト: ujtakk/nnabla
        ref_func,
        inputs,
        func_args=[axis],
        func_kwargs=dict(keepdims=keepdims),
        ctx=ctx,
        func_name=func_name,
        # The backward test on macOS doesn't pass with this tolerance.
        # Does Eigen library used in CPU computation backend produce
        # the different results on different platforms?
        # atol_b=3e-3,
        atol_b=6e-3)


@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape, axis", [((2, 1, 64, 64), (2, 3))])
@pytest.mark.parametrize("ctx, func_name", list_context('Sum'))
def test_large_sum_reduction(seed, inshape, axis, ctx, func_name):
    if not func_name.endswith('Cuda'):
        # This configuration is only to test the CUDA implementation branch
        # where reduction_size / outer_size >= 2048, so we skip this test for
        # CPU and CuDNN and also do not need to run for both keepdims.
        pytest.skip('skip CUDA specific implementation test for other target')

    rng = np.random.RandomState(seed)
    inputs = [rng.randn(*inshape).astype(np.float32)]
    function_tester(rng,
                    F.sum,
                    np.sum,
                    inputs,
                    ctx=ctx,
                    func_name=func_name,
コード例 #37
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.

from __future__ import division

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
import refs
from nbla_test_utils import list_context

ctxs = list_context('BinaryConnectConvolution')


def binarize(x):
    y = np.sign(x)
    y[y == 0] = -1.0
    return y


def ref_convolution(x, w, b, base_axis, pad, stride, dilation, group):
    y = []
    for xx in x.reshape((-1,) + x.shape[base_axis:]):
        y += [refs.convolution_2d(xx, w, b, pad, stride,
                                  dilation, group)[np.newaxis]]
    y = np.vstack(y)
    return y.reshape(x.shape[:base_axis] + y.shape[1:])
コード例 #38
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Concatenate')


def ref_concatenate(*inputs, **params):
    axis = params.pop('axis', len(inputs[0].shape) - 1)
    return np.concatenate(inputs, axis=axis)


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("axis", [0, 1, 2])
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize('different_size', [False, True])
@pytest.mark.parametrize('num_inputs', [2, 3])
def test_concatenate_forward_backward(seed, axis, different_size, num_inputs,
                                      ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
コード例 #39
0
ファイル: test_reshape.py プロジェクト: zwsong/nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Reshape')


def ref_reshape(x, shape):
    return x.reshape(shape).copy()


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape, outshape", [((1, 1, 6), (1, 2, 3)), ((2, 3), (1, 6))])
def test_reshape_forward_backward(seed, inshape, outshape, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    # Input
    inputs = [rng.randn(*inshape).astype(np.float32)]
    function_tester(rng, F.reshape, ref_reshape, inputs, func_args=[
コード例 #40
0
ファイル: test_acos.py プロジェクト: donproc/nnabla
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('ACos')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_acos_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [np.clip(rng.randn(2, 3, 4).astype(np.float32), -0.9, 0.9)]
    function_tester(rng,
                    F.acos,
                    np.arccos,
                    inputs,
                    ctx=ctx,
                    func_name=func_name,
                    atol_f=1e-3,
コード例 #41
0
ファイル: test_embed.py プロジェクト: gishikawa3/my_nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Embed')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("shape_x", [(10, ), (2, 8), (2, 3, 4), (2, 2, 3, 4)])
@pytest.mark.parametrize("shape_w", [(5, 3), (4, 3, 4), (6, 2, 2, 3)])
def test_embed_forward_backward(seed, shape_x, shape_w, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    n_class = shape_w[0]
    x = rng.randint(0, n_class - 1, shape_x).astype(np.int32)
    w = rng.randn(*shape_w).astype(np.float32)
    inputs = [x, w]
    function_tester(rng,
                    F.embed,
コード例 #42
0
ファイル: test_top_n_error.py プロジェクト: sony/nnabla
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context
from nnabla.testing import assert_allclose

ctxs = list_context('TopNError')


def ref_top_n_error(x, l, axis, n):
    orig_x = x.copy()
    x = np.rollaxis(x, axis, x.ndim).reshape(-1, x.shape[axis])
    ll = np.rollaxis(l, axis, x.ndim).flatten()
    y = []
    for x_, ll_ in zip(x, ll):
        threshold = x_[ll_]
        count = 0
        for x__ in x_:
            if x__ >= threshold:
                count += 1
        y.append(1 if count > n else 0)
    return np.array(y).reshape(l.shape)
コード例 #43
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.

from __future__ import division

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
import refs
from nbla_test_utils import list_context

ctxs = list_context('BinaryConnectConvolution')


def binarize(x, quantize_zero_to):
    y = np.sign(x)
    y[y == 0] = quantize_zero_to
    return y


def ref_convolution(x, w, b, base_axis, pad, stride, dilation, group,
                    quantize_zero_to):
    y = []
    for xx in x.reshape((-1, ) + x.shape[base_axis:]):
        y += [
            refs.convolution_2d(xx, w, b, pad, stride, dilation,
                                group)[np.newaxis]
コード例 #44
0
#
#     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.

import pytest
import nnabla.solvers as S
import numpy as np
from solver_test_utils import solver_tester, RefSolver
from nbla_test_utils import list_context

ctxs = list_context('Nesterov')


class RefNesterov(RefSolver):
    def __init__(self, lr, momentum):
        self.lr = lr
        self.momentum = momentum
        self.v = {}

    def _set_state_impl(self, key, param):
        self.v[key] = np.zeros_like(param)

    def _update_impl(self, key, p, g):
        _update_nesterov(p, g, self.v[key], self.lr, self.momentum)

コード例 #45
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.
"""
This is still left unlike other *2 operation (sub2, mul2, ...) because
it has cudnn implementation.
"""
import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Minimum2')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_minimum2_double_backward(seed, ctx, func_name):
    from nbla_test_utils import backward_function_tester
    rng = np.random.RandomState(seed)
    inputs = [
        rng.randn(2, 3).astype(np.float32),
        rng.randn(2, 3).astype(np.float32)
    ]
    backward_function_tester(rng,
                             F.minimum2,
                             None,
                             inputs=inputs,
コード例 #46
0
ファイル: test_maximum2.py プロジェクト: donproc/nnabla
# 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.

"""
This is still left unlike other *2 operation (sub2, mul2, ...) because
it has cudnn implementation.
"""
import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Maximum2')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_maximum2_double_backward(seed, ctx, func_name):
    from nbla_test_utils import backward_function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3).astype(np.float32),
              rng.randn(2, 3).astype(np.float32)]
    backward_function_tester(rng, F.maximum2,
                             inputs=inputs,
                             func_args=[], func_kwargs={},
                             atol_accum=1e-3,
                             dstep=1e-3,
                             ctx=ctx)
コード例 #47
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs_rand = list_context('Rand')
ctxs_randint = list_context('Randint')
ctxs_randn = list_context('Randn')
ctxs_rand_binomial = list_context('RandBinomial')
ctxs_rand_beta = list_context('RandBeta')
ctxs_rand_gamma = list_context('RandGamma')


@pytest.mark.parametrize("ctx, func_name", ctxs_rand)
@pytest.mark.parametrize("low, high", [(0, 1), (-2.5, 100), (0.1, 0.11)])
@pytest.mark.parametrize("shape", [[], [5], [100, 100]])
@pytest.mark.parametrize("seed", [-1, 313])
def test_rand_forward(seed, ctx, func_name, low, high, shape):
    with nn.context_scope(ctx):
        o = F.rand(low, high, shape, seed=seed)
    assert o.shape == tuple(shape)
コード例 #48
0
ファイル: test_reshape.py プロジェクト: gishikawa3/my_nnabla
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Reshape')


def ref_reshape(x, shape, inplace):
    return x.reshape(shape).copy()


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape, outshape", [((1, 1, 6), (1, 2, 3)),
                                               ((2, 3), (1, 6)),
                                               ((2, 4), (-1, 2, 2))])
def test_reshape_forward_backward(seed, inshape, outshape, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    # Input
コード例 #49
0
ファイル: test_pow2.py プロジェクト: sony/nnabla
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Pow2')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inplace", [False, True])
def test_pow2_double_backward(inplace, seed, ctx, func_name):
    from nbla_test_utils import backward_function_tester
    rng = np.random.RandomState(seed)
    inputs = [
        (rng.randint(5, size=(2, 3)).astype(np.float32) + 1.0) * 0.75,
        rng.randn(2, 3).astype(np.float32),
    ]
    backward_function_tester(rng,
                             F.pow2,
                             inputs=inputs,
コード例 #50
0
ファイル: test_sinh.py プロジェクト: donproc/nnabla
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Sinh')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_sinh_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 1]
    function_tester(rng, F.sinh, np.sinh, inputs, ctx=ctx, func_name=func_name,
                    atol_f=1e-3, atol_b=1e-2)


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_sinh_double_backward(seed, ctx, func_name):
コード例 #51
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('NormNormalization')


def ref_norm_normalization(x, p, axis, eps=1e-12):
    if p is None:
        p = 2.
    # calculate norm
    y = x
    y = np.abs(y)
    y = np.power(y, p)
    y = np.sum(y, axis, keepdims=True) + eps
    y = np.power(y, 1. / p)
    # normalization
    y = x / y
    return y
コード例 #52
0
ファイル: test_r_pow_scalar.py プロジェクト: zwsong/nnabla
# 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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('RPowScalar')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [314])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_r_pow_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng, F.r_pow_scalar, lambda x, y: y ** x, inputs,
                    func_args=[val], atol_b=1e-2,
                    ctx=ctx, func_name=func_name)
コード例 #53
0
#     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.

import pytest
import nnabla as nn
import nnabla.solvers as S
import numpy as np
from solver_test_utils import solver_tester, RefSolver
from nbla_test_utils import list_context

ctxs = list_context('Adam')


class RefAdam(RefSolver):
    def __init__(self, alpha, beta1, beta2, eps):
        self.alpha = alpha
        self.beta1 = beta1
        self.beta2 = beta2
        self.eps = eps
        self.m = {}
        self.v = {}
        self.t = {}

    def _set_state_impl(self, key, param):
        self.m[key] = np.zeros_like(param)
        self.v[key] = np.zeros_like(param)
コード例 #54
0
ファイル: test_add_scalar.py プロジェクト: zwsong/nnabla
# 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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('AddScalar')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("val", [0.5, 1, 2])
def test_add_scalar_forward_backward(seed, val, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    inputs = [rng.randn(2, 3, 4).astype(np.float32) * 2]
    function_tester(rng, F.add_scalar, lambda x, y: x + y, inputs,
                    func_args=[val],
                    ctx=ctx, func_name=func_name)
コード例 #55
0
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context, function_tester

ctxs = list_context('TopKGrad')


def ref_top_k_grad(k, abs, base_axis, grad):
    outer_dim = np.prod(grad.shape[:base_axis], dtype=int)
    inner_dim = np.prod(grad.shape[base_axis:], dtype=int)
    gg = grad.reshape(outer_dim, inner_dim).copy()
    ix = np.argsort(np.abs(gg) if abs else gg)[:, -k:]
    dx = np.zeros((outer_dim, inner_dim))
    for idx, row in enumerate(ix):
        dx[idx, row] = gg[idx, row]
    dx = dx.squeeze(axis=0) if base_axis == 0 else dx
    return dx.reshape(grad.shape)


def ref_top_k_grad_fw(x, k, abs, base_axis):
コード例 #56
0
#
#     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.

import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('EpsilonInsensitiveLoss')


def ref_epsilon_insensitive_loss_forward(x0, x1, epsilon):
    y = np.zeros_like(x0)
    abs_diff = np.abs(x0 - x1)
    idx = np.where(abs_diff > epsilon)
    y[idx] = abs_diff[idx] - epsilon
    return y


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("epsilon", [0.001, 1])
def test_epsilon_insensitive_loss_forward_backward(seed, ctx, func_name, epsilon):
    from nbla_test_utils import function_tester
コード例 #57
0
ファイル: test_abs.py プロジェクト: donproc/nnabla
# 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.

import pytest
import numpy as np
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Abs')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
def test_abs_forward_backward(seed, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [
        cap_ignore_region(
            rng.randn(2, 3, 4).astype(np.float32) * 2,
            (-1e-3, 1e-3))]
    function_tester(rng, F.abs, np.abs, inputs,
                    ctx=ctx, func_name=func_name)

コード例 #58
0
ファイル: test_shift.py プロジェクト: zwsong/nnabla
#
# 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.

import pytest
import numpy as np
from scipy.ndimage.interpolation import shift as scipy_shift

import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Shift')


def ref_shift(x, shifts, border_mode):
    if shifts is None:
        shifts = (0,) * len(x.shape)
    return scipy_shift(x, shifts, mode=border_mode)


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("inshape", [(2, 3, 4)])
@pytest.mark.parametrize("shifts", [None, (0, 0, 2,), (0, -2, 1), (1, 0, -1)])
@pytest.mark.parametrize("border_mode", ["nearest", "reflect"])
def test_shift_forward_backward(seed, inshape, shifts, border_mode, ctx, func_name):
    from nbla_test_utils import function_tester
コード例 #59
0
ファイル: test_dropout.py プロジェクト: zwsong/nnabla
#     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.

from six.moves import range
import pytest
import numpy as np
import nnabla as nn
import nnabla.functions as F
from nbla_test_utils import list_context

ctxs = list_context('Dropout')


@pytest.mark.parametrize("ctx, func_name", ctxs)
@pytest.mark.parametrize("seed", [313])
@pytest.mark.parametrize("p", [p / 10. for p in range(1, 9)])
def test_dropout_forward_backward(p, seed, ctx, func_name):
    from nbla_test_utils import cap_ignore_region, function_tester
    rng = np.random.RandomState(seed)
    inputs = [
        cap_ignore_region(
            rng.randn(2, 3, 4).astype(np.float32) * 2,
            (-1e-3, 1e-3))]  # Ensure there is no zero.
    i = nn.Variable(inputs[0].shape, need_grad=True)
    i.d = inputs[0]
    # NNabla forward