<s> import warnings <EOL> from mock import Mock , PropertyMock <EOL> import pytest <EOL> import numpy <EOL> import theano <EOL> class TestGetAllLayers : <EOL> def test_stack ( self ) : <EOL> from lasagne . layers import InputLayer , DenseLayer , get_all_layers <EOL> from itertools import permutations <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> for count in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> for query in permutations ( [ l1 , l2 , l3 ] , count ) : <EOL> if l3 in query : <EOL> expected = [ l1 , l2 , l3 ] <EOL> elif l2 in query : <EOL> expected = [ l1 , l2 ] <EOL> elif l1 in query : <EOL> expected = [ l1 ] <EOL> else : <EOL> expected = [ ] <EOL> assert get_all_layers ( query ) == expected <EOL> assert get_all_layers ( l3 , treat_as_input = [ l2 ] ) == [ l2 , l3 ] <EOL> def test_merge ( self ) : <EOL> from lasagne . layers import ( InputLayer , DenseLayer , ElemwiseSumLayer , <EOL> get_all_layers ) <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> l4 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:30> ) ) <EOL> l5 = DenseLayer ( l4 , <NUM_LIT> ) <EOL> l6 = ElemwiseSumLayer ( [ l3 , l5 ] ) <EOL> assert get_all_layers ( l6 ) == [ l1 , l2 , l3 , l4 , l5 , l6 ] <EOL> assert get_all_layers ( [ l4 , l6 ] ) == [ l4 , l1 , l2 , l3 , l5 , l6 ] <EOL> assert get_all_layers ( [ l5 , l6 ] ) == [ l4 , l5 , l1 , l2 , l3 , l6 ] <EOL> assert get_all_layers ( [ l4 , l2 , l5 , l6 ] ) == [ l4 , l1 , l2 , l5 , l3 , l6 ] <EOL> assert get_all_layers ( l6 , treat_as_input = [ l2 ] ) == [ l2 , l3 , l4 , l5 , l6 ] <EOL> assert get_all_layers ( l6 , treat_as_input = [ l3 , l5 ] ) == [ l3 , l5 , l6 ] <EOL> assert get_all_layers ( [ l6 , l2 ] , treat_as_input = [ l6 ] ) == [ l6 , l1 , l2 ] <EOL> def test_split ( self ) : <EOL> from lasagne . layers import InputLayer , DenseLayer , get_all_layers <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> l4 = DenseLayer ( l1 , <NUM_LIT:50> ) <EOL> assert get_all_layers ( l3 ) == [ l1 , l2 , l3 ] <EOL> assert get_all_layers ( l4 ) == [ l1 , l4 ] <EOL> assert get_all_layers ( [ l3 , l4 ] ) == [ l1 , l2 , l3 , l4 ] <EOL> assert get_all_layers ( [ l4 , l3 ] ) == [ l1 , l4 , l2 , l3 ] <EOL> assert get_all_layers ( l3 , treat_as_input = [ l2 ] ) == [ l2 , l3 ] <EOL> assert get_all_layers ( [ l3 , l4 ] , treat_as_input = [ l2 ] ) == [ l2 , l3 , <EOL> l1 , l4 ] <EOL> def test_bridge ( self ) : <EOL> from lasagne . layers import ( InputLayer , DenseLayer , ElemwiseSumLayer , <EOL> get_all_layers ) <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT:30> ) <EOL> l4 = ElemwiseSumLayer ( [ l2 , l3 ] ) <EOL> l5 = DenseLayer ( l4 , <NUM_LIT> ) <EOL> assert get_all_layers ( l5 ) == [ l1 , l2 , l3 , l4 , l5 ] <EOL> assert get_all_layers ( l5 , treat_as_input = [ l4 ] ) == [ l4 , l5 ] <EOL> assert get_all_layers ( l5 , treat_as_input = [ l3 ] ) == [ l1 , l2 , l3 , l4 , l5 ] <EOL> class TestGetOutput_InputLayer : <EOL> @ pytest . fixture <EOL> def get_output ( self ) : <EOL> from lasagne . layers . helper import get_output <EOL> return get_output <EOL> @ pytest . fixture <EOL> def layer ( self ) : <EOL> from lasagne . layers . input import InputLayer <EOL> return InputLayer ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> def test_get_output_without_arguments ( self , layer , get_output ) : <EOL> assert get_output ( layer ) is layer . input_var <EOL> def test_get_output_input_is_variable ( self , layer , get_output ) : <EOL> variable = theano . Variable ( "<STR_LIT>" ) <EOL> assert get_output ( layer , variable ) is variable <EOL> def test_get_output_input_is_array ( self , layer , get_output ) : <EOL> inputs = [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> output = get_output ( layer , inputs ) <EOL> assert numpy . all ( output . eval ( ) == inputs ) <EOL> def test_get_output_input_is_a_mapping ( self , layer , get_output ) : <EOL> inputs = { layer : theano . tensor . matrix ( ) } <EOL> assert get_output ( layer , inputs ) is inputs [ layer ] <EOL> class TestGetOutput_Layer : <EOL> @ pytest . fixture <EOL> def get_output ( self ) : <EOL> from lasagne . layers . helper import get_output <EOL> return get_output <EOL> @ pytest . fixture <EOL> def layers ( self ) : <EOL> from lasagne . layers . base import Layer <EOL> from lasagne . layers . input import InputLayer <EOL> l1 = Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) , <EOL> get_output_kwargs = [ ] ) <EOL> l2 = Mock ( Layer ( l1 ) , output_shape = ( None , ) , get_output_kwargs = [ ] ) <EOL> l2 . input_layer = l1 <EOL> l3 = Mock ( Layer ( l2 ) , output_shape = ( None , ) , get_output_kwargs = [ '<STR_LIT>' ] ) <EOL> l3 . input_layer = l2 <EOL> return l1 , l2 , l3 <EOL> def test_get_output_without_arguments ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> output = get_output ( l3 ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( <EOL> l2 . get_output_for . return_value ) <EOL> l2 . get_output_for . assert_called_with ( <EOL> l1 . input_var ) <EOL> def test_get_output_with_single_argument ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> inputs , kwarg = theano . tensor . matrix ( ) , object ( ) <EOL> output = get_output ( l3 , inputs , kwarg = kwarg ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( <EOL> l2 . get_output_for . return_value , kwarg = kwarg ) <EOL> l2 . get_output_for . assert_called_with ( <EOL> inputs , kwarg = kwarg ) <EOL> def test_get_output_input_is_a_mapping ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 ) . input_var = p <EOL> inputs = { l3 : theano . tensor . matrix ( ) } <EOL> assert get_output ( l3 , inputs ) is inputs [ l3 ] <EOL> assert l3 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert l2 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_input_is_a_mapping_no_key ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> output = get_output ( l3 , { } ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( <EOL> l2 . get_output_for . return_value ) <EOL> l2 . get_output_for . assert_called_with ( <EOL> l1 . input_var ) <EOL> def test_get_output_input_is_a_mapping_to_array ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 ) . input_var = p <EOL> inputs = { l3 : [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] } <EOL> output = get_output ( l3 , inputs ) <EOL> assert numpy . all ( output . eval ( ) == inputs [ l3 ] ) <EOL> assert l3 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert l2 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_input_is_a_mapping_for_layer ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 ) . input_var = p <EOL> input_expr , kwarg = theano . tensor . matrix ( ) , object ( ) <EOL> inputs = { l2 : input_expr } <EOL> output = get_output ( l3 , inputs , kwarg = kwarg ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( input_expr , kwarg = kwarg ) <EOL> assert l2 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_input_is_a_mapping_for_input_layer ( self , layers , <EOL> get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 ) . input_var = p <EOL> input_expr , kwarg = theano . tensor . matrix ( ) , object ( ) <EOL> inputs = { l1 : input_expr } <EOL> output = get_output ( l3 , inputs , kwarg = kwarg ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( <EOL> l2 . get_output_for . return_value , kwarg = kwarg ) <EOL> l2 . get_output_for . assert_called_with ( <EOL> input_expr , kwarg = kwarg ) <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_with_unused_kwarg ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> unused_kwarg = object ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> get_output ( l3 , kwagg = unused_kwarg ) <EOL> assert len ( w ) == <NUM_LIT:1> <EOL> assert issubclass ( w [ <NUM_LIT:0> ] . category , UserWarning ) <EOL> assert '<STR_LIT>' in str ( w [ <NUM_LIT:0> ] . message ) <EOL> def test_get_output_with_no_unused_kwarg ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> get_output ( l3 ) <EOL> assert len ( w ) == <NUM_LIT:0> <EOL> @ pytest . fixture <EOL> def layer_from_shape ( self ) : <EOL> from lasagne . layers . base import Layer <EOL> return Layer ( ( None , <NUM_LIT:20> ) ) <EOL> def test_layer_from_shape_invalid_get_output ( self , layer_from_shape , <EOL> get_output ) : <EOL> layer = layer_from_shape <EOL> with pytest . raises ( ValueError ) : <EOL> get_output ( layer ) <EOL> with pytest . raises ( ValueError ) : <EOL> get_output ( layer , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> with pytest . raises ( ValueError ) : <EOL> get_output ( layer , { Mock ( ) : [ <NUM_LIT:1> , <NUM_LIT:2> ] } ) <EOL> def test_layer_from_shape_valid_get_output ( self , layer_from_shape , <EOL> get_output ) : <EOL> layer = layer_from_shape <EOL> inputs = { layer : theano . tensor . matrix ( ) } <EOL> assert get_output ( layer , inputs ) is inputs [ layer ] <EOL> inputs = { None : theano . tensor . matrix ( ) } <EOL> layer . get_output_for = Mock ( ) <EOL> assert get_output ( layer , inputs ) is layer . get_output_for . return_value <EOL> layer . get_output_for . assert_called_with ( inputs [ None ] ) <EOL> class TestGetOutput_MergeLayer : <EOL> @ pytest . fixture <EOL> def get_output ( self ) : <EOL> from lasagne . layers . helper import get_output <EOL> return get_output <EOL> @ pytest . fixture <EOL> def layers ( self ) : <EOL> from lasagne . layers . base import Layer , MergeLayer <EOL> from lasagne . layers . input import InputLayer <EOL> l1 = [ Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) , <EOL> get_output_kwargs = [ ] ) , <EOL> Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) , <EOL> get_output_kwargs = [ ] ) ] <EOL> l2 = [ Mock ( Layer ( l1 [ <NUM_LIT:0> ] ) , output_shape = ( None , ) , <EOL> get_output_kwargs = [ ] ) , <EOL> Mock ( Layer ( l1 [ <NUM_LIT:1> ] ) , output_shape = ( None , ) , <EOL> get_output_kwargs = [ ] ) ] <EOL> l2 [ <NUM_LIT:0> ] . input_layer = l1 [ <NUM_LIT:0> ] <EOL> l2 [ <NUM_LIT:1> ] . input_layer = l1 [ <NUM_LIT:1> ] <EOL> l3 = Mock ( MergeLayer ( l2 ) , get_output_kwargs = [ '<STR_LIT>' ] ) <EOL> l3 . input_layers = l2 <EOL> return l1 , l2 , l3 <EOL> def test_get_output_without_arguments ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> output = get_output ( l3 ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( [ <EOL> l2 [ <NUM_LIT:0> ] . get_output_for . return_value , <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . return_value , <EOL> ] ) <EOL> l2 [ <NUM_LIT:0> ] . get_output_for . assert_called_with ( <EOL> l1 [ <NUM_LIT:0> ] . input_var ) <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . assert_called_with ( <EOL> l1 [ <NUM_LIT:1> ] . input_var ) <EOL> def test_get_output_with_single_argument_fails ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> inputs , kwarg = theano . tensor . matrix ( ) , object ( ) <EOL> with pytest . raises ( ValueError ) : <EOL> output = get_output ( l3 , inputs , kwarg = kwarg ) <EOL> def test_get_output_input_is_a_mapping ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 [ <NUM_LIT:0> ] ) . input_var = p <EOL> type ( l1 [ <NUM_LIT:1> ] ) . input_var = p <EOL> inputs = { l3 : theano . tensor . matrix ( ) } <EOL> assert get_output ( l3 , inputs ) is inputs [ l3 ] <EOL> assert l3 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:1> ] . get_output_for . call_count == <NUM_LIT:0> <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_input_is_a_mapping_no_key ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> output = get_output ( l3 , { } ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( [ <EOL> l2 [ <NUM_LIT:0> ] . get_output_for . return_value , <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . return_value , <EOL> ] ) <EOL> l2 [ <NUM_LIT:0> ] . get_output_for . assert_called_with ( <EOL> l1 [ <NUM_LIT:0> ] . input_var ) <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . assert_called_with ( <EOL> l1 [ <NUM_LIT:1> ] . input_var ) <EOL> def test_get_output_input_is_a_mapping_to_array ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 [ <NUM_LIT:0> ] ) . input_var = p <EOL> type ( l1 [ <NUM_LIT:1> ] ) . input_var = p <EOL> inputs = { l3 : [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] } <EOL> output = get_output ( l3 , inputs ) <EOL> assert numpy . all ( output . eval ( ) == inputs [ l3 ] ) <EOL> assert l3 . get_output_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:1> ] . get_output_for . call_count == <NUM_LIT:0> <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_input_is_a_mapping_for_layer ( self , layers , get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 [ <NUM_LIT:0> ] ) . input_var = p <EOL> input_expr , kwarg = theano . tensor . matrix ( ) , object ( ) <EOL> inputs = { l2 [ <NUM_LIT:0> ] : input_expr } <EOL> output = get_output ( l3 , inputs , kwarg = kwarg ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( [ <EOL> input_expr , <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . return_value , <EOL> ] , kwarg = kwarg ) <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . assert_called_with ( <EOL> l1 [ <NUM_LIT:1> ] . input_var , kwarg = kwarg ) <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_for . call_count == <NUM_LIT:0> <EOL> assert p . call_count == <NUM_LIT:0> <EOL> def test_get_output_input_is_a_mapping_for_input_layer ( self , layers , <EOL> get_output ) : <EOL> l1 , l2 , l3 = layers <EOL> p = PropertyMock ( ) <EOL> type ( l1 [ <NUM_LIT:0> ] ) . input_var = p <EOL> input_expr , kwarg = theano . tensor . matrix ( ) , object ( ) <EOL> inputs = { l1 [ <NUM_LIT:0> ] : input_expr } <EOL> output = get_output ( l3 , inputs , kwarg = kwarg ) <EOL> assert output is l3 . get_output_for . return_value <EOL> l3 . get_output_for . assert_called_with ( [ <EOL> l2 [ <NUM_LIT:0> ] . get_output_for . return_value , <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . return_value , <EOL> ] , kwarg = kwarg ) <EOL> l2 [ <NUM_LIT:0> ] . get_output_for . assert_called_with ( <EOL> input_expr , kwarg = kwarg ) <EOL> l2 [ <NUM_LIT:1> ] . get_output_for . assert_called_with ( <EOL> l1 [ <NUM_LIT:1> ] . input_var , kwarg = kwarg ) <EOL> assert p . call_count == <NUM_LIT:0> <EOL> @ pytest . fixture <EOL> def layer_from_shape ( self ) : <EOL> from lasagne . layers . input import InputLayer <EOL> from lasagne . layers . base import MergeLayer <EOL> return MergeLayer ( [ <EOL> ( None , <NUM_LIT:20> ) , <EOL> Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) ) ] ) <EOL> def test_layer_from_shape_invalid_get_output ( self , layer_from_shape , <EOL> get_output ) : <EOL> layer = layer_from_shape <EOL> with pytest . raises ( ValueError ) : <EOL> get_output ( layer ) <EOL> with pytest . raises ( ValueError ) : <EOL> get_output ( layer , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> with pytest . raises ( ValueError ) : <EOL> get_output ( layer , { layer . input_layers [ <NUM_LIT:1> ] : [ <NUM_LIT:1> , <NUM_LIT:2> ] } ) <EOL> def test_layer_from_shape_valid_get_output ( self , layer_from_shape , <EOL> get_output ) : <EOL> layer = layer_from_shape <EOL> inputs = { layer : theano . tensor . matrix ( ) } <EOL> assert get_output ( layer , inputs ) is inputs [ layer ] <EOL> inputs = { None : theano . tensor . matrix ( ) } <EOL> layer . get_output_for = Mock ( ) <EOL> assert get_output ( layer , inputs ) is layer . get_output_for . return_value <EOL> layer . get_output_for . assert_called_with ( <EOL> [ inputs [ None ] , layer . input_layers [ <NUM_LIT:1> ] . input_var ] ) <EOL> class TestGetOutputShape_InputLayer : <EOL> @ pytest . fixture <EOL> def get_output_shape ( self ) : <EOL> from lasagne . layers . helper import get_output_shape <EOL> return get_output_shape <EOL> @ pytest . fixture <EOL> def layer ( self ) : <EOL> from lasagne . layers . input import InputLayer <EOL> return InputLayer ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> def test_get_output_shape_without_arguments ( self , layer , get_output_shape ) : <EOL> assert get_output_shape ( layer ) == ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_get_output_shape_input_is_tuple ( self , layer , get_output_shape ) : <EOL> shp = ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> assert get_output_shape ( layer , shp ) == shp <EOL> def test_get_output_shape_input_is_a_mapping ( self , layer , <EOL> get_output_shape ) : <EOL> input_shapes = { layer : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> assert get_output_shape ( layer , input_shapes ) == input_shapes [ layer ] <EOL> class TestGetOutputShape_Layer : <EOL> @ pytest . fixture <EOL> def get_output_shape ( self ) : <EOL> from lasagne . layers . helper import get_output_shape <EOL> return get_output_shape <EOL> @ pytest . fixture <EOL> def layers ( self ) : <EOL> from lasagne . layers . base import Layer <EOL> from lasagne . layers . input import InputLayer <EOL> l1 = Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) ) <EOL> l2 = Mock ( Layer ( l1 ) , output_shape = ( None , ) ) <EOL> l2 . input_layer = l1 <EOL> l3 = Mock ( Layer ( l2 ) , output_shape = ( None , ) ) <EOL> l3 . input_layer = l2 <EOL> return l1 , l2 , l3 <EOL> def test_get_output_shape_without_arguments ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> output_shape = get_output_shape ( l3 ) <EOL> assert output_shape is l3 . output_shape <EOL> assert l3 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_with_single_argument ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> shp = ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> output_shape = get_output_shape ( l3 , shp ) <EOL> assert output_shape is l3 . get_output_shape_for . return_value <EOL> l3 . get_output_shape_for . assert_called_with ( <EOL> l2 . get_output_shape_for . return_value ) <EOL> l2 . get_output_shape_for . assert_called_with ( shp ) <EOL> def test_get_output_shape_input_is_a_mapping ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> input_shapes = { l3 : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> assert get_output_shape ( l3 , input_shapes ) is input_shapes [ l3 ] <EOL> assert l3 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_input_is_a_mapping_no_key ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> output_shape = get_output_shape ( l3 , { } ) <EOL> assert output_shape is l3 . output_shape <EOL> assert l3 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_input_is_a_mapping_for_layer ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> shp = ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> input_shapes = { l2 : shp } <EOL> output_shape = get_output_shape ( l3 , input_shapes ) <EOL> assert output_shape is l3 . get_output_shape_for . return_value <EOL> l3 . get_output_shape_for . assert_called_with ( shp ) <EOL> assert l2 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_input_is_a_mapping_for_input_layer ( <EOL> self , layers , get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> shp = ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> input_shapes = { l1 : shp } <EOL> output_shape = get_output_shape ( l3 , input_shapes ) <EOL> assert output_shape is l3 . get_output_shape_for . return_value <EOL> l3 . get_output_shape_for . assert_called_with ( <EOL> l2 . get_output_shape_for . return_value ) <EOL> l2 . get_output_shape_for . assert_called_with ( shp ) <EOL> @ pytest . fixture <EOL> def layer_from_shape ( self ) : <EOL> from lasagne . layers . base import Layer <EOL> return Layer ( ( None , <NUM_LIT:20> ) ) <EOL> def test_layer_from_shape ( self , layer_from_shape , get_output_shape ) : <EOL> layer = layer_from_shape <EOL> input_shapes = { layer : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> assert get_output_shape ( layer , input_shapes ) is input_shapes [ layer ] <EOL> input_shapes = { None : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> layer . get_output_shape_for = Mock ( ) <EOL> assert ( get_output_shape ( layer , input_shapes ) is <EOL> layer . get_output_shape_for . return_value ) <EOL> layer . get_output_shape_for . assert_called_with ( input_shapes [ None ] ) <EOL> class TestGetOutputShape_MergeLayer : <EOL> @ pytest . fixture <EOL> def get_output_shape ( self ) : <EOL> from lasagne . layers . helper import get_output_shape <EOL> return get_output_shape <EOL> @ pytest . fixture <EOL> def layers ( self ) : <EOL> from lasagne . layers . base import Layer , MergeLayer <EOL> from lasagne . layers . input import InputLayer <EOL> l1 = [ Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) ) , <EOL> Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) ) ] <EOL> l2 = [ Mock ( Layer ( l1 [ <NUM_LIT:0> ] ) , output_shape = ( None , ) ) , <EOL> Mock ( Layer ( l1 [ <NUM_LIT:1> ] ) , output_shape = ( None , ) ) ] <EOL> l2 [ <NUM_LIT:0> ] . input_layer = l1 [ <NUM_LIT:0> ] <EOL> l2 [ <NUM_LIT:1> ] . input_layer = l1 [ <NUM_LIT:1> ] <EOL> l3 = Mock ( MergeLayer ( l2 ) ) <EOL> l3 . input_layers = l2 <EOL> return l1 , l2 , l3 <EOL> def test_get_output_shape_without_arguments ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> output_shape = get_output_shape ( l3 ) <EOL> assert output_shape is l3 . output_shape <EOL> assert l3 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:1> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_with_single_argument_fails ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> shp = ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> with pytest . raises ( ValueError ) : <EOL> output_shape = get_output_shape ( l3 , shp ) <EOL> def test_get_output_shape_input_is_a_mapping ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> input_shapes = { l3 : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> assert get_output_shape ( l3 , input_shapes ) is input_shapes [ l3 ] <EOL> assert l3 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:1> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_input_is_a_mapping_no_key ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> output_shape = get_output_shape ( l3 , { } ) <EOL> assert output_shape is l3 . output_shape <EOL> assert l3 . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> assert l2 [ <NUM_LIT:1> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_input_is_a_mapping_for_layer ( self , layers , <EOL> get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> shp = ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> input_shapes = { l2 [ <NUM_LIT:0> ] : shp } <EOL> output = get_output_shape ( l3 , input_shapes ) <EOL> assert output is l3 . get_output_shape_for . return_value <EOL> l3 . get_output_shape_for . assert_called_with ( [ <EOL> shp , l2 [ <NUM_LIT:1> ] . get_output_shape_for . return_value ] ) <EOL> l2 [ <NUM_LIT:1> ] . get_output_shape_for . assert_called_with ( l1 [ <NUM_LIT:1> ] . shape ) <EOL> assert l2 [ <NUM_LIT:0> ] . get_output_shape_for . call_count == <NUM_LIT:0> <EOL> def test_get_output_shape_input_is_a_mapping_for_input_layer ( <EOL> self , layers , get_output_shape ) : <EOL> l1 , l2 , l3 = layers <EOL> shp = ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> input_shapes = { l1 [ <NUM_LIT:0> ] : shp } <EOL> output = get_output_shape ( l3 , input_shapes ) <EOL> assert output is l3 . get_output_shape_for . return_value <EOL> l3 . get_output_shape_for . assert_called_with ( [ <EOL> l2 [ <NUM_LIT:0> ] . get_output_shape_for . return_value , <EOL> l2 [ <NUM_LIT:1> ] . get_output_shape_for . return_value , <EOL> ] ) <EOL> l2 [ <NUM_LIT:0> ] . get_output_shape_for . assert_called_with ( shp ) <EOL> l2 [ <NUM_LIT:1> ] . get_output_shape_for . assert_called_with ( l1 [ <NUM_LIT:1> ] . shape ) <EOL> @ pytest . fixture <EOL> def layer_from_shape ( self ) : <EOL> from lasagne . layers . input import InputLayer <EOL> from lasagne . layers . base import MergeLayer <EOL> return MergeLayer ( [ <EOL> ( None , <NUM_LIT:20> ) , <EOL> Mock ( InputLayer ( ( None , ) ) , output_shape = ( None , ) ) ] ) <EOL> def test_layer_from_shape_valid_get_output_shape ( self , layer_from_shape , <EOL> get_output_shape ) : <EOL> layer = layer_from_shape <EOL> input_shapes = { layer : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> assert get_output_shape ( layer , input_shapes ) is input_shapes [ layer ] <EOL> input_shapes = { None : ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) } <EOL> layer . get_output_shape_for = Mock ( ) <EOL> assert ( get_output_shape ( layer , input_shapes ) is <EOL> layer . get_output_shape_for . return_value ) <EOL> layer . get_output_shape_for . assert_called_with ( <EOL> [ input_shapes [ None ] , layer . input_layers [ <NUM_LIT:1> ] . shape ] ) <EOL> class TestGetAllParams : <EOL> def test_get_all_params ( self ) : <EOL> from lasagne . layers import ( InputLayer , DenseLayer , get_all_params ) <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> assert get_all_params ( l3 ) == l2 . get_params ( ) + l3 . get_params ( ) <EOL> assert ( get_all_params ( l3 , regularizable = False ) == <EOL> ( l2 . get_params ( regularizable = False ) + <EOL> l3 . get_params ( regularizable = False ) ) ) <EOL> assert ( get_all_params ( l3 , regularizable = True ) == <EOL> ( l2 . get_params ( regularizable = True ) + <EOL> l3 . get_params ( regularizable = True ) ) ) <EOL> class TestCountParams : <EOL> def test_get_all_params ( self ) : <EOL> from lasagne . layers import ( InputLayer , DenseLayer , count_params ) <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> num_weights = <NUM_LIT:20> * <NUM_LIT:30> + <NUM_LIT:30> * <NUM_LIT> <EOL> num_biases = <NUM_LIT:30> + <NUM_LIT> <EOL> assert count_params ( l3 , regularizable = True ) == num_weights <EOL> assert count_params ( l3 , regularizable = False ) == num_biases <EOL> assert count_params ( l3 ) == num_weights + num_biases <EOL> class TestGetAllParamValues : <EOL> def test_get_all_param_values ( self ) : <EOL> from lasagne . layers import ( InputLayer , DenseLayer , <EOL> get_all_param_values ) <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> pvs = get_all_param_values ( l3 ) <EOL> assert len ( pvs ) == <NUM_LIT:4> <EOL> class TestSetAllParamValues : <EOL> def test_set_all_param_values ( self ) : <EOL> from lasagne . layers import ( InputLayer , DenseLayer , <EOL> set_all_param_values ) <EOL> from lasagne . utils import floatX <EOL> l1 = InputLayer ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> l2 = DenseLayer ( l1 , <NUM_LIT:30> ) <EOL> l3 = DenseLayer ( l2 , <NUM_LIT> ) <EOL> a2 = floatX ( numpy . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:20> , <NUM_LIT:30> ) ) ) <EOL> b2 = floatX ( numpy . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:30> , ) ) ) <EOL> a3 = floatX ( numpy . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:30> , <NUM_LIT> ) ) ) <EOL> b3 = floatX ( numpy . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT> , ) ) ) <EOL> set_all_param_values ( l3 , [ a2 , b2 , a3 , b3 ] ) <EOL> assert numpy . allclose ( l3 . W . get_value ( ) , a3 ) <EOL> assert numpy . allclose ( l3 . b . get_value ( ) , b3 ) <EOL> assert numpy . allclose ( l2 . W . get_value ( ) , a2 ) <EOL> assert numpy . allclose ( l2 . b . get_value ( ) , b2 ) <EOL> with pytest . raises ( ValueError ) : <EOL> set_all_param_values ( l3 , [ a3 , b3 , a2 ] ) <EOL> with pytest . raises ( ValueError ) : <EOL> a3_bad = floatX ( numpy . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT> , <NUM_LIT> ) ) ) <EOL> set_all_param_values ( l3 , [ a2 , b2 , a3_bad , b3 ] ) </s>
