<commit_before>import unittest

import numpy

from chainer import cuda
from chainer import gradient_check
from chainer.testing import attr
from chainer.testing import condition


if cuda.available:
    cuda.init()


def _uniform(*shapes):
    return map(lambda s: numpy.random.uniform(-1, 1, s).astype(numpy.float32), shapes)

class NumeraicalGradientTest(unittest.TestCase):

    def f(self, xs):
        return (xs[0] ** 2,)

    def df(self, xs):
        return ((2 * xs[0],),)

    def setUp(self):
        self.xs = _uniform((2, 1))
        self.gys = _uniform((2, 1))

    def check_numerical_grad(self, f, df, xs, dys):
        dfxs = df(xs)

        # matrix-vector multiplication of dfxs and dys
        dx_expect = map(lambda dfx: sum(map(lambda (a, b): a*b, zip(dfx, dys))), dfxs)

        func = lambda: f(xs)
        dx_actual = gradient_check.numerical_grad(func, xs, dys)

        self.assertEqual(len(dx_expect), len(dx_actual))
        for e, a in zip(dx_expect, dx_actual):
            gradient_check.assert_allclose(e, a)

    @condition.retry(3)
    def test_numerical_grad_cpu(self):
        self.check_numerical_grad(self.f, self.df, self.xs, self.gys)

    @condition.retry(3)
    @attr.gpu
    def test_numerical_grad_gpu(self):
        self.check_numerical_grad(self.f, self.df,
                                  map(cuda.to_gpu, self.xs),
                                  map(cuda.to_gpu, self.gys))


class NumericalGradientTest2(NumeraicalGradientTest):

    f = lambda self, xs: (1,)
    df = lambda self, xs: ((0,),)


def _exp(x):
    if isinstance(x, numpy.ndarray):
        return numpy.exp(x)
    else:
        return cuda.cumath.exp(x)


class NumericalGradientTest3(NumeraicalGradientTest):

    def f(self, xs):
        return (_exp(xs[0]),)

    def df(self, xs):
        return ((_exp(xs[0]),),)

    def setUp(self):
        self.xs = _uniform((2, 1))
        self.gys = _uniform((2, 1))


def _full_like(x, val):
    if isinstance(x, numpy.ndarray):
        return numpy.full_like(x, val)
    else:
        return cuda.full_like(x, val)


class NumericalGradientTest4(NumeraicalGradientTest):

    def f(self, xs):
        assert len(xs) == 2
        return (2 * xs[0] + 3 * xs[1], 4 * xs[0] + 5 * xs[1], 6 * xs[0] + 7 * xs[1])


    def df(self, xs):
        assert len(xs) == 2
        return ((_full_like(xs[0], 2), _full_like(xs[0], 4), _full_like(xs[0], 6)),
                (_full_like(xs[1], 3), _full_like(xs[1], 5), _full_like(xs[1], 7)))

    def setUp(self):
        self.xs = _uniform((2, 1), (2, 1))
        self.gys = _uniform((2, 1), (2, 1), (2, 1))





class AssertAllCloseTest(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, (2, 3)).astype(numpy.float32)
        self.y = numpy.random.uniform(-1, 1, (2, 3)).astype(numpy.float32)

    def check_identical(self, x):
        gradient_check.assert_allclose(x, x, atol=0, rtol=0)
<commit_msg>Add unittest for numerical grad<commit_after>import unittest

import numpy

from chainer import cuda
from chainer import gradient_check
from chainer.testing import attr
from chainer.testing import condition


if cuda.available:
    cuda.init()


def _uniform(*shapes):
    return map(lambda s: numpy.random.uniform(-1, 1, s).astype(numpy.float32), shapes)

class NumeraicalGradientTest(unittest.TestCase):

    def f(self, xs):
        return (xs[0] ** 2,)

    def df(self, xs):
        return ((2 * xs[0],),)

    def setUp(self):
        self.xs = _uniform((2, 1))
        self.gys = _uniform((2, 1))

    def check_numerical_grad(self, f, df, xs, dys):
        dfxs = df(xs)

        # matrix-vector multiplication of dfxs and dys
        dx_expect = map(lambda dfx: sum(map(lambda (a, b): a*b, zip(dfx, dys))), dfxs)

        func = lambda: f(xs)
        dx_actual = gradient_check.numerical_grad(func, xs, dys)

        self.assertEqual(len(dx_expect), len(dx_actual))
        for e, a in zip(dx_expect, dx_actual):
            gradient_check.assert_allclose(e, a)

    @condition.retry(3)
    def test_numerical_grad_cpu(self):
        self.check_numerical_grad(self.f, self.df, self.xs, self.gys)

    @condition.retry(3)
    @attr.gpu
    def test_numerical_grad_gpu(self):
        self.check_numerical_grad(self.f, self.df,
                                  map(cuda.to_gpu, self.xs),
                                  map(cuda.to_gpu, self.gys))


class NumericalGradientTest2(NumeraicalGradientTest):

    f = lambda self, xs: (1,)
    df = lambda self, xs: ((0,),)


def _exp(x):
    if isinstance(x, numpy.ndarray):
        return numpy.exp(x)
    else:
        return cuda.cumath.exp(x)


class NumericalGradientTest3(NumeraicalGradientTest):

    def f(self, xs):
        return (_exp(xs[0]),)

    def df(self, xs):
        return ((_exp(xs[0]),),)

    def setUp(self):
        self.xs = _uniform((2, 1))
        self.gys = _uniform((2, 1))


def _full_like(x, val):
    if isinstance(x, numpy.ndarray):
        return numpy.full_like(x, val)
    else:
        return cuda.full_like(x, val)


# def _dot(x, y):
#     if isinstance(x, numpy.ndarray):
#         assert isinstance(y, numpy.ndarray)
#         return numpy.dot(x, y)
#     else:
#         return cuda.gpuarray.dot(x, y)


# def _transpose(x):
#     if isinstance(x, numpy.ndarray):
#         return x.T
#     else:
#         return cuda.culinalg.transpose(x)


class NumericalGradientTest4(NumeraicalGradientTest):

    def f(self, xs):
        assert len(xs) == 2
        return (2 * xs[0] + 3 * xs[1], 4 * xs[0] + 5 * xs[1], 6 * xs[0] + 7 * xs[1])


    def df(self, xs):
        assert len(xs) == 2
        return ((_full_like(xs[0], 2), _full_like(xs[0], 4), _full_like(xs[0], 6)),
                (_full_like(xs[1], 3), _full_like(xs[1], 5), _full_like(xs[1], 7)))

    def setUp(self):
        self.xs = _uniform((2, 1), (2, 1))
        self.gys = _uniform((2, 1), (2, 1), (2, 1))


# class NumericalGradientTest5(NumeraicalGradientTest):

#     def f(self, xs):
#         assert len(xs) == 2
#         return (2 * xs[0], 5 * xs[1], _dot(xs[0], xs[1]))


#     def df(self, xs):
#         assert len(xs) == 2
#         return ((_full_like(xs[0], 2), _full_like(xs[0], 0), _transpose(xs[1])),
#                 (_full_like(xs[1], 0), _full_like(xs[1], 5), xs[0]))

#     def setUp(self):
#         self.xs = _uniform((3, 2), (2, 1))
#         self.gys = _uniform((3, 2), (2, 1), (3, 1))


class AssertAllCloseTest(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, (2, 3)).astype(numpy.float32)
        self.y = numpy.random.uniform(-1, 1, (2, 3)).astype(numpy.float32)

    def check_identical(self, x):
        gradient_check.assert_allclose(x, x, atol=0, rtol=0)

{'fraction_non_alphanumeric': 0.15392623438429506, 'fraction_numerical': 0.026621058893515764, 'mean_word_length': 2.61364857603439, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5368753', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2439, 'n_words': 560}