<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> @ testing . parameterize ( <EOL> { '<STR_LIT>' : ( <NUM_LIT:10> , <NUM_LIT:5> ) , '<STR_LIT>' : ( <NUM_LIT:10> , ) } , <EOL> { '<STR_LIT>' : ( <NUM_LIT:0> , <NUM_LIT:5> ) , '<STR_LIT>' : ( <NUM_LIT:0> , ) } , <EOL> { '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT> ) , '<STR_LIT>' : ( <NUM_LIT:1> , ) } , <EOL> ) <EOL> class TestSelectItem ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x_data = numpy . random . uniform ( <EOL> - <NUM_LIT:1> , <NUM_LIT:1> , self . in_shape ) . astype ( numpy . float32 ) <EOL> self . t_data = numpy . random . randint ( <EOL> <NUM_LIT:0> , <NUM_LIT:2> , self . out_shape ) . astype ( numpy . int32 ) <EOL> self . gy_data = numpy . random . uniform ( <EOL> - <NUM_LIT:1> , <NUM_LIT:1> , self . out_shape ) . astype ( numpy . float32 ) <EOL> def check_forward ( self , x_data , t_data ) : <EOL> x = chainer . Variable ( x_data ) <EOL> t = chainer . Variable ( t_data ) <EOL> y = functions . select_item ( x , t ) <EOL> y_exp = cuda . to_cpu ( x_data ) [ range ( t_data . size ) , cuda . to_cpu ( t_data ) ] <EOL> numpy . testing . assert_equal ( cuda . to_cpu ( y . data ) , y_exp ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x_data , self . t_data ) <EOL> @ attr . gpu <EOL> def test_forward_gpu ( self ) : <EOL> self . check_forward ( cuda . to_gpu ( self . x_data ) , <EOL> cuda . to_gpu ( self . t_data ) ) <EOL> def check_backward ( self , x_data , t_data , gy_data ) : <EOL> gradient_check . check_backward ( <EOL> functions . SelectItem ( ) , <EOL> ( x_data , t_data ) , gy_data , eps = <NUM_LIT> ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x_data , self . t_data , self . gy_data ) <EOL> @ attr . gpu <EOL> def test_backward_gpu ( self ) : <EOL> self . check_backward ( cuda . to_gpu ( self . x_data ) , <EOL> cuda . to_gpu ( self . t_data ) , <EOL> cuda . to_gpu ( self . gy_data ) ) <EOL> @ testing . parameterize ( <EOL> { '<STR_LIT>' : - <NUM_LIT:1> , '<STR_LIT>' : False } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : False } , <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : True } , <EOL> ) <EOL> class TestSelectItemValueCheck ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( numpy . float32 ) <EOL> self . t = numpy . array ( [ self . t_value ] , dtype = numpy . int32 ) <EOL> self . original_debug = chainer . is_debug ( ) <EOL> chainer . set_debug ( True ) <EOL> def tearDown ( self ) : <EOL> chainer . set_debug ( self . original_debug ) <EOL> def check_value_check ( self , x_data , t_data ) : <EOL> x = chainer . Variable ( x_data ) <EOL> t = chainer . Variable ( t_data ) <EOL> if self . valid : <EOL> functions . select_item ( x , t ) <EOL> else : <EOL> with self . assertRaises ( ValueError ) : <EOL> functions . select_item ( x , t ) <EOL> def test_value_check_cpu ( self ) : <EOL> self . check_value_check ( self . x , self . t ) <EOL> @ attr . gpu <EOL> def test_value_check_gpu ( self ) : <EOL> self . check_value_check ( self . x , self . t ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
