代码之家  ›  专栏  ›  技术社区  ›  Vineeth Reddy

控制Keras层中的信息流和选通因子

  •  0
  • Vineeth Reddy  · 技术社区  · 8 年前

    给定CNN架构( architecture image 信息的分数“g”被发送到下一层,剩余的“1-g”被发送到前一层(如跳过连接)

    提前感谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   Daniel Möller    8 年前

    对于闸门(自动分数g):

    from keras.models import Model
    from keras.layers import *
    
    inputTensor = Input(someInputShape)
    
    #the actual value
    valueTensor = CreateSomeLayer(parameters)(inputTensor)
    
    #the gate - this is the value of 'g', from zero to 1
    gateTensor = AnotherLayer(matchingParameters, activation='sigmoid')(inputTensor)
    
    #value * gate = fraction g
    fractionG = Lambda(lambda x: x[0]*x[1])([valueTensor,gateTensor])
    
    #value - fraction = 1 - g
    complement = Lambda(lambda x: x[0] - x[1])([valueTensor,fractionG])
    
    #each tensor may go into individual layers and follow individual paths:
    immediateNextOutput = ImmediateNextLayer(params)(fractionG)
    oneOfTheForwardOutputs = OneOfTheForwardLayers(params)(complement)
    
    #keep going, make one or more outputs, and create your model:
    model = Model(inputs=inputTensor, outputs=outputTensorOrListOfOutputTensors)    
    

    #concat
    joinedTensor = Concatenate(axis=optionalAxis)([input1,input2])
    
    #add
    joinedTensor = Add()([input1,input2])
    
    #etc.....
    
    nextLayerOut = TheLayer(parameters)(joinedTensor)
    

    如果要手动控制“g”:

    gateTensor 由用户定义:

    import keras.backend as K
    
    gateTensor = Input(tensor=K.variable([g]))
    

    创建模型时,将此张量作为输入传递。(因为它是一个 tensor 输入,它不会改变您使用 fit 方法)。

    model = Model(inputs=[inputTensor,gateTensor], outputs=outputTensorOrListOfOutputTensors)