<s> import unittest <EOL> import numpy <EOL> import six <EOL> import chainer <EOL> from chainer import cuda <EOL> from chainer import gradient_check <EOL> from chainer import links <EOL> from chainer import testing <EOL> from chainer . testing import attr <EOL> from chainer . testing import condition <EOL> def _batch_normalization ( expander , gamma , beta , x , mean , var , eps , test ) : <EOL> mean = mean [ expander ] <EOL> if test : <EOL> std = numpy . sqrt ( var [ expander ] ) <EOL> else : <EOL> std = numpy . sqrt ( var [ expander ] + eps ) <EOL> y_expect = gamma * ( x - mean ) / std + beta <EOL> return y_expect <EOL> @ testing . parameterize ( * testing . product ( { <EOL> '<STR_LIT:test>' : [ True , False ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> } ) ) <EOL> class BatchNormalizationTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . expander = ( None , Ellipsis ) + ( None , ) * self . ndim <EOL> self . aggr_axes = ( <NUM_LIT:0> , ) + tuple ( six . moves . range ( <NUM_LIT:2> , self . ndim + <NUM_LIT:2> ) ) <EOL> self . link = links . BatchNormalization ( <NUM_LIT:3> ) <EOL> gamma = self . link . gamma . data <EOL> gamma [ ... ] = numpy . random . uniform ( <NUM_LIT> , <NUM_LIT:1> , gamma . shape ) <EOL> beta = self . link . beta . data <EOL> beta [ ... ] = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , beta . shape ) <EOL> self . link . zerograds ( ) <EOL> self . gamma = gamma . copy ( ) [ self . expander ] <EOL> self . beta = beta . copy ( ) [ self . expander ] <EOL> shape = ( <NUM_LIT:7> , <NUM_LIT:3> ) + ( <NUM_LIT:2> , ) * self . ndim <EOL> self . x = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , shape ) . astype ( numpy . float32 ) <EOL> self . gy = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , shape ) . astype ( numpy . float32 ) <EOL> if self . test : <EOL> self . mean = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , ( <NUM_LIT:3> , ) ) . astype ( numpy . float32 ) <EOL> self . var = numpy . random . uniform ( <NUM_LIT:0.5> , <NUM_LIT:1> , ( <NUM_LIT:3> , ) ) . astype ( numpy . float32 ) <EOL> self . link . avg_mean [ ... ] = self . mean <EOL> self . link . avg_var [ ... ] = self . var <EOL> else : <EOL> self . mean = self . x . mean ( axis = self . aggr_axes ) <EOL> self . var = self . x . var ( axis = self . aggr_axes ) <EOL> def check_forward ( self , x_data ) : <EOL> x = chainer . Variable ( x_data , volatile = self . volatile ) <EOL> y = self . link ( x , test = self . test ) <EOL> self . assertEqual ( y . data . dtype , numpy . float32 ) <EOL> y_expect = _batch_normalization ( <EOL> self . expander , self . gamma , self . beta , self . x , self . mean , <EOL> self . var , self . link . eps , self . test ) <EOL> gradient_check . assert_allclose ( y_expect , y . data , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_forward_gpu ( self ) : <EOL> self . link . to_gpu ( ) <EOL> self . check_forward ( cuda . to_gpu ( self . x ) ) <EOL> @ attr . multi_gpu ( <NUM_LIT:2> ) <EOL> def test_forward_multi_gpu ( self ) : <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> self . link . to_gpu ( ) <EOL> x = cuda . to_gpu ( self . x ) <EOL> with cuda . get_device ( <NUM_LIT:0> ) : <EOL> self . check_forward ( x ) <EOL> def check_backward ( self , x_data , y_grad ) : <EOL> gradient_check . check_backward ( <EOL> self . link , x_data , y_grad , ( self . link . gamma , self . link . beta ) , <EOL> eps = <NUM_LIT> , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x , self . gy ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_backward_gpu ( self ) : <EOL> self . link . to_gpu ( ) <EOL> self . check_backward ( cuda . to_gpu ( self . x ) , cuda . to_gpu ( self . gy ) ) <EOL> @ testing . parameterize ( <EOL> { '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:10> } , <EOL> ) <EOL> class TestPopulationStatistics ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . decay = <NUM_LIT> <EOL> self . size = <NUM_LIT:3> <EOL> self . link = links . BatchNormalization ( self . size , self . decay ) <EOL> self . x = numpy . random . uniform ( <EOL> - <NUM_LIT:1> , <NUM_LIT:1> , ( self . nx , self . size ) ) . astype ( numpy . float32 ) <EOL> self . y = numpy . random . uniform ( <EOL> - <NUM_LIT:1> , <NUM_LIT:1> , ( self . ny , self . size ) ) . astype ( numpy . float32 ) <EOL> def check_statistics ( self , x ) : <EOL> x = chainer . Variable ( x ) <EOL> self . link ( x , finetune = True ) <EOL> gradient_check . assert_allclose ( self . x . mean ( axis = <NUM_LIT:0> ) , self . link . avg_mean ) <EOL> unbiased_var = self . x . var ( axis = <NUM_LIT:0> ) * self . nx / ( self . nx - <NUM_LIT:1> ) <EOL> gradient_check . assert_allclose ( unbiased_var , self . link . avg_var ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_statistics_cpu ( self ) : <EOL> self . check_statistics ( self . x ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_statistics_gpu ( self ) : <EOL> self . link . to_gpu ( ) <EOL> self . check_statistics ( cuda . to_gpu ( self . x ) ) <EOL> def check_statistics2 ( self , x , y ) : <EOL> x = chainer . Variable ( x ) <EOL> y = chainer . Variable ( y ) <EOL> self . link ( x , finetune = True ) <EOL> self . link ( y , finetune = True ) <EOL> mean = ( self . x . sum ( axis = <NUM_LIT:0> ) + self . y . sum ( axis = <NUM_LIT:0> ) ) / ( self . nx + self . ny ) <EOL> var = ( self . x . var ( axis = <NUM_LIT:0> ) * self . nx + <EOL> self . y . var ( axis = <NUM_LIT:0> ) * self . ny ) / ( self . nx + self . ny ) <EOL> unbiased_var = var * self . ny / ( self . ny - <NUM_LIT:1> ) <EOL> gradient_check . assert_allclose ( mean , self . link . avg_mean ) <EOL> gradient_check . assert_allclose ( unbiased_var , self . link . avg_var ) <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_statistics2_cpu ( self ) : <EOL> self . check_statistics2 ( self . x , self . y ) <EOL> @ attr . gpu <EOL> @ condition . retry ( <NUM_LIT:3> ) <EOL> def test_statistics2_gpu ( self ) : <EOL> self . link . to_gpu ( ) <EOL> self . check_statistics2 ( <EOL> cuda . to_gpu ( self . x ) , <EOL> cuda . to_gpu ( self . y ) ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
