<s> import pytest <EOL> import numpy as np <EOL> import theano . tensor as T <EOL> import lasagne <EOL> from collections import OrderedDict <EOL> from theano . scan_module . scan_utils import equal_computations <EOL> from mock import Mock <EOL> class TestRegularizationPenalties ( object ) : <EOL> def l1 ( self , x ) : <EOL> return np . abs ( x ) . sum ( ) <EOL> def l2 ( self , x ) : <EOL> return ( x ** <NUM_LIT:2> ) . sum ( ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_penalty ( self , penalty ) : <EOL> np_penalty = getattr ( self , penalty ) <EOL> theano_penalty = getattr ( lasagne . regularization , penalty ) <EOL> X = T . matrix ( ) <EOL> X0 = lasagne . utils . floatX ( np . random . uniform ( - <NUM_LIT:3> , <NUM_LIT:3> , ( <NUM_LIT:10> , <NUM_LIT:10> ) ) ) <EOL> theano_result = theano_penalty ( X ) . eval ( { X : X0 } ) <EOL> np_result = np_penalty ( X0 ) <EOL> assert np . allclose ( theano_result , np_result ) <EOL> class TestRegularizationHelpers ( object ) : <EOL> @ pytest . fixture <EOL> def layers ( self ) : <EOL> l_1 = lasagne . layers . InputLayer ( ( <NUM_LIT:10> , ) ) <EOL> l_2 = lasagne . layers . DenseLayer ( l_1 , num_units = <NUM_LIT:20> ) <EOL> l_3 = lasagne . layers . DenseLayer ( l_2 , num_units = <NUM_LIT:30> ) <EOL> return l_1 , l_2 , l_3 <EOL> def test_apply_penalty ( self ) : <EOL> from lasagne . regularization import apply_penalty , l2 <EOL> A = T . vector ( ) <EOL> B = T . matrix ( ) <EOL> assert apply_penalty ( [ ] , l2 ) == <NUM_LIT:0> <EOL> assert equal_computations ( [ apply_penalty ( A , l2 ) ] , <EOL> [ l2 ( A ) ] ) <EOL> assert equal_computations ( [ apply_penalty ( [ A , B ] , l2 ) ] , <EOL> [ sum ( [ l2 ( A ) , l2 ( B ) ] ) ] ) <EOL> def test_regularize_layer_params_single_layer ( self , layers ) : <EOL> from lasagne . regularization import regularize_layer_params <EOL> l_1 , l_2 , l_3 = layers <EOL> penalty = Mock ( return_value = <NUM_LIT:0> ) <EOL> loss = regularize_layer_params ( l_2 , penalty ) <EOL> assert penalty . call_count == <NUM_LIT:1> <EOL> penalty . assert_any_call ( l_2 . W ) <EOL> def test_regularize_layer_params_multiple_layers ( self , layers ) : <EOL> from lasagne . regularization import regularize_layer_params <EOL> l_1 , l_2 , l_3 = layers <EOL> penalty = Mock ( return_value = <NUM_LIT:0> ) <EOL> loss = regularize_layer_params ( [ l_1 , l_2 , l_3 ] , penalty ) <EOL> assert penalty . call_count == <NUM_LIT:2> <EOL> penalty . assert_any_call ( l_2 . W ) <EOL> penalty . assert_any_call ( l_3 . W ) <EOL> def test_regularize_network_params ( self , layers ) : <EOL> from lasagne . regularization import regularize_network_params <EOL> l_1 , l_2 , l_3 = layers <EOL> penalty = Mock ( return_value = <NUM_LIT:0> ) <EOL> loss = regularize_network_params ( l_3 , penalty ) <EOL> assert penalty . call_count == <NUM_LIT:2> <EOL> penalty . assert_any_call ( l_2 . W ) <EOL> penalty . assert_any_call ( l_3 . W ) <EOL> def test_regularize_layer_params_weighted ( self , layers ) : <EOL> from lasagne . regularization import regularize_layer_params_weighted <EOL> from lasagne . regularization import apply_penalty , l2 <EOL> l_1 , l_2 , l_3 = layers <EOL> layers = OrderedDict ( ) <EOL> layers [ l_2 ] = <NUM_LIT:0.1> <EOL> layers [ l_3 ] = <NUM_LIT:0.5> <EOL> loss = regularize_layer_params_weighted ( layers , <EOL> lasagne . regularization . l2 ) <EOL> assert equal_computations ( [ loss ] , <EOL> [ sum ( [ <NUM_LIT:0.1> * apply_penalty ( [ l_2 . W ] , l2 ) , <EOL> <NUM_LIT:0.5> * apply_penalty ( [ l_3 . W ] , l2 ) ] ) ] ) </s>
