初始化keras占位符作为自定义层的输入


问题内容

我想使用自定义keras层来操纵上一层的激活。下一层只是将数字与上一层的激活数相乘。

class myLayer(Layer):

def __init__(self, **kwargs):
    super(myLayer, self).__init__(**kwargs)

def build(self, input_shape):
    self.output_dim = input_shape[0][1]
    super(myLayer, self).build(input_shape)

def call(self, inputs, **kwargs):
    if not isinstance(inputs, list):
        raise ValueError('This layer should be called on a list of inputs.')

    mainInput = inputs[0]
    nInput = inputs[1]

    changed = tf.multiply(mainInput,nInput)

    forTest  = changed
    forTrain = inputs[0]

    return K.in_train_phase(forTrain, forTest)

def compute_output_shape(self, input_shape):
    print(input_shape)
    return (input_shape[0][0], self.output_dim)

我正在创建模型

inputTensor = Input((5,))
out = Dense(units, input_shape=(5,),activation='relu')(inputTensor)

n = K.placeholder(shape=(1,))
auxInput = Input(tensor=n)
out = myLayer()([out, auxInput])

out = Dense(units, activation='relu')(out)
out = Dense(3, activation='softmax')(out)
model = Model(inputs=[inputTensor, auxInput], outputs=out)   
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics='acc'])

尝试使用时出现此错误

model.fit(X_train, Y_train, epochs=epochs, verbose=1)

错误

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_3' with dtype float and shape [1]

当我尝试将值赋予占位符时

model.fit([X_train, np.array([3])], Y_train, epochs=epochs, verbose=1)

我得到:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 2 arrays:

我应该如何初始化这个占位符?我的目标是使用model.evaluate在推理期间测试模型中n个不同值的影响。谢谢。


问题答案:

我找到了一种避免将数组用于的解决方案n

代替使用a placeholder,而使用K.variable

n = K.variable([someInitialValue])
auxInput = Input(tensor=n)

然后n,即使在编译模型之后,您也可以随时设置这样的值:

K.set_value(n,[anotherValue])

这使您无需重新编译模型,也无需传递nfit方法即可继续进行训练。

model.fit(X_train,Y_train,....)

如果使用许多类似的输入,则可以:

n = K.variable([val1,val2,val3,val4]) #tensor definition
K.set_value(n,[new1,new2,new3,new4]) #changing values

在图层内部,第二个输入(即张量)n将包含4个元素:

n1 = inputs[1][0]
n2 = inputs[1][1]
....