<s> from stage import * <EOL> import numpy as np <EOL> class OrdinalRegression ( Stage ) : <EOL> def __init__ ( <EOL> self , <EOL> outputDim , <EOL> fixExtreme = True , <EOL> inputNames = None , <EOL> name = None , <EOL> outputdEdX = True , <EOL> learningRate = <NUM_LIT:0.0> , <EOL> learningRateAnnealConst = <NUM_LIT:0.0> , <EOL> momentum = <NUM_LIT:0.0> , <EOL> deltaMomentum = <NUM_LIT:0.0> , <EOL> weightClip = <NUM_LIT:0.0> , <EOL> gradientClip = <NUM_LIT:0.0> , <EOL> weightRegConst = <NUM_LIT:0.0> ) : <EOL> Stage . __init__ ( <EOL> self , <EOL> name = name , <EOL> inputNames = inputNames , <EOL> outputDim = outputDim , <EOL> outputdEdX = outputdEdX , <EOL> learningRate = learningRate , <EOL> learningRateAnnealConst = learningRateAnnealConst , <EOL> momentum = momentum , <EOL> deltaMomentum = deltaMomentum , <EOL> weightClip = weightClip , <EOL> gradientClip = gradientClip , <EOL> weightRegConst = weightRegConst ) <EOL> self . fixExtreme = fixExtreme <EOL> mu = np . linspace ( - <NUM_LIT:1> , <NUM_LIT:1> , self . outputDim ) <EOL> pi = np . zeros ( self . outputDim ) + np . log ( <NUM_LIT:1> / float ( self . outputDim ) ) <EOL> self . W = np . zeros ( ( <NUM_LIT:2> , self . outputDim ) ) <EOL> self . W [ <NUM_LIT:0> ] = mu <EOL> self . W [ <NUM_LIT:1> ] = pi <EOL> def forward ( self , X ) : <EOL> mu = self . W [ <NUM_LIT:0> ] . reshape ( <NUM_LIT:1> , self . W . shape [ <NUM_LIT:1> ] ) <EOL> pi = self . W [ <NUM_LIT:1> ] . reshape ( <NUM_LIT:1> , self . W . shape [ <NUM_LIT:1> ] ) <EOL> self . Xshape = X . shape <EOL> X = X . reshape ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . X = X <EOL> Z = np . exp ( mu * X + ( pi - np . power ( mu , <NUM_LIT:2> ) / <NUM_LIT:2> ) ) <EOL> Y = Z / np . sum ( Z , axis = - <NUM_LIT:1> ) . reshape ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . Z = Z <EOL> self . Y = Y <EOL> return Y <EOL> def backward ( self , dEdY ) : <EOL> target = dEdY != <NUM_LIT:0.0> <EOL> targetInt = target . astype ( '<STR_LIT:int>' ) <EOL> targetIdx = np . nonzero ( target ) [ <NUM_LIT:1> ] <EOL> mu = self . W [ <NUM_LIT:0> ] <EOL> pi = self . W [ <NUM_LIT:1> ] <EOL> dEdX = ( - mu [ targetIdx ] + np . dot ( self . Y , mu ) ) / float ( self . X . shape [ <NUM_LIT:0> ] ) <EOL> dEdX = dEdX . reshape ( self . Xshape ) <EOL> dEdMu = np . mean ( <EOL> ( self . X - mu ) * <EOL> ( self . Y - targetInt ) , axis = <NUM_LIT:0> ) <EOL> if self . fixExtreme : <EOL> dEdMu [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> dEdMu [ - <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> dEdPi = np . mean ( self . Y - targetInt , axis = <NUM_LIT:0> ) <EOL> self . dEdW = np . zeros ( self . W . shape ) <EOL> self . dEdW [ <NUM_LIT:0> ] = dEdMu <EOL> self . dEdW [ <NUM_LIT:1> ] = dEdPi <EOL> return dEdX <EOL> def updateLearningParams ( self , numEpoch ) : <EOL> Stage . updateLearningParams ( self , numEpoch ) <EOL> print '<STR_LIT>' , <EOL> for i in range ( self . W . shape [ - <NUM_LIT:1> ] ) : <EOL> print '<STR_LIT>' % self . W [ <NUM_LIT:0> , i ] , <EOL> print '<STR_LIT>' , <EOL> for i in range ( self . W . shape [ - <NUM_LIT:1> ] ) : <EOL> print '<STR_LIT>' % self . W [ <NUM_LIT:1> , i ] , <EOL> print </s>
