<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . testing import assert_raises <EOL> import theano <EOL> from theano import config <EOL> from theano . tests . unittest_tools import assert_allclose <EOL> from pylearn2 . models . mlp import MLP <EOL> from pylearn2 . models . mlp import Sigmoid , Tanh , Linear , RectifiedLinear <EOL> from pylearn2 . models . mlp import ConvElemwise <EOL> from pylearn2 . space import Conv2DSpace <EOL> from pylearn2 . models . mlp import SigmoidConvNonlinearity <EOL> from pylearn2 . models . mlp import TanhConvNonlinearity <EOL> from pylearn2 . models . mlp import IdentityConvNonlinearity <EOL> from pylearn2 . models . mlp import RectifierConvNonlinearity <EOL> def check_case ( conv_nonlinearity , mlp_nonlinearity , cost_implemented = True ) : <EOL> """<STR_LIT>""" <EOL> np . random . seed ( <NUM_LIT> ) <EOL> r = <NUM_LIT> <EOL> s = <NUM_LIT> <EOL> shape = [ r , s ] <EOL> nvis = r * s <EOL> output_channels = <NUM_LIT> <EOL> batch_size = <NUM_LIT> <EOL> x = np . random . rand ( batch_size , r , s , <NUM_LIT:1> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = [ batch_size , output_channels , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> x = x . astype ( config . floatX ) <EOL> y = y . astype ( config . floatX ) <EOL> x_mlp = x . flatten ( ) . reshape ( batch_size , nvis ) <EOL> y_mlp = y . flatten ( ) . reshape ( batch_size , output_channels ) <EOL> conv_model = MLP ( <EOL> input_space = Conv2DSpace ( shape = shape , <EOL> axes = [ '<STR_LIT:b>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' ] , <EOL> num_channels = <NUM_LIT:1> ) , <EOL> layers = [ ConvElemwise ( layer_name = '<STR_LIT>' , <EOL> nonlinearity = conv_nonlinearity , <EOL> output_channels = output_channels , <EOL> kernel_shape = shape , <EOL> pool_shape = [ <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> pool_stride = shape , <EOL> irange = <NUM_LIT:1.0> ) ] , <EOL> batch_size = batch_size <EOL> ) <EOL> X = conv_model . get_input_space ( ) . make_theano_batch ( ) <EOL> Y = conv_model . get_target_space ( ) . make_theano_batch ( ) <EOL> Y_hat = conv_model . fprop ( X ) <EOL> g = theano . function ( [ X ] , Y_hat ) <EOL> mlp_model = MLP ( <EOL> layers = [ mlp_nonlinearity ( dim = output_channels , <EOL> layer_name = '<STR_LIT>' , <EOL> irange = <NUM_LIT:1.0> ) ] , <EOL> batch_size = batch_size , <EOL> nvis = nvis <EOL> ) <EOL> W , b = conv_model . get_param_values ( ) <EOL> W_mlp = np . zeros ( shape = ( output_channels , nvis ) , dtype = config . floatX ) <EOL> for k in range ( output_channels ) : <EOL> W_mlp [ k ] = W [ k , <NUM_LIT:0> ] . flatten ( ) [ : : - <NUM_LIT:1> ] <EOL> W_mlp = W_mlp . T <EOL> b_mlp = b . flatten ( ) <EOL> mlp_model . set_param_values ( [ W_mlp , b_mlp ] ) <EOL> X1 = mlp_model . get_input_space ( ) . make_theano_batch ( ) <EOL> Y1 = mlp_model . get_target_space ( ) . make_theano_batch ( ) <EOL> Y1_hat = mlp_model . fprop ( X1 ) <EOL> f = theano . function ( [ X1 ] , Y1_hat ) <EOL> assert_allclose ( f ( x_mlp ) . flatten ( ) , g ( x ) . flatten ( ) , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> if cost_implemented : <EOL> mlp_cost = theano . function ( [ X1 , Y1 ] , mlp_model . cost ( Y1 , Y1_hat ) ) <EOL> conv_cost = theano . function ( [ X , Y ] , conv_model . cost ( Y , Y_hat ) ) <EOL> assert_allclose ( conv_cost ( x , y ) , mlp_cost ( x_mlp , y_mlp ) ) <EOL> else : <EOL> assert_raises ( NotImplementedError , conv_model . cost , Y , Y_hat ) <EOL> assert_raises ( NotImplementedError , mlp_model . cost , Y1 , Y1_hat ) <EOL> def test_all_costs ( ) : <EOL> """<STR_LIT>""" <EOL> cases = [ [ SigmoidConvNonlinearity ( ) , Sigmoid , True ] , <EOL> [ IdentityConvNonlinearity ( ) , Linear , True ] , <EOL> [ TanhConvNonlinearity ( ) , Tanh , False ] , <EOL> [ RectifierConvNonlinearity ( ) , RectifiedLinear , False ] ] <EOL> for conv_nonlinearity , mlp_nonlinearity , cost_implemented in cases : <EOL> check_case ( conv_nonlinearity , mlp_nonlinearity , cost_implemented ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_all_costs ( ) </s>
