import unittest

import numpy

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


@testing.parameterize(*testing.product({
    'in_shape': [(3, 4, 2)],
    'axis1': [0],
    'axis2': [1],
    'dtype': [numpy.float16, numpy.float32, numpy.float32],
}))
class TestSwapaxes(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)

    def check_forward(self, x_data):
        axis1, axis2 = self.axis1, self.axis2
        x = chainer.Variable(x_data)
        y = functions.swapaxes(x, axis1, axis2)
        self.assertEqual(y.data.dtype, self.dtype)
        self.assertTrue((self.x.swapaxes(axis1, axis2) ==
                         cuda.to_cpu(y.data)).all())

    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):
        x = chainer.Variable(x_data)
        y = functions.swapaxes(x, self.axis1, self.axis2)
        y.grad = y.data
        y.backward()
        testing.assert_allclose(x.data, x.grad, atol=0, rtol=0)

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

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


testing.run_module(__name__, __file__)

@@ -94,16 +94,51 @@
 nctions%0A
+from chainer import gradient_check%0A
 from cha
@@ -157,16 +157,16 @@
 testing%0A
-
 from cha
@@ -465,10 +465,268 @@
 orm(
--1
+%0A            0.5, 1, self.in_shape).astype(self.dtype)%0A        self.g = numpy.random.uniform(%0A            0.5, 1, self.in_shape).astype(self.dtype)%0A        self.g = self.g.swapaxes(self.axis1, self.axis2)%0A        self.gg = numpy.random.uniform(%0A            0.5
 , 1,
@@ -1607,32 +1607,32 @@
 ward_gpu(self):%0A
-
         self.che
@@ -1665,16 +1665,582 @@
 lf.x))%0A%0A
+    def check_double_backward(self, x_data, g_data, gg_data):%0A        def f(x):%0A            y = functions.swapaxes(x, self.axis1, self.axis2)%0A            return y * y%0A%0A        gradient_check.check_double_backward(%0A            f, x_data, g_data, gg_data, dtype=numpy.float64)%0A%0A    def test_double_backward_cpu(self):%0A        self.check_double_backward(self.x, self.g, self.gg)%0A%0A    @attr.gpu%0A    def test_double_backward_gpu(self):%0A        self.check_double_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.g),%0A                                   cuda.to_gpu(self.gg))%0A%0A
 %0Atesting

Add test