<s> import unittest <EOL> from nose . tools import ( assert_is_not_none , assert_true , assert_raises , assert_equal ) <EOL> import io <EOL> import pickle <EOL> import numpy <EOL> from sknn . mlp import Regressor as MLPR <EOL> from sknn . mlp import Layer as L , Convolution as C <EOL> class TestConvolution ( unittest . TestCase ) : <EOL> def _run ( self , nn , a_in = None , fit = True ) : <EOL> assert_true ( nn . is_convolution ( ) ) <EOL> if a_in is None : <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:1> ) ) <EOL> a_out = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:4> ) ) <EOL> if fit is True : <EOL> nn . fit ( a_in , a_out ) <EOL> a_test = nn . predict ( a_in ) <EOL> assert_equal ( type ( a_out ) , type ( a_in ) ) <EOL> return a_test <EOL> def test_MissingLastDim ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) , <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:16> ) ) ) <EOL> def test_SquareKernel ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> def test_KernelPooling ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> def test_VerticalKernel ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:16> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> def test_VerticalVerbose ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:16> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> , verbose = <NUM_LIT:1> , valid_size = <NUM_LIT:0.1> ) ) <EOL> def test_HorizontalKernel ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:1> , <NUM_LIT:16> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> def test_ValidationSize ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> , <EOL> valid_size = <NUM_LIT:0.5> ) ) <EOL> def test_ValidationSet ( self ) : <EOL> v_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:1> ) ) <EOL> v_out = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:4> ) ) <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> , <EOL> valid_set = ( v_in , v_out ) ) ) <EOL> def test_MultipleLayers ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:6> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:5> , <NUM_LIT:5> ) ) , <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:8> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> def test_PoolingMaxType ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , pool_type = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> def test_PoolingMeanType ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , pool_type = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) ) <EOL> class TestUpscaling ( unittest . TestCase ) : <EOL> def _run ( self , nn , scale ) : <EOL> assert_true ( nn . is_convolution ( ) ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:16> , <NUM_LIT:1> ) ) <EOL> a_out = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:16> * scale , <NUM_LIT:16> * scale , <NUM_LIT:3> ) ) <EOL> nn . fit ( a_in , a_out ) <EOL> a_test = nn . predict ( a_in ) <EOL> assert_equal ( type ( a_out ) , type ( a_in ) ) <EOL> return a_test <EOL> def test_UpscalingFactorFour ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:3> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , scale_factor = ( <NUM_LIT:4> , <NUM_LIT:4> ) , border_mode = '<STR_LIT>' ) ] , <EOL> n_iter = <NUM_LIT:1> ) , <EOL> scale = <NUM_LIT:4> ) <EOL> def test_DownscaleUpscale ( self ) : <EOL> self . _run ( MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:6> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , border_mode = '<STR_LIT>' ) , <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:3> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , scale_factor = ( <NUM_LIT:2> , <NUM_LIT:2> ) , border_mode = '<STR_LIT>' ) ] , <EOL> n_iter = <NUM_LIT:1> ) , <EOL> scale = <NUM_LIT:1> ) <EOL> class TestConvolutionSpecs ( unittest . TestCase ) : <EOL> def test_SmallSquareKernel ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:5> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT:30> * <NUM_LIT:30> * <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> def test_SquareKernelFull ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:5> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:5> ] ) <EOL> def test_HorizontalKernel ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:7> , kernel_shape = ( <NUM_LIT:16> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:5> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:16> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT:16> * <NUM_LIT:7> , <NUM_LIT:5> ] ) <EOL> def test_VerticalKernel ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:1> , <NUM_LIT:16> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:7> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:16> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT:16> * <NUM_LIT:4> , <NUM_LIT:7> ] ) <EOL> def test_SquareKernelPool ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:5> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT:15> * <NUM_LIT:15> * <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> def test_SquarePoolFull ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:5> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT:16> * <NUM_LIT:16> * <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> def test_InvalidBorderMode ( self ) : <EOL> assert_raises ( NotImplementedError , C , <EOL> "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , border_mode = '<STR_LIT>' ) <EOL> def test_MultiLayerPooling ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) , <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) , <EOL> L ( "<STR_LIT>" ) ] ) <EOL> a_in , a_out = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:1> ) ) , numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:16> ) ) <EOL> nn . _initialize ( a_in , a_out ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:16> ] ) <EOL> def test_Upscaling ( self ) : <EOL> nn = MLPR ( layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:1> , <NUM_LIT:1> ) , scale_factor = ( <NUM_LIT:2> , <NUM_LIT:2> ) , border_mode = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" , units = <NUM_LIT:5> ) ] ) <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:1> ) ) <EOL> nn . _create_specs ( a_in ) <EOL> assert_equal ( nn . unit_counts , [ <NUM_LIT> , <NUM_LIT:64> * <NUM_LIT:64> * <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> class TestActivationTypes ( unittest . TestCase ) : <EOL> def _run ( self , activation ) : <EOL> a_in , a_out = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:1> ) ) , numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:4> ) ) <EOL> nn = MLPR ( <EOL> layers = [ <EOL> C ( activation , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) , <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , pool_type = '<STR_LIT>' ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) <EOL> nn . fit ( a_in , a_out ) <EOL> a_test = nn . predict ( a_in ) <EOL> assert_equal ( type ( a_out ) , type ( a_in ) ) <EOL> def test_RectifierConv ( self ) : <EOL> self . _run ( "<STR_LIT>" ) <EOL> def test_ExponentialLinear ( self ) : <EOL> self . _run ( "<STR_LIT>" ) <EOL> def test_SigmoidConv ( self ) : <EOL> self . _run ( "<STR_LIT>" ) <EOL> def test_TanhConv ( self ) : <EOL> self . _run ( "<STR_LIT>" ) <EOL> def test_LinearConv ( self ) : <EOL> self . _run ( "<STR_LIT>" ) <EOL> def test_UnknownConv ( self ) : <EOL> assert_raises ( NotImplementedError , self . _run , "<STR_LIT>" ) <EOL> class TestConvolutionRGB ( TestConvolution ) : <EOL> def _run ( self , nn , a_in = None ) : <EOL> if a_in is None : <EOL> a_in = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:1> ) ) <EOL> a_out = numpy . zeros ( ( <NUM_LIT:8> , <NUM_LIT:4> ) ) <EOL> nn . fit ( a_in , a_out ) <EOL> a_test = nn . predict ( a_in ) <EOL> assert_equal ( type ( a_out ) , type ( a_in ) ) <EOL> class TestSerialization ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . nn = MLPR ( <EOL> layers = [ <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:6> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:4> , kernel_shape = ( <NUM_LIT:5> , <NUM_LIT:5> ) ) , <EOL> C ( "<STR_LIT>" , channels = <NUM_LIT:8> , kernel_shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> L ( "<STR_LIT>" ) ] , <EOL> n_iter = <NUM_LIT:1> ) <EOL> def test_SerializeEmpty ( self ) : <EOL> buf = io . BytesIO ( ) <EOL> pickle . dump ( self . nn , buf ) <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> nn = pickle . load ( buf ) <EOL> buf = io . BytesIO ( ) <EOL> pickle . dump ( self . nn , buf ) <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> nn = pickle . load ( buf ) <EOL> assert_equal ( nn . layers , self . nn . layers ) <EOL> class TestSerializedNetwork ( TestConvolution ) : <EOL> def _run ( self , original , a_in = None ) : <EOL> a_test = super ( TestSerializedNetwork , self ) . _run ( original , a_in ) <EOL> buf = io . BytesIO ( ) <EOL> pickle . dump ( original , buf ) <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> nn = pickle . load ( buf ) <EOL> a_copy = super ( TestSerializedNetwork , self ) . _run ( nn , a_in , fit = False ) <EOL> assert_true ( ( a_test == a_copy ) . all ( ) ) </s>
