import unittest

import numpy

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


@testing.parameterize(*testing.product({
    'shape': [(3, 4), ()],
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestFlatten(unittest.TestCase):

    dtype = numpy.float32

    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.g_shape = (numpy.prod((1,) + self.shape),)
        self.g = numpy.random.uniform(-1, 1, self.g_shape).astype(self.dtype)

    def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = functions.flatten(x)

        self.assertEqual(y.shape, self.g_shape)
        self.assertEqual(y.dtype, self.dtype)
        testing.assert_allclose(self.x.flatten(), y.data)

    def test_forward_cpu(self):
        self.check_forward(self.x)

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(cuda.to_gpu(self.x))

    def check_backward(self, x_data, g_data):
        gradient_check.check_backward(
            functions.Flatten(), x_data, g_data, dtype=numpy.float64)

    def test_backward_cpu(self):
        self.check_backward(self.x, self.g)

    @attr.gpu
    def test_backward_gpu(self):
        self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.g))


testing.run_module(__name__, __file__)

[((1406, 1444), 'chainer.testing.run_module', 'testing.run_module', (['__name__', '__file__'], {}), '(__name__, __file__)\n', (1424, 1444), False, 'from chainer import testing\n'), ((678, 702), 'chainer.Variable', 'chainer.Variable', (['x_data'], {}), '(x_data)\n', (694, 702), False, 'import chainer\n'), ((715, 735), 'chainer.functions.flatten', 'functions.flatten', (['x'], {}), '(x)\n', (732, 735), False, 'from chainer import functions\n'), ((222, 323), 'chainer.testing.product', 'testing.product', (["{'shape': [(3, 4), ()], 'dtype': [numpy.float16, numpy.float32, numpy.float64]}"], {}), "({'shape': [(3, 4), ()], 'dtype': [numpy.float16, numpy.\n    float32, numpy.float64]})\n", (237, 323), False, 'from chainer import testing\n'), ((518, 547), 'numpy.prod', 'numpy.prod', (['((1,) + self.shape)'], {}), '((1,) + self.shape)\n', (528, 547), False, 'import numpy\n'), ((1031, 1050), 'chainer.cuda.to_gpu', 'cuda.to_gpu', (['self.x'], {}), '(self.x)\n', (1042, 1050), False, 'from chainer import cuda\n'), ((1150, 1169), 'chainer.functions.Flatten', 'functions.Flatten', ([], {}), '()\n', (1167, 1169), False, 'from chainer import functions\n'), ((1362, 1381), 'chainer.cuda.to_gpu', 'cuda.to_gpu', (['self.x'], {}), '(self.x)\n', (1373, 1381), False, 'from chainer import cuda\n'), ((1383, 1402), 'chainer.cuda.to_gpu', 'cuda.to_gpu', (['self.g'], {}), '(self.g)\n', (1394, 1402), False, 'from chainer import cuda\n'), ((435, 474), 'numpy.random.uniform', 'numpy.random.uniform', (['(-1)', '(1)', 'self.shape'], {}), '(-1, 1, self.shape)\n', (455, 474), False, 'import numpy\n'), ((567, 608), 'numpy.random.uniform', 'numpy.random.uniform', (['(-1)', '(1)', 'self.g_shape'], {}), '(-1, 1, self.g_shape)\n', (587, 608), False, 'import numpy\n')]