给定CNN架构(architecture image),其中从一层到另一层的信息流由门控因子控制。 信息的“g”部分被发送到下一层,剩下的“1-g”被发送到其中一个前向层(如跳过连接)
如何在Keras实施这样的架构? 提前致谢
答案 0 :(得分:1)
使用功能API模型。
对于门(自动分数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)
在这种情况下,我们所要做的就是用用户定义的gateTensor
替换import keras.backend as K
gateTensor = Input(tensor=K.variable([g]))
:
tensor
创建模型时,将此张量作为输入传递。 (因为它是fit
输入,它不会改变您使用model = Model(inputs=[inputTensor,gateTensor], outputs=outputTensorOrListOfOutputTensors)
方法的方式。
year B C startyear endyear
2010 2 A 2012 2014
2011 2 A 2010 2013
2013 2 B .. ..
2012 2 C``