Пример #1
0
 def __init__(self, Para):
     PassThrough.__init__(self, Para)
     try:
         self.activationType = Para['activationType']
         if not (self.activationType in Activation.activationTypes):
             self.printError('Activation type not defined')
     except:
         self.printError('Activation type not defined')
Пример #2
0
 def __init__(self, para):
     PassThrough.__init__(self, para)
     try:
         self.poolType = para['poolType']
         if not (self.poolType in self.poolTypes):
             self.printError('Pool type not defined.')
         self.stride = para['stride']
         self.kernelShape = para['kernelShape']
     except:
         self.printError('Parameters not complete.')
Пример #3
0
 def createAndInitializeStruct(self):
     PassThrough.createAndInitializeStruct(self)
     # set bias to zeros
     self.sBeta = np.zeros(self.outChannel,
                           dtype=float)  # bias intialized to zeros
     self.sGamma = np.ones(self.outChannel,
                           dtype=float)  # Gamma initialized to ones
     self.normD = Scale.normDimList[len(self.inpShape) -
                                    2]  # axises to calculate mean and std
     self.dGamma = np.zeros(self.outChannel, dtype=float)
     self.dBeta = np.zeros(self.outChannel, dtype=float)
     self.paraDict['sBeta'] = self.sBeta
     self.paraDict['sGamma'] = self.sGamma
     self.dParaDict['sBeta'] = self.dBeta
     self.dParaDict['sGamma'] = self.dGamma
     self.createOptimizerStruct()
Пример #4
0
 def createAndInitializeStruct(self):
     PassThrough.createAndInitializeStruct(self)
     self.normD = Normalize.normDimList[len(
         self.inpShape) - 2]  # axises to calculate mean and std
     self.mean = np.zeros(self.outChannel, dtype=float)
     self.var = np.zeros(self.outChannel, dtype=float)
     self.std = np.zeros(self.outChannel, dtype=float)
     self.dMean = np.zeros(self.outChannel, dtype=float)
     self.dVar = np.zeros(self.outChannel, dtype=float)
     self.meanList = np.zeros((Normalize.numStats, self.outChannel),
                              dtype=float)
     self.stdList = np.zeros((Normalize.numStats, self.outChannel),
                             dtype=float)
     self.tmp = np.zeros(self.inpShape, dtype=float)
     self.tmp2 = np.zeros(self.outChannel, dtype=float)
     self.count = 0
     self.normComputed = False
Пример #5
0
class Fork2(PassThrough):
    def __init__(self, para):
        PassThrough.__init__(self, para)
        self.skip = PassThrough(
            {'instanceName': para['instanceName'] + '_skip'})
        self.main = PassThrough(
            {'instanceName': para['instanceName'] + '_main'})

    def fork(self, topSkip, topMain, bot):
        self.skip.stack(topSkip, bot)
        self.main.stack(topMain, bot)
        self.bottom = bot.topInterface

    def createAndInitializeStruct(self):
        self.skip.createAndInitializeTopStruct()
        self.main.createAndInitializeTopStruct()
        self.createAndInitializeBotStruct()

    def forward(self):
        cbs = Layer.currentBatchSize
        np.copyto(self.skip.out[:cbs], self.bottom.getOutput()[:cbs])
        np.copyto(self.main.out[:cbs], self.bottom.getOutput()[:cbs])

    def backward(self):
        cbs = Layer.currentBatchSize
        np.add(self.skip.top.getInputDerivative()[:cbs],
               self.main.top.getInputDerivative()[:cbs],
               out=self.inpDeriv[:cbs])

    def printIOShape(self):
        print(colors.BOLD + self.instanceName + colors.END, end='')
        print(' input:',
              self.bottom,
              ' ',
              self.bottom.getOutput().shape,
              end='')
        print(' skip:', self.skip.getOutput().shape, end='')
        print(' main:', self.main.getOutput().shape)
Пример #6
0
    def createAndInitializeStruct(self):
        self.inpShape = self.bottom.getOutput().shape
        (_, hIn, wIn, cIn) = self.inpShape
        (kH, kW) = self.kernelShape
        self.kArea = kH * kW
        hOut = (hIn - kH) // self.stride + 1
        wOut = (wIn - kW) // self.stride + 1
        self.outChannel = cIn
        self.outShape = (Layer.batchSize, hOut, wOut, self.outChannel)
        self.zerosArray = np.zeros(self.inpShape[1:], dtype=float)

        if self.top.isPadded():
            (pHOut, pWOut) = self.top.getPadShape()
            pOutShape = (Layer.batchSize, self.outShape[1] + 2 * pHOut,
                         self.outShape[2] + 2 * pWOut, self.outChannel)
            self.pOut = np.zeros(pOutShape, dtype=float)  # padded activations
            self.out = self.pOut[:, pHOut:-pHOut, pWOut:-pWOut, :]
        else:
            self.pOut = np.zeros(self.outShape, dtype=float)
            self.out = self.pOut

        self.tmpDA = np.zeros(self.outShape, dtype=float)
        PassThrough.createAndInitializeBotStruct(self)
Пример #7
0
class Sum2(PassThrough):
    def __init__(self,para):
        PassThrough.__init__(self,para)
        self.skip = PassThrough({'instanceName':para['instanceName']+'_skip'})
        self.main = PassThrough({'instanceName':para['instanceName']+'_main'})
    
    def sum(self,top, botSkip,botMain):
        self.skip.stack(top,botSkip)
        self.main.stack(top,botMain)
        self.top = top.bottomInterface
        # set the bottom to the main path. for collecting dimension only
        self.bottom = self.main.bottom
    
    def createAndInitializeStruct(self):
        self.skip.createAndInitializeBotStruct()
        self.main.createAndInitializeBotStruct()
        # check the skip and main path input dimensions 
        if self.skip.bottom.getOutput().shape != self.main.bottom.getOutput().shape:
            self.printError("Skip and main input dimensions do not match.")            
        self.createAndInitializeTopStruct()
        
    def forward(self):
        cbs = Layer.currentBatchSize
        np.add(self.skip.bottom.getOutput()[:cbs],self.main.bottom.getOutput()[:cbs],
               out=self.out[:cbs])  
    
    def backward(self):
        cbs = Layer.currentBatchSize 
        np.copyto(self.main.inpDeriv[:cbs],self.top.getInputDerivative()[:cbs])
        np.copyto(self.skip.inpDeriv[:cbs],self.top.getInputDerivative()[:cbs])             
    
    def printIOShape(self):
        print(colors.BOLD+self.instanceName+colors.END,end='')
        print(' skip:',self.skip.bottom,' ',self.skip.bottom.getOutput().shape,end='')
        print(' main:',self.main.bottom,' ',self.main.bottom.getOutput().shape,end='')
        print(' output:',self.out.shape)
Пример #8
0
 def __init__(self, para):
     PassThrough.__init__(self, para)
     self.skip = PassThrough(
         {'instanceName': para['instanceName'] + '_skip'})
     self.main = PassThrough(
         {'instanceName': para['instanceName'] + '_main'})
Пример #9
0
 def __init__(self, para):
     PassThrough.__init__(self, para)
Пример #10
0
 def createAndInitializeStruct(self):
     PassThrough.createAndInitializeStruct(self)
     self.lossOut = []