<s> import numpy as np <EOL> from layers import * <EOL> from fast_layers import * <EOL> conv_forward = conv_forward_fast <EOL> conv_backward = conv_backward_fast <EOL> avg_pool_forward = avg_pool_forward_fast <EOL> avg_pool_backward = avg_pool_backward_fast <EOL> max_pool_forward = max_pool_forward_fast <EOL> max_pool_backward = max_pool_backward_fast <EOL> def spatial_reflection_padding ( array , pad_width , pad_type = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> return np . pad ( array , pad_width , '<STR_LIT>' , pad_type ) <EOL> def spatial_replication_padding ( array , pad_width ) : <EOL> '''<STR_LIT>''' <EOL> return np . pad ( array , pad_width , '<STR_LIT>' ) <EOL> def skip_forward ( x , n_out_channels ) : <EOL> '''<STR_LIT>''' <EOL> N , n_in_channels , H , W = x . shape <EOL> assert ( n_in_channels == n_out_channels ) or ( <EOL> n_out_channels == n_in_channels * <NUM_LIT:2> ) , '<STR_LIT>' <EOL> skip = np . array ( x , copy = True ) <EOL> pool_cache , downsampled , skip_p = None , False , <NUM_LIT:0> <EOL> if n_out_channels > n_in_channels : <EOL> pool_param = { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:2> } <EOL> skip , pool_cache = avg_pool_forward ( skip , pool_param ) <EOL> p = skip_p = ( n_in_channels ) / <NUM_LIT:2> <EOL> skip = np . pad ( skip , ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( p , p ) , ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> mode = '<STR_LIT>' ) <EOL> downsampled = True <EOL> return skip , ( pool_cache , downsampled , skip_p ) <EOL> def skip_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> pool_cache , downsampled , skip_p = cache <EOL> dskip = np . array ( dout , copy = True ) <EOL> if downsampled : <EOL> dskip = dskip [ : , skip_p : - skip_p , : , : ] <EOL> dskip = avg_pool_backward ( dskip , pool_cache ) <EOL> return dskip <EOL> def affine_relu_forward ( x , w , b ) : <EOL> '''<STR_LIT>''' <EOL> a , fc_cache = affine_forward ( x , w , b ) <EOL> out , relu_cache = relu_forward ( a ) <EOL> cache = ( fc_cache , relu_cache ) <EOL> return out , cache <EOL> def affine_relu_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> fc_cache , relu_cache = cache <EOL> da = relu_backward ( dout , relu_cache ) <EOL> dx , dw , db = affine_backward ( da , fc_cache ) <EOL> return dx , dw , db <EOL> def affine_bn_relu_forward ( x , w , b , gamma , beta , bn_param ) : <EOL> '''<STR_LIT>''' <EOL> h , h_cache = affine_forward ( x , w , b ) <EOL> hnorm , hnorm_cache = batchnorm_forward ( h , gamma , beta , bn_param ) <EOL> hnormrelu , relu_cache = relu_forward ( hnorm ) <EOL> cache = ( h_cache , hnorm_cache , relu_cache ) <EOL> return hnormrelu , cache <EOL> def affine_bn_relu_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> h_cache , hnorm_cache , relu_cache = cache <EOL> dhnormrelu = relu_backward ( dout , relu_cache ) <EOL> dhnorm , dgamma , dbeta = batchnorm_backward ( dhnormrelu , hnorm_cache ) <EOL> dx , dw , db = affine_backward ( dhnorm , h_cache ) <EOL> return dx , dw , db , dgamma , dbeta <EOL> def conv_relu_forward ( x , w , b , conv_param ) : <EOL> '''<STR_LIT>''' <EOL> a , conv_cache = conv_forward_fast ( x , w , b , conv_param ) <EOL> out , relu_cache = relu_forward ( a ) <EOL> cache = ( conv_cache , relu_cache ) <EOL> return out , cache <EOL> def conv_relu_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> conv_cache , relu_cache = cache <EOL> da = relu_backward ( dout , relu_cache ) <EOL> dx , dw , db = conv_backward_fast ( da , conv_cache ) <EOL> return dx , dw , db <EOL> def conv_relu_pool_forward ( x , w , b , conv_param , pool_param ) : <EOL> '''<STR_LIT>''' <EOL> a , conv_cache = conv_forward_fast ( x , w , b , conv_param ) <EOL> s , relu_cache = relu_forward ( a ) <EOL> out , pool_cache = max_pool_forward ( s , pool_param ) <EOL> cache = ( conv_cache , relu_cache , pool_cache ) <EOL> return out , cache <EOL> def conv_relu_pool_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> conv_cache , relu_cache , pool_cache = cache <EOL> ds = max_pool_backward ( dout , pool_cache ) <EOL> da = relu_backward ( ds , relu_cache ) <EOL> dx , dw , db = conv_backward_fast ( da , conv_cache ) <EOL> return dx , dw , db <EOL> def conv_bn_relu_forward ( x , w , b , conv_param , gamma , beta , bn_param ) : <EOL> '''<STR_LIT>''' <EOL> out , conv_cache = conv_forward_fast ( x , w , b , conv_param ) <EOL> out , batchnorm_cache = spatial_batchnorm_forward ( <EOL> out , gamma , beta , bn_param ) <EOL> out , relu_cache = relu_forward ( out ) <EOL> cache = ( conv_cache , batchnorm_cache , relu_cache ) <EOL> return out , cache <EOL> def conv_bn_relu_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> conv_cache , batchnorm_cache , relu_cache = cache <EOL> dout = relu_backward ( dout , relu_cache ) <EOL> dout , dgamma , dbeta = spatial_batchnorm_backward ( dout , batchnorm_cache ) <EOL> dx , dw , db = conv_backward_fast ( dout , conv_cache ) <EOL> return dx , dw , db , dgamma , dbeta <EOL> def bn_relu_conv_forward ( x , w , b , conv_param , gamma , beta , bn_param , res = None ) : <EOL> '''<STR_LIT>''' <EOL> out , batchnorm_cache = spatial_batchnorm_forward ( <EOL> out , gamma , beta , bn_param ) <EOL> out , relu_cache = relu_forward ( out ) <EOL> out , conv_cache = conv_forward_fast ( x , w , b , conv_param ) <EOL> if res is not None : <EOL> out += res <EOL> cache = ( conv_cache , batchnorm_cache , relu_cache ) <EOL> return out , cache <EOL> def bn_relu_conv_backward ( dout , cache , dres_ref = None ) : <EOL> '''<STR_LIT>''' <EOL> assert dres_ref is None or len ( dres_ref ) == <NUM_LIT:0> <EOL> conv_cache , batchnorm_cache , relu_cache = cache <EOL> if dres_ref is not None : <EOL> dres_ref . append ( dout ) <EOL> dx , dw , db = conv_backward_fast ( dout , conv_cache ) <EOL> dout = relu_backward ( dout , relu_cache ) <EOL> dout , dgamma , dbeta = spatial_batchnorm_backward ( dout , batchnorm_cache ) <EOL> return dx , dw , db , dgamma , dbeta <EOL> def conv_bn_relu_pool_forward ( x , w , b , conv_param , gamma , beta , bn_param , pool_param ) : <EOL> '''<STR_LIT>''' <EOL> out , conv_cache = conv_forward_fast ( x , w , b , conv_param ) <EOL> out , batchnorm_cache = spatial_batchnorm_forward ( <EOL> out , gamma , beta , bn_param ) <EOL> out , relu_cache = relu_forward ( out ) <EOL> out , maxpool_cache = max_pool_forward ( out , pool_param ) <EOL> cache = ( conv_cache , batchnorm_cache , relu_cache , maxpool_cache ) <EOL> return out , cache <EOL> def conv_bn_relu_pool_backward ( dout , cache ) : <EOL> '''<STR_LIT>''' <EOL> conv_cache , batchnorm_cache , relu_cache , maxpool_cache = cache <EOL> dout = max_pool_backward ( dout , maxpool_cache ) <EOL> dout = relu_backward ( dout , relu_cache ) <EOL> dout , dgamma , dbeta = spatial_batchnorm_backward ( dout , batchnorm_cache ) <EOL> dx , dw , db = conv_backward_fast ( dout , conv_cache ) <EOL> return dx , dw , db , dgamma , dbeta </s>
