def setDelta(self, delta): if (self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['OUTPUT']): raise myerror.MyError("Invalid unit type!") self.delta = delta
def getDelta(self): if (self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['OUTPUT']): raise myerror.MyError("Invalid unit type!") return self.delta
def setInput(self, value): if (self.unitType != UnitType['INPUT'] and self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['OUTPUT']): raise myerror.MyError("Invalid unit type!") self.inputValue = value
def getInput(self): if (self.unitType != UnitType['INPUT'] and self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['OUTPUT']): raise myerror.MyError("Invalid unit type!") return self.inputValue
def getRightConnections(self): if (self.unitType != UnitType['INPUT'] and self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['BIAS']): raise myerror.MyError("Invalid unit type!") return self.rightConnections
def getLeftConnections(self): if (self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['OUTPUT']): raise myerror.MyError("Invalid unit type!") return self.leftConnections
def setRightConnections(self, connections): if (self.unitType != UnitType['INPUT'] and self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['BIAS']): raise myerror.MyError("Invalid unit type!") self.rightConnections = connections
def setLeftConnections(self, connections): if (self.unitType != UnitType['HIDDEN'] and self.unitType != UnitType['OUTPUT']): raise myerror.MyError("Invalid unit type!") self.leftConnections = connections
def __init__(self, param): # check parameter if param == None: raise myerror.MyError("NUll parameter") if ('numOfUnits' in param) == False: raise myerror.MyError("numOfUnits must be specified") if len(param['numOfUnits']) <= 2: raise myerror.MyError("At least 1 hidden units must be specified") # default weight self.DEFAULT_WEIGHT = 0 # number of units self.numOfUnits = param['numOfUnits'] # learning coefficient self.learningCoefficient = 0.01 # mini batch size self.miniBatchSize = 10 # connection objects self.connections = {} # unit objects self.units = {} # average of input value self.inputMeans = [] if 'means' in param: self.inputMeans = param['means'] # standard deviation of inputs value self.inputSDs = [] if 'sds' in param: self.inputSDs = param['sds'] # initialize all of processes # all layers layerArray = [] # initialize input layer # lead is BIAS unit inputUnitArray = [] inputUnitArray.append(unit.Unit(unit.UnitType['BIAS'])) for i in xrange(0, self.numOfUnits[0]): inputUnitArray.append(unit.Unit(unit.UnitType['INPUT'])) layerArray.append(inputUnitArray) # initialize hidden layers # lead is BIAS unit for j in xrange(1, (len(self.numOfUnits) - 1)): hiddenUnitArray = [] hiddenUnitArray.append(unit.Unit(unit.UnitType['BIAS'])) for k in xrange(0, self.numOfUnits[j]): hiddenUnitArray.append(unit.Unit(unit.UnitType['HIDDEN'])) layerArray.append(hiddenUnitArray) # initialize output layer outputUnitArray = [] for l in xrange(0, self.numOfUnits[len(self.numOfUnits) - 1]): outputUnitArray.append(unit.Unit(unit.UnitType['OUTPUT'])) layerArray.append(outputUnitArray) # unit objects self.units = layerArray # utility dnnUtil = utility.Util() # generation of connections allConnectionArray = [] for m in xrange(0, (len(self.numOfUnits) - 1)): connectionArray = [] for n in xrange(0, len(self.units[m])): connArray = [] leftUnit = self.units[m][n] for p in xrange(0, len(self.units[m + 1])): rightUnit = self.units[m + 1][p] if rightUnit.getUnitType() != unit.UnitType['BIAS']: conn = connection.Connection() conn.setRightUnit(rightUnit) conn.setLeftUnit(leftUnit) if leftUnit.getUnitType() == unit.UnitType['BIAS']: conn.setWeight(self.DEFAULT_WEIGHT) else: conn.setWeight(self.DEFAULT_WEIGHT + dnnUtil.rnorm(0, 1)) connArray.append(conn) connTmpArray = rightUnit.getLeftConnections() connTmpArray.append(conn) rightUnit.setLeftConnections(connTmpArray) connectionArray.append(connArray) leftUnit.setRightConnections(connArray) allConnectionArray.append(connectionArray) self.connections = allConnectionArray if 'weights' in param: for s in xrange(0, len(self.connections)): for t in xrange(0, len(self.connections[s])): for u in xrange(0, len(self.connections[s][t])): self.connections[s][t][u].setWeight( param['weights'][s][t][u])