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

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

问题描述:

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

I want to manipulate the activations of the previous layer with a custom keras layer. The below layer simply multiplies a number with the activations of the previous layer.

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个不同值的影响. 谢谢.

How should I initialize this placeholder? My goal is to use model.evaluate to test effect of different values of n the model during inference. Thanks.

我找到了一种避免对n使用数组的解决方案.

I found a solution avoiding the use of an array for n.

使用K.variable代替使用placeholder:

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

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

Then you can set the value of n like this at any time, even after compiling the model:

K.set_value(n,[anotherValue])

这使您可以继续训练而不必重新编译模型,也无需将n传递给fit方法.

This allows you to keep training without having to recompile the model, and without passing n to the fit method.

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


如果要使用许多类似的输入,您可以做到:


If working with many inputs like that, you can make it:

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

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

And inside the layers, the second input which is the tensor for n will have 4 elements:

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