<s> import math <EOL> import unittest <EOL> import numpy <EOL> import six <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> @ testing . parameterize ( <EOL> * testing . product ( { <EOL> '<STR_LIT>' : [ <NUM_LIT:5> , <NUM_LIT:10> ] , '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> ] , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> } ) <EOL> ) <EOL> class TestContrastive ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> x_shape = ( self . batchsize , self . input_dim ) <EOL> self . x0 = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , x_shape ) . astype ( numpy . float32 ) <EOL> self . x1 = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , x_shape ) . astype ( numpy . float32 ) <EOL> self . t = numpy . random . randint ( <EOL> <NUM_LIT:0> , <NUM_LIT:2> , ( self . batchsize , ) ) . astype ( numpy . int32 ) <EOL> def check_forward ( self , x0_data , x1_data , t_data ) : <EOL> x0_val = chainer . Variable ( x0_data ) <EOL> x1_val = chainer . Variable ( x1_data ) <EOL> t_val = chainer . Variable ( t_data ) <EOL> loss = functions . contrastive ( x0_val , x1_val , t_val , self . margin ) <EOL> self . assertEqual ( loss . data . shape , ( ) ) <EOL> self . assertEqual ( loss . data . dtype , numpy . float32 ) <EOL> loss_value = float ( cuda . to_cpu ( loss . data ) ) <EOL> loss_expect = <NUM_LIT:0> <EOL> for i in six . moves . range ( self . x0 . shape [ <NUM_LIT:0> ] ) : <EOL> x0d , x1d , td = self . x0 [ i ] , self . x1 [ i ] , self . t [ i ] <EOL> d = numpy . sum ( ( x0d - x1d ) ** <NUM_LIT:2> ) <EOL> if td == <NUM_LIT:1> : <EOL> loss_expect += d <EOL> elif td == <NUM_LIT:0> : <EOL> loss_expect += max ( self . margin - math . sqrt ( d ) , <NUM_LIT:0> ) ** <NUM_LIT:2> <EOL> loss_expect /= <NUM_LIT> * self . t . shape [ <NUM_LIT:0> ] <EOL> self . assertAlmostEqual ( loss_expect , loss_value , places = <NUM_LIT:5> ) <EOL> def test_negative_margin ( self ) : <EOL> self . margin = - <NUM_LIT:1> <EOL> self . assertRaises ( ValueError , self . check_forward , <EOL> self . x0 , self . x1 , self . t ) <EOL> self . assertRaises ( ValueError , self . check_backward , <EOL> self . x0 , self . x1 , self . t ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x0 , self . x1 , self . t ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_gpu_no_cudnn ( self ) : <EOL> self . check_forward ( cuda . to_gpu ( self . x0 ) , cuda . to_gpu ( self . x1 ) , <EOL> cuda . to_gpu ( self . t ) ) <EOL> def check_backward ( self , x0_data , x1_data , t_data ) : <EOL> gradient_check . check_backward ( <EOL> functions . Contrastive ( self . margin ) , <EOL> ( x0_data , x1_data , t_data ) , None , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x0 , self . x1 , self . t ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_gpu_no_cudnn ( self ) : <EOL> self . check_backward ( cuda . to_gpu ( self . x0 ) , cuda . to_gpu ( self . x1 ) , <EOL> cuda . to_gpu ( self . t ) ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
