Exemple #1
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('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)
Exemple #2
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('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
Exemple #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)

Exemple #4
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
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:])
Exemple #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)
#     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,
#
# 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:
# 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,
#     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():
Exemple #10
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('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=[])
Exemple #11
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('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
Exemple #12
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('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)
Exemple #13
0
# 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)
# 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])
Exemple #15
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('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
#
#     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)
Exemple #17
0
# 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)
Exemple #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,
Exemple #19
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('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)),
Exemple #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
Exemple #21
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('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)
Exemple #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,
Exemple #23
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('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]
Exemple #24
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
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(
Exemple #25
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('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]
Exemple #26
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('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)
Exemple #27
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('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)
Exemple #28
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('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)
Exemple #29
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
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:])
Exemple #30
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('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,
Exemple #31
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('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)
Exemple #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)
Exemple #33
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('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])
Exemple #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)
#
#     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):
Exemple #36
0
        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,
# 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:])
#
#     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
Exemple #39
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('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=[
Exemple #40
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('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,
Exemple #41
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('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,
Exemple #42
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('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)
# 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]
#
#     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)

Exemple #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,
Exemple #46
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('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)
#
#     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)
Exemple #48
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('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
Exemple #49
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('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,
Exemple #50
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('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):
# 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
Exemple #52
0
# 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)
Exemple #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)
Exemple #54
0
# 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)
Exemple #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):
#
#     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
Exemple #57
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('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)

Exemple #58
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
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
Exemple #59
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.

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