<s> import pickle <EOL> import sys <EOL> from lasagne . layers import BatchNormLayer <EOL> from lasagne . layers import ConcatLayer <EOL> from lasagne . layers import Conv2DLayer <EOL> from lasagne . layers import DenseLayer <EOL> from lasagne . layers import InputLayer <EOL> from lasagne . layers import Layer <EOL> from lasagne . nonlinearities import identity <EOL> from lasagne . nonlinearities import softmax <EOL> from lasagne . objectives import categorical_crossentropy <EOL> from lasagne . updates import nesterov_momentum <EOL> from mock import Mock <EOL> from mock import patch <EOL> import numpy as np <EOL> import pytest <EOL> from sklearn . base import clone <EOL> from sklearn . datasets import make_classification <EOL> from sklearn . datasets import make_regression <EOL> from sklearn . grid_search import GridSearchCV <EOL> from sklearn . metrics import accuracy_score <EOL> from sklearn . metrics import mean_absolute_error <EOL> import theano <EOL> import theano . tensor as T <EOL> floatX = theano . config . floatX <EOL> class TestLayers : <EOL> @ pytest . fixture <EOL> def layers ( self ) : <EOL> from nolearn . lasagne . base import Layers <EOL> return Layers ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) , ( '<STR_LIT>' , <NUM_LIT:3> ) ] ) <EOL> def test_getitem_with_key ( self , layers ) : <EOL> assert layers [ '<STR_LIT>' ] == <NUM_LIT:1> <EOL> def test_getitem_with_index ( self , layers ) : <EOL> assert layers [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> def test_getitem_with_slice ( self , layers ) : <EOL> from nolearn . lasagne . base import Layers <EOL> sliced = layers [ : <NUM_LIT:2> ] <EOL> assert isinstance ( sliced , Layers ) <EOL> assert sliced . keys ( ) == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert sliced . values ( ) == [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> def test_keys_returns_list ( self , layers ) : <EOL> assert layers . keys ( ) == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_values_returns_list ( self , layers ) : <EOL> assert layers . values ( ) == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> class TestFunctionalToy : <EOL> def classif ( self , NeuralNet , X , y ) : <EOL> l = InputLayer ( shape = ( None , X . shape [ <NUM_LIT:1> ] ) ) <EOL> l = DenseLayer ( l , num_units = len ( np . unique ( y ) ) , nonlinearity = softmax ) <EOL> net = NeuralNet ( l , update_learning_rate = <NUM_LIT> ) <EOL> return net . fit ( X , y ) <EOL> def classif_no_valid ( self , NeuralNet , X , y ) : <EOL> from nolearn . lasagne import TrainSplit <EOL> l = InputLayer ( shape = ( None , X . shape [ <NUM_LIT:1> ] ) ) <EOL> l = DenseLayer ( l , num_units = len ( np . unique ( y ) ) , nonlinearity = softmax ) <EOL> net = NeuralNet ( <EOL> l , update_learning_rate = <NUM_LIT> , train_split = TrainSplit ( <NUM_LIT:0> ) ) <EOL> return net . fit ( X , y ) <EOL> def regr ( self , NeuralNet , X , y ) : <EOL> l = InputLayer ( shape = ( None , X . shape [ <NUM_LIT:1> ] ) ) <EOL> l = DenseLayer ( l , num_units = y . shape [ <NUM_LIT:1> ] , nonlinearity = None ) <EOL> net = NeuralNet ( l , regression = True , update_learning_rate = <NUM_LIT> ) <EOL> return net . fit ( X , y ) <EOL> def test_classif_two_classes ( self , NeuralNet ) : <EOL> X , y = make_classification ( ) <EOL> X = X . astype ( floatX ) <EOL> y = y . astype ( np . int32 ) <EOL> self . classif ( NeuralNet , X , y ) <EOL> def test_classif_ten_classes ( self , NeuralNet ) : <EOL> X , y = make_classification ( n_classes = <NUM_LIT:10> , n_informative = <NUM_LIT:10> ) <EOL> X = X . astype ( floatX ) <EOL> y = y . astype ( np . int32 ) <EOL> self . classif ( NeuralNet , X , y ) <EOL> def test_classif_no_valid_two_classes ( self , NeuralNet ) : <EOL> X , y = make_classification ( ) <EOL> X = X . astype ( floatX ) <EOL> y = y . astype ( np . int32 ) <EOL> self . classif_no_valid ( NeuralNet , X , y ) <EOL> def test_regr_one_target ( self , NeuralNet ) : <EOL> X , y = make_regression ( ) <EOL> X = X . astype ( floatX ) <EOL> y = y . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) . astype ( np . float32 ) <EOL> self . regr ( NeuralNet , X , y ) <EOL> def test_regr_ten_targets ( self , NeuralNet ) : <EOL> X , y = make_regression ( n_targets = <NUM_LIT:10> ) <EOL> X = X . astype ( floatX ) <EOL> y = y . astype ( floatX ) <EOL> self . regr ( NeuralNet , X , y ) <EOL> class TestFunctionalMNIST : <EOL> def test_accuracy ( self , net_fitted , mnist , y_pred ) : <EOL> X , y = mnist <EOL> y_test = y [ <NUM_LIT> : ] <EOL> assert accuracy_score ( y_pred , y_test ) > <NUM_LIT> <EOL> def test_train_history ( self , net_fitted ) : <EOL> history = net_fitted . train_history_ <EOL> assert len ( history ) == <NUM_LIT:2> <EOL> assert history [ <NUM_LIT:1> ] [ '<STR_LIT>' ] > <NUM_LIT> <EOL> assert history [ <NUM_LIT:1> ] [ '<STR_LIT>' ] > history [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> assert set ( history [ <NUM_LIT:0> ] . keys ( ) ) == set ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] ) <EOL> def test_early_stopping ( self , net_fitted ) : <EOL> early_stopping = net_fitted . on_epoch_finished [ <NUM_LIT:0> ] <EOL> assert early_stopping . train_history == net_fitted . train_history_ <EOL> def test_pickle ( self , net_fitted , X_test , y_pred ) : <EOL> recursionlimit = sys . getrecursionlimit ( ) <EOL> sys . setrecursionlimit ( <NUM_LIT> ) <EOL> pickled = pickle . dumps ( net_fitted , - <NUM_LIT:1> ) <EOL> net_loaded = pickle . loads ( pickled ) <EOL> assert np . array_equal ( net_loaded . predict ( X_test ) , y_pred ) <EOL> sys . setrecursionlimit ( recursionlimit ) <EOL> def test_load_params_from_net ( self , net , net_fitted , X_test , y_pred ) : <EOL> net_loaded = clone ( net ) <EOL> net_loaded . load_params_from ( net_fitted ) <EOL> assert np . array_equal ( net_loaded . predict ( X_test ) , y_pred ) <EOL> def test_load_params_from_params_values ( self , net , net_fitted , <EOL> X_test , y_pred ) : <EOL> net_loaded = clone ( net ) <EOL> net_loaded . load_params_from ( net_fitted . get_all_params_values ( ) ) <EOL> assert np . array_equal ( net_loaded . predict ( X_test ) , y_pred ) <EOL> def test_save_params_to_path ( self , net_fitted , X_test , y_pred ) : <EOL> path = '<STR_LIT>' <EOL> net_fitted . save_params_to ( path ) <EOL> net_loaded = clone ( net_fitted ) <EOL> net_loaded . load_params_from ( path ) <EOL> assert np . array_equal ( net_loaded . predict ( X_test ) , y_pred ) <EOL> def test_load_params_from_message ( self , net , net_fitted , capsys ) : <EOL> net2 = clone ( net ) <EOL> net2 . verbose = <NUM_LIT:1> <EOL> net2 . load_params_from ( net_fitted ) <EOL> out = capsys . readouterr ( ) [ <NUM_LIT:0> ] <EOL> message = """<STR_LIT>""" <EOL> assert out == message <EOL> def test_partial_fit ( self , net , X_train , y_train ) : <EOL> net2 = clone ( net ) <EOL> assert net2 . partial_fit ( X_train , y_train ) is net2 <EOL> net2 . partial_fit ( X_train , y_train ) <EOL> history = net2 . train_history_ <EOL> assert len ( history ) == <NUM_LIT:2> <EOL> assert history [ <NUM_LIT:1> ] [ '<STR_LIT>' ] > <NUM_LIT> <EOL> def test_lasagne_functional_grid_search ( mnist , monkeypatch ) : <EOL> from nolearn . lasagne import NeuralNet <EOL> nn = NeuralNet ( <EOL> layers = [ ] , <EOL> ) <EOL> param_grid = { <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : <NUM_LIT:100> } , { '<STR_LIT>' : <NUM_LIT:200> } ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] , <EOL> } <EOL> X , y = mnist <EOL> vars_hist = [ ] <EOL> def fit ( self , X , y ) : <EOL> vars_hist . append ( vars ( self ) . copy ( ) ) <EOL> return self <EOL> with patch . object ( NeuralNet , '<STR_LIT>' , autospec = True ) as mock_fit : <EOL> mock_fit . side_effect = fit <EOL> with patch ( '<STR_LIT>' ) as score : <EOL> score . return_value = <NUM_LIT> <EOL> gs = GridSearchCV ( nn , param_grid , cv = <NUM_LIT:2> , refit = False , verbose = <NUM_LIT:4> ) <EOL> gs . fit ( X , y ) <EOL> assert [ entry [ '<STR_LIT>' ] for entry in vars_hist ] == [ <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] * <NUM_LIT:2> <EOL> assert [ entry [ '<STR_LIT>' ] for entry in vars_hist ] == ( <EOL> [ { '<STR_LIT>' : <NUM_LIT:100> } ] * <NUM_LIT:4> + <EOL> [ { '<STR_LIT>' : <NUM_LIT:200> } ] * <NUM_LIT:4> <EOL> ) <EOL> def test_clone ( ) : <EOL> from nolearn . lasagne import NeuralNet <EOL> from nolearn . lasagne import BatchIterator <EOL> from nolearn . lasagne import objective <EOL> params = dict ( <EOL> layers = [ <EOL> ( '<STR_LIT:input>' , InputLayer ) , <EOL> ( '<STR_LIT>' , DenseLayer ) , <EOL> ( '<STR_LIT>' , DenseLayer ) , <EOL> ] , <EOL> input_shape = ( <NUM_LIT:100> , <NUM_LIT> ) , <EOL> output_num_units = <NUM_LIT:10> , <EOL> output_nonlinearity = softmax , <EOL> more_params = { <EOL> '<STR_LIT>' : <NUM_LIT:100> , <EOL> } , <EOL> update = nesterov_momentum , <EOL> update_learning_rate = <NUM_LIT> , <EOL> update_momentum = <NUM_LIT> , <EOL> regression = False , <EOL> objective = objective , <EOL> objective_loss_function = categorical_crossentropy , <EOL> batch_iterator_train = BatchIterator ( batch_size = <NUM_LIT:100> ) , <EOL> y_tensor_type = T . ivector , <EOL> use_label_encoder = False , <EOL> on_epoch_finished = None , <EOL> on_training_finished = None , <EOL> max_epochs = <NUM_LIT:100> , <EOL> eval_size = <NUM_LIT:0.1> , <EOL> check_input = True , <EOL> verbose = <NUM_LIT:0> , <EOL> ) <EOL> nn = NeuralNet ( ** params ) <EOL> nn2 = clone ( nn ) <EOL> params1 = nn . get_params ( ) <EOL> params2 = nn2 . get_params ( ) <EOL> for ignore in ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) : <EOL> for par in ( params , params1 , params2 ) : <EOL> par . pop ( ignore , None ) <EOL> assert params == params1 == params2 <EOL> def test_lasagne_functional_regression ( boston ) : <EOL> from nolearn . lasagne import NeuralNet <EOL> X , y = boston <EOL> layer1 = InputLayer ( shape = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> layer2 = DenseLayer ( layer1 , num_units = <NUM_LIT:100> ) <EOL> output = DenseLayer ( layer2 , num_units = <NUM_LIT:1> , nonlinearity = identity ) <EOL> nn = NeuralNet ( <EOL> layers = output , <EOL> update_learning_rate = <NUM_LIT> , <EOL> update_momentum = <NUM_LIT:0.1> , <EOL> regression = True , <EOL> max_epochs = <NUM_LIT:50> , <EOL> ) <EOL> nn . fit ( X [ : <NUM_LIT> ] , y [ : <NUM_LIT> ] ) <EOL> assert mean_absolute_error ( nn . predict ( X [ <NUM_LIT> : ] ) , y [ <NUM_LIT> : ] ) < <NUM_LIT> <EOL> class TestDefaultObjective : <EOL> @ pytest . fixture <EOL> def get_output ( self , monkeypatch ) : <EOL> from nolearn . lasagne import base <EOL> get_output_mock = Mock ( ) <EOL> monkeypatch . setattr ( base , '<STR_LIT>' , get_output_mock ) <EOL> return get_output_mock <EOL> @ pytest . fixture <EOL> def objective ( self ) : <EOL> from nolearn . lasagne . base import objective <EOL> return objective <EOL> def test_with_defaults ( self , objective , get_output ) : <EOL> loss_function , target = Mock ( ) , Mock ( ) <EOL> loss_function . return_value = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> result = objective ( <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , loss_function = loss_function , target = target ) <EOL> assert result == <NUM_LIT> <EOL> get_output . assert_called_with ( <NUM_LIT:3> , deterministic = False ) <EOL> loss_function . assert_called_with ( get_output . return_value , target ) <EOL> def test_with_get_output_kw ( self , objective , get_output ) : <EOL> loss_function , target = Mock ( ) , Mock ( ) <EOL> loss_function . return_value = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> objective ( <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , loss_function = loss_function , target = target , <EOL> get_output_kw = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> ) <EOL> get_output . assert_called_with ( <NUM_LIT:3> , deterministic = False , i_was = '<STR_LIT>' ) <EOL> class TestTrainSplit : <EOL> @ pytest . fixture <EOL> def TrainSplit ( self ) : <EOL> from nolearn . lasagne import TrainSplit <EOL> return TrainSplit <EOL> def test_reproducable ( self , TrainSplit , nn ) : <EOL> X , y = np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT> ) <EOL> X_train1 , X_valid1 , y_train1 , y_valid1 = TrainSplit ( <NUM_LIT> ) ( <EOL> X , y , nn ) <EOL> X_train2 , X_valid2 , y_train2 , y_valid2 = TrainSplit ( <NUM_LIT> ) ( <EOL> X , y , nn ) <EOL> assert np . all ( X_train1 == X_train2 ) <EOL> assert np . all ( y_valid1 == y_valid2 ) <EOL> def test_eval_size_zero ( self , TrainSplit , nn ) : <EOL> X , y = np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT> ) <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT:0.0> ) ( <EOL> X , y , nn ) <EOL> assert len ( X_train ) == len ( X ) <EOL> assert len ( y_train ) == len ( y ) <EOL> assert len ( X_valid ) == <NUM_LIT:0> <EOL> assert len ( y_valid ) == <NUM_LIT:0> <EOL> def test_eval_size_half ( self , TrainSplit , nn ) : <EOL> X , y = np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT> ) <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT> ) ( <EOL> X , y , nn ) <EOL> assert len ( X_train ) + len ( X_valid ) == <NUM_LIT:100> <EOL> assert len ( y_train ) + len ( y_valid ) == <NUM_LIT:100> <EOL> assert len ( X_train ) > <NUM_LIT> <EOL> def test_regression ( self , TrainSplit , nn ) : <EOL> X = np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) <EOL> y = np . random . random ( ( <NUM_LIT:100> ) ) <EOL> nn . regression = True <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT> ) ( <EOL> X , y , nn ) <EOL> assert len ( X_train ) == len ( y_train ) == <NUM_LIT> <EOL> assert len ( X_valid ) == len ( y_valid ) == <NUM_LIT:20> <EOL> def test_stratified ( self , TrainSplit , nn ) : <EOL> X = np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) <EOL> y = np . hstack ( [ np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT> ) , np . repeat ( [ <NUM_LIT:1> ] , <NUM_LIT> ) ] ) <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT> ) ( <EOL> X , y , nn ) <EOL> assert y_train . sum ( ) == <NUM_LIT> * <NUM_LIT> <EOL> assert y_valid . sum ( ) == <NUM_LIT> * <NUM_LIT> <EOL> def test_not_stratified ( self , TrainSplit , nn ) : <EOL> X = np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) <EOL> y = np . hstack ( [ np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT> ) , np . repeat ( [ <NUM_LIT:1> ] , <NUM_LIT> ) ] ) <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT> , stratify = False ) ( <EOL> X , y , nn ) <EOL> assert y_train . sum ( ) == <NUM_LIT> <EOL> assert y_valid . sum ( ) == <NUM_LIT:0> <EOL> def test_X_is_dict ( self , TrainSplit , nn ) : <EOL> X = { <EOL> '<STR_LIT:1>' : np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , <EOL> '<STR_LIT:2>' : np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , <EOL> } <EOL> y = np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT> ) <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT> ) ( <EOL> X , y , nn ) <EOL> assert len ( X_train [ '<STR_LIT:1>' ] ) == len ( X_train [ '<STR_LIT:2>' ] ) == len ( y_train ) == <NUM_LIT> <EOL> assert len ( X_valid [ '<STR_LIT:1>' ] ) == len ( X_valid [ '<STR_LIT:2>' ] ) == len ( y_valid ) == <NUM_LIT:20> <EOL> def test_X_is_dict_eval_size_0 ( self , TrainSplit , nn ) : <EOL> X = { <EOL> '<STR_LIT:1>' : np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , <EOL> '<STR_LIT:2>' : np . random . random ( ( <NUM_LIT:100> , <NUM_LIT:10> ) ) , <EOL> } <EOL> y = np . repeat ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT> ) <EOL> X_train , X_valid , y_train , y_valid = TrainSplit ( <NUM_LIT:0> ) ( <EOL> X , y , nn ) <EOL> assert len ( X_train [ '<STR_LIT:1>' ] ) == len ( X_train [ '<STR_LIT:2>' ] ) == len ( y_train ) == <NUM_LIT:100> <EOL> assert len ( X_valid [ '<STR_LIT:1>' ] ) == len ( X_valid [ '<STR_LIT:2>' ] ) == len ( y_valid ) == <NUM_LIT:0> <EOL> class TestTrainTestSplitBackwardCompatibility : <EOL> @ pytest . fixture <EOL> def LegacyNet ( self , NeuralNet ) : <EOL> class LegacyNet ( NeuralNet ) : <EOL> def train_test_split ( self , X , y , eval_size ) : <EOL> self . __call_args__ = ( X , y , eval_size ) <EOL> split = int ( X . shape [ <NUM_LIT:0> ] * eval_size ) <EOL> return X [ : split ] , X [ split : ] , y [ : split ] , y [ split : ] <EOL> return LegacyNet <EOL> def test_legacy_eval_size ( self , NeuralNet ) : <EOL> net = NeuralNet ( [ ] , eval_size = <NUM_LIT> , max_epochs = <NUM_LIT:0> ) <EOL> assert net . train_split . eval_size == <NUM_LIT> <EOL> def test_legacy_method_default_eval_size ( self , LegacyNet ) : <EOL> net = LegacyNet ( [ ] , max_epochs = <NUM_LIT:0> ) <EOL> X , y = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:3> ) ) , np . zeros ( <NUM_LIT:10> ) <EOL> net . train_loop ( X , y ) <EOL> assert net . __call_args__ == ( X , y , <NUM_LIT> ) <EOL> def test_legacy_method_given_eval_size ( self , LegacyNet ) : <EOL> net = LegacyNet ( [ ] , eval_size = <NUM_LIT> , max_epochs = <NUM_LIT:0> ) <EOL> X , y = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:3> ) ) , np . zeros ( <NUM_LIT:10> ) <EOL> net . train_loop ( X , y ) <EOL> assert net . __call_args__ == ( X , y , <NUM_LIT> ) <EOL> class TestBatchIterator : <EOL> @ pytest . fixture <EOL> def BatchIterator ( self ) : <EOL> from nolearn . lasagne import BatchIterator <EOL> return BatchIterator <EOL> @ pytest . fixture <EOL> def X ( self ) : <EOL> return np . arange ( <NUM_LIT:200> ) . reshape ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) . T . astype ( '<STR_LIT:float>' ) <EOL> @ pytest . fixture <EOL> def X_dict ( self ) : <EOL> return { <EOL> '<STR_LIT>' : np . arange ( <NUM_LIT:200> ) . reshape ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) . T . astype ( '<STR_LIT:float>' ) , <EOL> '<STR_LIT>' : np . arange ( <NUM_LIT:200> ) . reshape ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) . astype ( '<STR_LIT:float>' ) , <EOL> } <EOL> @ pytest . fixture <EOL> def y ( self ) : <EOL> return np . arange ( <NUM_LIT:20> ) <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ True , False ] ) <EOL> def test_simple_x_and_y ( self , BatchIterator , X , y , shuffle ) : <EOL> bi = BatchIterator ( <NUM_LIT:2> , shuffle = shuffle ) ( X , y ) <EOL> batches = list ( bi ) <EOL> assert len ( batches ) == <NUM_LIT:10> <EOL> X0 , y0 = batches [ <NUM_LIT:0> ] <EOL> assert X0 . shape == ( <NUM_LIT:2> , <NUM_LIT:10> ) <EOL> assert y0 . shape == ( <NUM_LIT:2> , ) <EOL> Xt = np . vstack ( b [ <NUM_LIT:0> ] for b in batches ) <EOL> yt = np . hstack ( b [ <NUM_LIT:1> ] for b in batches ) <EOL> assert Xt . shape == X . shape <EOL> assert yt . shape == y . shape <EOL> np . testing . assert_equal ( Xt [ : , <NUM_LIT:0> ] , yt ) <EOL> if shuffle is False : <EOL> np . testing . assert_equal ( X [ : <NUM_LIT:2> ] , X0 ) <EOL> np . testing . assert_equal ( y [ : <NUM_LIT:2> ] , y0 ) <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ True , False ] ) <EOL> def test_simple_x_no_y ( self , BatchIterator , X , shuffle ) : <EOL> bi = BatchIterator ( <NUM_LIT:2> , shuffle = shuffle ) ( X ) <EOL> batches = list ( bi ) <EOL> assert len ( batches ) == <NUM_LIT:10> <EOL> X0 , y0 = batches [ <NUM_LIT:0> ] <EOL> assert X0 . shape == ( <NUM_LIT:2> , <NUM_LIT:10> ) <EOL> assert y0 is None <EOL> if shuffle is False : <EOL> np . testing . assert_equal ( X [ : <NUM_LIT:2> ] , X0 ) <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ True , False ] ) <EOL> def test_X_is_dict ( self , BatchIterator , X_dict , shuffle ) : <EOL> bi = BatchIterator ( <NUM_LIT:2> , shuffle = shuffle ) ( X_dict ) <EOL> batches = list ( bi ) <EOL> assert len ( batches ) == <NUM_LIT:10> <EOL> X0 , y0 = batches [ <NUM_LIT:0> ] <EOL> assert X0 [ '<STR_LIT>' ] . shape == ( <NUM_LIT:2> , <NUM_LIT:10> ) <EOL> assert X0 [ '<STR_LIT>' ] . shape == ( <NUM_LIT:2> , <NUM_LIT:10> ) <EOL> assert y0 is None <EOL> Xt1 = np . vstack ( b [ <NUM_LIT:0> ] [ '<STR_LIT>' ] for b in batches ) <EOL> Xt2 = np . vstack ( b [ <NUM_LIT:0> ] [ '<STR_LIT>' ] for b in batches ) <EOL> assert Xt1 . shape == X_dict [ '<STR_LIT>' ] . shape <EOL> assert Xt2 . shape == X_dict [ '<STR_LIT>' ] . shape <EOL> np . testing . assert_equal ( Xt1 [ : , <NUM_LIT:0> ] , Xt2 [ : , <NUM_LIT:0> ] / <NUM_LIT:10> ) <EOL> if shuffle is False : <EOL> np . testing . assert_equal ( X_dict [ '<STR_LIT>' ] [ : <NUM_LIT:2> ] , X0 [ '<STR_LIT>' ] ) <EOL> np . testing . assert_equal ( X_dict [ '<STR_LIT>' ] [ : <NUM_LIT:2> ] , X0 [ '<STR_LIT>' ] ) <EOL> def test_shuffle_no_copy ( self , BatchIterator , X , y ) : <EOL> bi = BatchIterator ( <NUM_LIT:2> , shuffle = True ) ( X , y ) <EOL> X0 , y0 = list ( bi ) [ <NUM_LIT:0> ] <EOL> assert X0 . base is X <EOL> class TestCheckForUnusedKwargs : <EOL> def test_okay ( self , NeuralNet ) : <EOL> net = NeuralNet ( <EOL> layers = [ ( '<STR_LIT:input>' , Mock ) , ( '<STR_LIT>' , Mock ) ] , <EOL> input_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) , <EOL> mylayer_hey = '<STR_LIT>' , <EOL> update_foo = <NUM_LIT:1> , <EOL> update_bar = <NUM_LIT:2> , <EOL> ) <EOL> net . _create_iter_funcs = lambda * args : ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> net . initialize ( ) <EOL> def test_unused ( self , NeuralNet ) : <EOL> net = NeuralNet ( <EOL> layers = [ ( '<STR_LIT:input>' , Mock ) , ( '<STR_LIT>' , Mock ) ] , <EOL> input_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) , <EOL> mylayer_hey = '<STR_LIT>' , <EOL> yourlayer_ho = '<STR_LIT>' , <EOL> update_foo = <NUM_LIT:1> , <EOL> update_bar = <NUM_LIT:2> , <EOL> ) <EOL> net . _create_iter_funcs = lambda * args : ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> with pytest . raises ( ValueError ) as err : <EOL> net . initialize ( ) <EOL> assert str ( err . value ) == '<STR_LIT>' <EOL> class TestInitializeLayers : <EOL> def test_initialization_with_layer_instance ( self , NeuralNet ) : <EOL> layer1 = InputLayer ( shape = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> layer2 = DenseLayer ( layer1 , name = '<STR_LIT>' , num_units = <NUM_LIT:2> ) <EOL> nn = NeuralNet ( layers = layer2 ) <EOL> out = nn . initialize_layers ( ) <EOL> assert nn . layers_ [ '<STR_LIT>' ] == layer2 == out <EOL> assert nn . layers_ [ '<STR_LIT>' ] == layer1 <EOL> def test_initialization_with_layer_instance_bad_params ( self , NeuralNet ) : <EOL> layer = DenseLayer ( InputLayer ( shape = ( <NUM_LIT> , <NUM_LIT> ) ) , num_units = <NUM_LIT:2> ) <EOL> nn = NeuralNet ( layers = layer , dense1_num_units = <NUM_LIT:3> ) <EOL> with pytest . raises ( ValueError ) : <EOL> nn . initialize_layers ( ) <EOL> def test_initialization_with_tuples ( self , NeuralNet ) : <EOL> input = Mock ( __name__ = '<STR_LIT>' , __bases__ = ( InputLayer , ) ) <EOL> hidden1 , hidden2 , output = [ <EOL> Mock ( __name__ = '<STR_LIT>' , __bases__ = ( Layer , ) ) for i in range ( <NUM_LIT:3> ) ] <EOL> nn = NeuralNet ( <EOL> layers = [ <EOL> ( input , { '<STR_LIT>' : ( <NUM_LIT:10> , <NUM_LIT:10> ) , '<STR_LIT:name>' : '<STR_LIT:input>' } ) , <EOL> ( hidden1 , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( hidden2 , { } ) , <EOL> ( output , { '<STR_LIT:name>' : '<STR_LIT>' } ) , <EOL> ] , <EOL> input_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) , <EOL> mock1_some = '<STR_LIT>' , <EOL> ) <EOL> out = nn . initialize_layers ( nn . layers ) <EOL> input . assert_called_with ( <EOL> name = '<STR_LIT:input>' , shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> assert nn . layers_ [ '<STR_LIT:input>' ] is input . return_value <EOL> hidden1 . assert_called_with ( <EOL> incoming = input . return_value , name = '<STR_LIT>' , <EOL> some = '<STR_LIT>' , another = '<STR_LIT>' ) <EOL> assert nn . layers_ [ '<STR_LIT>' ] is hidden1 . return_value <EOL> hidden2 . assert_called_with ( <EOL> incoming = hidden1 . return_value , name = '<STR_LIT>' ) <EOL> assert nn . layers_ [ '<STR_LIT>' ] is hidden2 . return_value <EOL> output . assert_called_with ( <EOL> incoming = hidden2 . return_value , name = '<STR_LIT>' ) <EOL> assert out is nn . layers_ [ '<STR_LIT>' ] <EOL> def test_initialization_legacy ( self , NeuralNet ) : <EOL> input = Mock ( __name__ = '<STR_LIT>' , __bases__ = ( InputLayer , ) ) <EOL> hidden1 , hidden2 , output = [ <EOL> Mock ( __name__ = '<STR_LIT>' , __bases__ = ( Layer , ) ) for i in range ( <NUM_LIT:3> ) ] <EOL> nn = NeuralNet ( <EOL> layers = [ <EOL> ( '<STR_LIT:input>' , input ) , <EOL> ( '<STR_LIT>' , hidden1 ) , <EOL> ( '<STR_LIT>' , hidden2 ) , <EOL> ( '<STR_LIT>' , output ) , <EOL> ] , <EOL> input_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) , <EOL> hidden1_some = '<STR_LIT>' , <EOL> ) <EOL> out = nn . initialize_layers ( nn . layers ) <EOL> input . assert_called_with ( <EOL> name = '<STR_LIT:input>' , shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> assert nn . layers_ [ '<STR_LIT:input>' ] is input . return_value <EOL> hidden1 . assert_called_with ( <EOL> incoming = input . return_value , name = '<STR_LIT>' , some = '<STR_LIT>' ) <EOL> assert nn . layers_ [ '<STR_LIT>' ] is hidden1 . return_value <EOL> hidden2 . assert_called_with ( <EOL> incoming = hidden1 . return_value , name = '<STR_LIT>' ) <EOL> assert nn . layers_ [ '<STR_LIT>' ] is hidden2 . return_value <EOL> output . assert_called_with ( <EOL> incoming = hidden2 . return_value , name = '<STR_LIT>' ) <EOL> assert out is nn . layers_ [ '<STR_LIT>' ] <EOL> def test_initialization_legacy_with_unicode_names ( self , NeuralNet ) : <EOL> input = Mock ( __name__ = '<STR_LIT>' , __bases__ = ( InputLayer , ) ) <EOL> hidden1 , hidden2 , output = [ <EOL> Mock ( __name__ = '<STR_LIT>' , __bases__ = ( Layer , ) ) for i in range ( <NUM_LIT:3> ) ] <EOL> nn = NeuralNet ( <EOL> layers = [ <EOL> ( u'<STR_LIT:input>' , input ) , <EOL> ( u'<STR_LIT>' , hidden1 ) , <EOL> ( u'<STR_LIT>' , hidden2 ) , <EOL> ( u'<STR_LIT>' , output ) , <EOL> ] , <EOL> input_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) , <EOL> hidden1_some = '<STR_LIT>' , <EOL> ) <EOL> nn . initialize_layers ( ) <EOL> def test_diamond ( self , NeuralNet ) : <EOL> input = Mock ( __name__ = '<STR_LIT>' , __bases__ = ( InputLayer , ) ) <EOL> hidden1 , hidden2 , concat , output = [ <EOL> Mock ( __name__ = '<STR_LIT>' , __bases__ = ( Layer , ) ) for i in range ( <NUM_LIT:4> ) ] <EOL> nn = NeuralNet ( <EOL> layers = [ <EOL> ( '<STR_LIT:input>' , input ) , <EOL> ( '<STR_LIT>' , hidden1 ) , <EOL> ( '<STR_LIT>' , hidden2 ) , <EOL> ( '<STR_LIT>' , concat ) , <EOL> ( '<STR_LIT>' , output ) , <EOL> ] , <EOL> input_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) , <EOL> hidden2_incoming = '<STR_LIT:input>' , <EOL> concat_incomings = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ) <EOL> nn . initialize_layers ( nn . layers ) <EOL> input . assert_called_with ( name = '<STR_LIT:input>' , shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> hidden1 . assert_called_with ( incoming = input . return_value , name = '<STR_LIT>' ) <EOL> hidden2 . assert_called_with ( incoming = input . return_value , name = '<STR_LIT>' ) <EOL> concat . assert_called_with ( <EOL> incomings = [ hidden1 . return_value , hidden2 . return_value ] , <EOL> name = '<STR_LIT>' <EOL> ) <EOL> output . assert_called_with ( incoming = concat . return_value , name = '<STR_LIT>' ) <EOL> class TestCheckGoodInput : <EOL> @ pytest . fixture <EOL> def check_good_input ( self , nn ) : <EOL> return nn . _check_good_input <EOL> @ pytest . fixture <EOL> def X ( self ) : <EOL> return np . arange ( <NUM_LIT:100> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:10> ) . astype ( floatX ) <EOL> @ pytest . fixture <EOL> def y ( self ) : <EOL> return np . arange ( <NUM_LIT:10> ) . astype ( np . int32 ) <EOL> @ pytest . fixture <EOL> def y_regr ( self ) : <EOL> return np . arange ( <NUM_LIT:10> ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) . astype ( floatX ) <EOL> def test_X_OK ( self , check_good_input , X ) : <EOL> assert check_good_input ( X ) == ( X , None ) <EOL> def test_X_and_y_OK ( self , check_good_input , X , y ) : <EOL> assert check_good_input ( X , y ) == ( X , y ) <EOL> def test_X_and_y_OK_regression ( self , nn , check_good_input , X , y_regr ) : <EOL> nn . regression = True <EOL> assert check_good_input ( X , y_regr ) == ( X , y_regr ) <EOL> def test_X_and_y_length_mismatch ( self , check_good_input , X , y ) : <EOL> with pytest . raises ( ValueError ) : <EOL> check_good_input ( <EOL> X [ : <NUM_LIT:9> ] , <EOL> y <EOL> ) <EOL> def test_X_dict_and_y_length_mismatch ( self , check_good_input , X , y ) : <EOL> with pytest . raises ( ValueError ) : <EOL> check_good_input ( <EOL> { '<STR_LIT>' : X , '<STR_LIT>' : X } , <EOL> y [ : <NUM_LIT:9> ] , <EOL> ) <EOL> def test_X_dict_length_mismatch ( self , check_good_input , X ) : <EOL> with pytest . raises ( ValueError ) : <EOL> check_good_input ( { <EOL> '<STR_LIT>' : X , <EOL> '<STR_LIT>' : X [ : <NUM_LIT:9> ] , <EOL> } ) <EOL> def test_y_regression_1dim ( self , nn , check_good_input , X , y_regr ) : <EOL> y = y_regr . reshape ( - <NUM_LIT:1> ) <EOL> nn . regression = True <EOL> X1 , y1 = check_good_input ( X , y ) <EOL> assert ( X1 == X ) . all ( ) <EOL> assert ( y1 == y . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) . all ( ) <EOL> def test_y_regression_2dim ( self , nn , check_good_input , X , y_regr ) : <EOL> y = y_regr <EOL> nn . regression = True <EOL> X1 , y1 = check_good_input ( X , y ) <EOL> assert ( X1 == X ) . all ( ) <EOL> assert ( y1 == y ) . all ( ) <EOL> class TestGetOutput : <EOL> def test_layer_object ( self , net_fitted , X_train ) : <EOL> layer = net_fitted . layers_ [ '<STR_LIT>' ] <EOL> output = net_fitted . get_output ( layer , X_train [ : <NUM_LIT:3> ] ) <EOL> assert output . shape == ( <NUM_LIT:3> , <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> def test_layer_name ( self , net_fitted , X_train ) : <EOL> output = net_fitted . get_output ( '<STR_LIT>' , X_train [ : <NUM_LIT:3> ] ) <EOL> assert output . shape == ( <NUM_LIT:3> , <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> def test_get_output_last_layer ( self , net_fitted , X_train ) : <EOL> result = net_fitted . get_output ( net_fitted . layers_ [ - <NUM_LIT:1> ] , X_train [ : <NUM_LIT> ] ) <EOL> expected = net_fitted . predict_proba ( X_train [ : <NUM_LIT> ] ) <EOL> np . testing . assert_equal ( result , expected ) <EOL> def test_no_conv ( self , net_no_conv ) : <EOL> net_no_conv . initialize ( ) <EOL> X = np . random . random ( ( <NUM_LIT:10> , <NUM_LIT:100> ) ) . astype ( floatX ) <EOL> result = net_no_conv . get_output ( '<STR_LIT>' , X ) <EOL> expected = net_no_conv . predict_proba ( X ) <EOL> np . testing . assert_equal ( result , expected ) <EOL> class TestMultiInputFunctional : <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def net ( self , NeuralNet ) : <EOL> return NeuralNet ( <EOL> layers = [ <EOL> ( InputLayer , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : ( None , <NUM_LIT> ) } ) , <EOL> ( DenseLayer , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( InputLayer , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : ( None , <NUM_LIT> ) } ) , <EOL> ( DenseLayer , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( ConcatLayer , <EOL> { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } ) , <EOL> ( DenseLayer , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( DenseLayer , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : softmax } ) , <EOL> ] , <EOL> update = nesterov_momentum , <EOL> update_learning_rate = <NUM_LIT> , <EOL> update_momentum = <NUM_LIT> , <EOL> max_epochs = <NUM_LIT:2> , <EOL> verbose = <NUM_LIT:4> , <EOL> ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def net_fitted ( self , net , mnist ) : <EOL> X , y = mnist <EOL> X_train , y_train = X [ : <NUM_LIT> ] , y [ : <NUM_LIT> ] <EOL> X_train1 , X_train2 = X_train [ : , : <NUM_LIT> ] , X_train [ : , <NUM_LIT> : ] <EOL> return net . fit ( { '<STR_LIT>' : X_train1 , '<STR_LIT>' : X_train2 } , y_train ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def y_pred ( self , net_fitted , mnist ) : <EOL> X , y = mnist <EOL> X_test = X [ <NUM_LIT> : ] <EOL> X_test1 , X_test2 = X_test [ : , : <NUM_LIT> ] , X_test [ : , <NUM_LIT> : ] <EOL> return net_fitted . predict ( { '<STR_LIT>' : X_test1 , '<STR_LIT>' : X_test2 } ) <EOL> def test_accuracy ( self , net_fitted , mnist , y_pred ) : <EOL> X , y = mnist <EOL> y_test = y [ <NUM_LIT> : ] <EOL> assert accuracy_score ( y_pred , y_test ) > <NUM_LIT> <EOL> class TestGradScale : <EOL> @ pytest . fixture <EOL> def grad_scale ( self ) : <EOL> from nolearn . lasagne import grad_scale <EOL> return grad_scale <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ <EOL> BatchNormLayer ( InputLayer ( ( None , <NUM_LIT:16> ) ) ) , <EOL> Conv2DLayer ( InputLayer ( ( None , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) ) , <NUM_LIT:2> , <NUM_LIT:3> ) , <EOL> DenseLayer ( InputLayer ( ( None , <NUM_LIT:16> ) ) , <NUM_LIT:16> ) , <EOL> ] ) <EOL> def test_it ( self , grad_scale , layer ) : <EOL> layer2 = grad_scale ( layer , <NUM_LIT> ) <EOL> assert layer2 is layer <EOL> for param in layer . get_params ( trainable = True ) : <EOL> np . testing . assert_almost_equal ( param . tag . grad_scale , <NUM_LIT> ) <EOL> for param in layer . get_params ( trainable = False ) : <EOL> assert hasattr ( param . tag , '<STR_LIT>' ) is False </s>
