<s> import unittest <EOL> import numpy <EOL> import chainer <EOL> from chainer import cuda <EOL> from chainer import functions <EOL> from chainer import gradient_check <EOL> from chainer import testing <EOL> from chainer . testing import attr <EOL> from chainer . testing import condition <EOL> def _inv ( x ) : <EOL> if x . ndim == <NUM_LIT:2> : <EOL> return numpy . linalg . inv ( x ) <EOL> return numpy . array ( [ numpy . linalg . inv ( ix ) for ix in x ] ) <EOL> def _make_eye ( shape ) : <EOL> if len ( shape ) == <NUM_LIT:2> : <EOL> n = shape [ <NUM_LIT:0> ] <EOL> return numpy . eye ( n , dtype = numpy . float32 ) <EOL> m = shape [ <NUM_LIT:0> ] <EOL> n = shape [ <NUM_LIT:1> ] <EOL> return numpy . array ( [ numpy . eye ( n , dtype = numpy . float32 ) ] * m ) <EOL> @ testing . parameterize ( * testing . product ( { <EOL> '<STR_LIT>' : [ ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:5> , <NUM_LIT:5> ) ] , <EOL> } ) ) <EOL> class InvFunctionTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = ( numpy . eye ( self . shape [ - <NUM_LIT:1> ] ) + <EOL> numpy . random . uniform ( - <NUM_LIT> , <NUM_LIT> , self . shape ) ) . astype ( <EOL> numpy . float32 ) <EOL> self . gy = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , self . shape ) . astype ( numpy . float32 ) <EOL> def check_forward ( self , x_data , atol = <NUM_LIT> , rtol = <NUM_LIT> ) : <EOL> x = chainer . Variable ( x_data ) <EOL> y = functions . inv ( x ) <EOL> gradient_check . assert_allclose ( <EOL> _inv ( self . x ) , y . data , atol = atol , rtol = rtol ) <EOL> def check_backward ( self , x_data , y_grad , ** kwargs ) : <EOL> gradient_check . check_backward ( <EOL> functions . Inv ( ) , x_data , y_grad , ** kwargs ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_identity_cpu ( self ) : <EOL> eye = _make_eye ( self . x . shape ) <EOL> x = chainer . Variable ( self . x ) <EOL> y = functions . matmul ( x , functions . inv ( x ) ) <EOL> gradient_check . assert_allclose ( y . data , eye , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_identity_gpu ( self ) : <EOL> eye = cuda . to_gpu ( _make_eye ( self . x . shape ) ) <EOL> x = chainer . Variable ( cuda . to_gpu ( self . x ) ) <EOL> y = functions . matmul ( x , functions . inv ( x ) ) <EOL> gradient_check . assert_allclose ( y . data , eye , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_gpu ( self ) : <EOL> self . check_forward ( cuda . to_gpu ( self . x ) , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x , self . gy , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_gpu ( self ) : <EOL> self . check_backward ( cuda . to_gpu ( self . x ) , cuda . to_gpu ( self . gy ) , <EOL> atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ testing . parameterize ( * testing . product ( { <EOL> '<STR_LIT>' : [ ( <NUM_LIT:5> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> ) ] , <EOL> } ) ) <EOL> class BatchInvFunctionTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = ( numpy . eye ( self . shape [ - <NUM_LIT:1> ] ) + <EOL> numpy . random . uniform ( - <NUM_LIT> , <NUM_LIT> , self . shape ) ) . astype ( <EOL> numpy . float32 ) <EOL> self . gy = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , self . shape ) . astype ( numpy . float32 ) <EOL> def check_forward ( self , x_data , atol = <NUM_LIT> , rtol = <NUM_LIT> ) : <EOL> x = chainer . Variable ( x_data ) <EOL> y = functions . batch_inv ( x ) <EOL> gradient_check . assert_allclose ( <EOL> _inv ( self . x ) , y . data , atol = atol , rtol = rtol ) <EOL> def check_backward ( self , x_data , y_grad , ** kwargs ) : <EOL> gradient_check . check_backward ( <EOL> functions . BatchInv ( ) , x_data , y_grad , ** kwargs ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_identity_cpu ( self ) : <EOL> eye = _make_eye ( self . x . shape ) <EOL> x = chainer . Variable ( self . x ) <EOL> y = functions . batch_matmul ( x , functions . batch_inv ( x ) ) <EOL> gradient_check . assert_allclose ( y . data , eye , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_identity_gpu ( self ) : <EOL> eye = cuda . to_gpu ( _make_eye ( self . x . shape ) ) <EOL> x = chainer . Variable ( cuda . to_gpu ( self . x ) ) <EOL> y = functions . batch_matmul ( x , functions . batch_inv ( x ) ) <EOL> gradient_check . assert_allclose ( y . data , eye , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_gpu ( self ) : <EOL> self . check_forward ( cuda . to_gpu ( self . x ) , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x , self . gy , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_gpu ( self ) : <EOL> self . check_backward ( cuda . to_gpu ( self . x ) , cuda . to_gpu ( self . gy ) , <EOL> atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> class InvFunctionRaiseTest ( unittest . TestCase ) : <EOL> def test_invalid_ndim ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> functions . inv ( chainer . Variable ( numpy . zeros ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> def test_invalid_shape ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> functions . inv ( chainer . Variable ( numpy . zeros ( <NUM_LIT:1> , <NUM_LIT:2> ) ) ) <EOL> class BatchInvFunctionRaiseTest ( unittest . TestCase ) : <EOL> def test_invalid_ndim ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> functions . batch_inv ( chainer . Variable ( numpy . zeros ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> def test_invalid_shape ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> functions . batch_inv ( chainer . Variable ( numpy . zeros ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) ) ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
