<s> from __future__ import print_function <EOL> from pybrain . structure . modules . linearlayer import LinearLayer <EOL> from pybrain . structure . modules . tanhlayer import TanhLayer <EOL> from pybrain . structure . moduleslice import ModuleSlice <EOL> from pybrain . structure . networks . feedforward import FeedForwardNetwork <EOL> from pybrain . structure . connections . shared import MotherConnection , SharedFullConnection <EOL> from pybrain . structure . modules . sigmoidlayer import SigmoidLayer <EOL> __author__ = '<STR_LIT>' <EOL> class SimpleConvolutionalNetwork ( FeedForwardNetwork ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inputdim , insize , convSize , numFeatureMaps , ** args ) : <EOL> FeedForwardNetwork . __init__ ( self , ** args ) <EOL> inlayer = LinearLayer ( inputdim * insize * insize ) <EOL> self . addInputModule ( inlayer ) <EOL> self . _buildStructure ( inputdim , insize , inlayer , convSize , numFeatureMaps ) <EOL> self . sortModules ( ) <EOL> def _buildStructure ( self , inputdim , insize , inlayer , convSize , numFeatureMaps ) : <EOL> outdim = insize - convSize + <NUM_LIT:1> <EOL> hlayer = TanhLayer ( outdim * outdim * numFeatureMaps , name = '<STR_LIT:h>' ) <EOL> self . addModule ( hlayer ) <EOL> outlayer = SigmoidLayer ( outdim * outdim , name = '<STR_LIT>' ) <EOL> self . addOutputModule ( outlayer ) <EOL> convConns = [ ] <EOL> for i in range ( convSize ) : <EOL> convConns . append ( MotherConnection ( convSize * numFeatureMaps * inputdim , name = '<STR_LIT>' + str ( i ) ) ) <EOL> outConn = MotherConnection ( numFeatureMaps ) <EOL> for i in range ( outdim ) : <EOL> for j in range ( outdim ) : <EOL> offset = i * outdim + j <EOL> outmod = ModuleSlice ( hlayer , inSliceFrom = offset * numFeatureMaps , inSliceTo = ( offset + <NUM_LIT:1> ) * numFeatureMaps , <EOL> outSliceFrom = offset * numFeatureMaps , outSliceTo = ( offset + <NUM_LIT:1> ) * numFeatureMaps ) <EOL> self . addConnection ( SharedFullConnection ( outConn , outmod , outlayer , outSliceFrom = offset , outSliceTo = offset + <NUM_LIT:1> ) ) <EOL> for k , mc in enumerate ( convConns ) : <EOL> offset = insize * ( i + k ) + j <EOL> inmod = ModuleSlice ( inlayer , outSliceFrom = offset * inputdim , outSliceTo = offset * inputdim + convSize * inputdim ) <EOL> self . addConnection ( SharedFullConnection ( mc , inmod , outmod ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from scipy import array , ravel <EOL> from . custom . convboard import ConvolutionalBoardNetwork <EOL> from pybrain . rl . environments . twoplayergames . tasks import CaptureGameTask <EOL> N = ConvolutionalBoardNetwork ( <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> input = [ [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] ] , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] , <EOL> ] <EOL> res = N . activate ( ravel ( array ( input ) ) ) <EOL> res = res . reshape ( <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> print ( ( N [ '<STR_LIT>' ] . inputbuffer [ <NUM_LIT:0> ] . reshape ( <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:2> ) [ : , : , <NUM_LIT:0> ] ) ) <EOL> print ( res ) <EOL> t = CaptureGameTask ( <NUM_LIT:4> ) <EOL> print ( ( t ( N ) ) ) <EOL> if False : <EOL> N = SimpleConvolutionalNetwork ( <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> print ( N ) <EOL> res = N . activate ( ravel ( array ( input ) ) ) <EOL> res = res . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> print ( res ) </s>
