Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou takatakamanbou/00readme.md Secret
Last active Aug 29, 2015

Embed
What would you like to do?
convnet150821.py
import numpy as np
import theano
import theano.tensor as T
import theano.tensor.signal.downsample as Tsd
import nnet150712 as nnet
########## Convolution Layer ##########
class ConvLayer( object ):
def __init__( self, Xdim, Wdim, afunc, withBias, border_mode = 'valid',
Wini = 0.01, floatX = theano.config.floatX ):
assert border_mode in [ 'valid', 'full' ]
self.border_mode = border_mode
# dimension of the input
Xnch, Xrow, Xcol = Xdim
self.Xshape = Xdim
# dimension of the convolution filters
Wnch, Wrow, Wcol = Wdim
self.Wshape = ( Wnch, Xnch, Wrow, Wcol )
# dimension of the output
if border_mode == 'valid':
Yrow, Ycol = Xrow - Wrow + 1, Xcol - Wcol + 1
else:
Yrow, Ycol = Xrow + Wrow - 1, Xcol + Wcol - 1
self.Yshape = ( Wnch, Yrow, Ycol )
self.Dout = Wnch * Yrow * Ycol
# activation function of the layer
self.afunc = nnet.d_afunc[afunc]
self.withBias = withBias
# theano shared variables
self.W = theano.shared( np.array( nnet.randomN( self.Wshape, Wini ), dtype = floatX ) )
self.dW = theano.shared( np.zeros( self.Wshape, dtype = floatX ) )
if withBias:
self.b = theano.shared( np.zeros( Wnch, dtype = floatX ) )
self.db = theano.shared( np.zeros( Wnch, dtype = floatX ) )
def output( self, X ):
# X: Ndat x Xshape, Y: Ndat x Yshape
Xs = ( None, self.Xshape[0], self.Xshape[1], self.Xshape[2] )
Ws = self.Wshape
Y = T.nnet.conv.conv2d( X, self.W, image_shape = Xs, filter_shape = Ws, border_mode = self.border_mode )
if self.withBias:
b = self.b.dimshuffle( 'x', 0, 'x', 'x' ) # 1 x nch x 1 x 1
Y += b
Z = self.afunc( Y )
return Y, Z # Ndat x Yshape
########## Pooling Layer ##########
class PoolLayer( object ):
def __init__( self, Xdim, ds, afunc, withBias, st = None, floatX = theano.config.floatX ):
# dimension of the input
Xnch, Xrow, Xcol = Xdim
self.Xshape = Xdim
# parameters of the pooling layer
self.ds = ds
self.st = st
self.ignore_border = False
rv = Tsd.DownsampleFactorMax.out_shape( self.Xshape, ds, ignore_border = self.ignore_border, st = st )
#self.Yshape = ( Xnch, rv[1], rv[2] )
self.Yshape = tuple( rv )
self.Dout = np.prod( self.Yshape )
# activation function of the layer
self.afunc = nnet.d_afunc[afunc]
self.withBias = withBias
# theano shared variables
if withBias:
self.b = theano.shared( np.zeros( Xnch, dtype = floatX ) )
self.db = theano.shared( np.zeros( Xnch, dtype = floatX ) )
def output( self, X ):
# X: Ndat x Xshape
Y = Tsd.max_pool_2d( X, self.ds, ignore_border = self.ignore_border, st = self.st ) # Ndat x Yshape
if self.withBias:
b = self.b.dimshuffle( 'x', 0, 'x', 'x' ) # 1 x nch x 1 x 1
Y += b
Z = self.afunc( Y )
return Y, Z
########## Full-Connection Layer ##########
class FullLayer( nnet.Layer ):
def __init__( self, Din, Nunit, afunc, withBias = True, Wini = 0.01, floatX = theano.config.floatX, T4toMat = False ):
super( FullLayer, self ).__init__( Din, Nunit, afunc, withBias, Wini, floatX )
self.T4toMat = T4toMat
def super_output( self, X ):
return super( FullLayer, self ).output( X )
def output( self, X ):
if self.T4toMat:
return self.super_output( X.reshape( ( X.shape[0], -1 ) ) )
else:
return self.super_output( X )
########## Convolutional Neural Net ##########
class CNN( object ):
def __init__( self, Layers ):
# layers - list of Layer instances
self.Layers = Layers
# theano functions
self.output = self._Tfunc_output()
self.cost = self._Tfunc_cost()
self.train = self._Tfunc_train()
### theano function for output computation
#
def _Tfunc_output( self ):
X = T.tensor4() # Ndat x Xnch x Xrow x Xcol
Y, Z = nnet._T_output( self.Layers, X )
return theano.function( [ X ], [ Y, Z ] )
### theano function for cost computation
#
def _Tfunc_cost( self ):
Z = T.matrix() # N x K
lab = T.ivector() # N-dim
cost = nnet._T_cost( Z, lab )
return theano.function( [ Z, lab ], cost )
### theano function for gradient descent learning
#
def _Tfunc_train( self ):
X = T.tensor4( 'X' )
lab = T.ivector( 'lab' )
eta = T.scalar( 'eta' )
mu = T.scalar( 'mu' )
lam = T.scalar( 'lambda' )
Y, Z = nnet._T_output( self.Layers, X )
cost = T.mean( nnet._T_cost( Z, lab ) )
updatesList = []
for il, layer in enumerate( self.Layers ):
# PoolLayer doesn't have W & dW
if not isinstance( layer, PoolLayer ):
gradW = T.grad( cost, layer.W )
#dWnew = -eta * gradW + mu * layer.dW
dWnew = -eta * ( gradW + lam * layer.W ) + mu * layer.dW
Wnew = layer.W + dWnew
updatesList.append( ( layer.W, Wnew ) )
updatesList.append( ( layer.dW, dWnew ) )
if layer.withBias:
gradb = T.grad( cost, layer.b )
# no weight decay for bias
dbnew = -eta * gradb + mu * layer.db
bnew = layer.b + dbnew
updatesList.append( ( layer.b, bnew ) )
updatesList.append( ( layer.db, dbnew ) )
return theano.function( [ X, lab, eta, mu, lam ], cost, updates = updatesList )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.