<s> import numpy as np <EOL> import theano <EOL> import theano . tensor as T <EOL> from neupy . utils import asfloat <EOL> from neupy . core . config import Configurable <EOL> from neupy . core . properties import ( TypedListProperty , ArrayProperty , <EOL> ChoiceProperty , IntProperty ) <EOL> from neupy . layers . connections import ChainConnection <EOL> from . utils import XAVIER_NORMAL , VALID_INIT_METHODS , generate_weight <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class BaseLayer ( ChainConnection , Configurable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** options ) : <EOL> super ( BaseLayer , self ) . __init__ ( ) <EOL> self . parameters = [ ] <EOL> self . relate_to_layer = None <EOL> self . relate_from_layer = None <EOL> self . layer_id = <NUM_LIT:1> <EOL> Configurable . __init__ ( self , ** options ) <EOL> def initialize ( self ) : <EOL> if self . relate_from_layer is not None : <EOL> self . layer_id = self . relate_from_layer . layer_id + <NUM_LIT:1> <EOL> def relate_to ( self , right_layer ) : <EOL> self . relate_to_layer = right_layer <EOL> right_layer . relate_from_layer = self <EOL> def __repr__ ( self ) : <EOL> classname = self . __class__ . __name__ <EOL> return '<STR_LIT>' . format ( name = classname ) <EOL> def create_shared_parameter ( value , name , shape , init_method , bounds ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , T . sharedvar . TensorSharedVariable ) : <EOL> return value <EOL> if value is None : <EOL> value = generate_weight ( shape , bounds , init_method ) <EOL> return theano . shared ( value = asfloat ( value ) , name = name , borrow = True ) <EOL> class SharedArrayProperty ( ArrayProperty ) : <EOL> """<STR_LIT>""" <EOL> expected_type = ( np . matrix , np . ndarray , <EOL> T . sharedvar . TensorSharedVariable , <EOL> T . TensorVariable ) <EOL> class ParameterBasedLayer ( BaseLayer ) : <EOL> """<STR_LIT>""" <EOL> size = IntProperty ( minval = <NUM_LIT:1> ) <EOL> weight = SharedArrayProperty ( default = None ) <EOL> bias = SharedArrayProperty ( default = None ) <EOL> bounds = TypedListProperty ( default = ( <NUM_LIT:0> , <NUM_LIT:1> ) , element_type = ( int , float ) ) <EOL> init_method = ChoiceProperty ( default = XAVIER_NORMAL , <EOL> choices = VALID_INIT_METHODS ) <EOL> def __init__ ( self , size , ** options ) : <EOL> if size is not None : <EOL> options [ '<STR_LIT:size>' ] = size <EOL> super ( ParameterBasedLayer , self ) . __init__ ( ** options ) <EOL> def weight_shape ( self ) : <EOL> output_size = self . relate_to_layer . size <EOL> return ( self . size , output_size ) <EOL> def bias_shape ( self ) : <EOL> output_size = self . relate_to_layer . size <EOL> return ( output_size , ) <EOL> def initialize ( self ) : <EOL> super ( ParameterBasedLayer , self ) . initialize ( ) <EOL> self . weight = create_shared_parameter ( <EOL> value = self . weight , <EOL> name = '<STR_LIT>' . format ( self . layer_id ) , <EOL> shape = self . weight_shape ( ) , <EOL> bounds = self . bounds , <EOL> init_method = self . init_method , <EOL> ) <EOL> self . bias = create_shared_parameter ( <EOL> value = self . bias , <EOL> name = '<STR_LIT>' . format ( self . layer_id ) , <EOL> shape = self . bias_shape ( ) , <EOL> bounds = self . bounds , <EOL> init_method = self . init_method , <EOL> ) <EOL> self . parameters = [ self . weight , self . bias ] <EOL> def __repr__ ( self ) : <EOL> classname = self . __class__ . __name__ <EOL> return '<STR_LIT>' . format ( name = classname , size = self . size ) </s>
