如何将变量转换为keras层

时间:2018-10-10 01:17:16

标签: python keras

输出为

tf.Variable 'Variable_1:0' shape=(10, 15, 9488) dtype=float32_ref

我想将其转换为keras层。

input_output = Input(shape=(10,512))
model = Model(input_output, output) 

有什么办法吗?

当前,错误是

Output tensors to a Model must be the output of a Keras Layer (thus holding past layer metadata).
Found:
tf.Variable 'Variable_1:0' shape=(10, 15, 9488) dtype=float32_ref

============================================
我尝试使用Lambda,但出现以下错误:
ValueError:模型的输出张量必须是Keras Layer
的输出 (因此保存过去的图层元数据)。
找到:位于0x7fad52faf550的keras.layers.core.Lambda对象

这就是我所做的:

def convert_tensor(self, outputs):
    return outputs

input_result = Input(shape=(int(outputs.shape[0]), int(outputs.shape[1]),int(outputs.shape[2])))
outputs = Lambda(self.convert_tensor(outputs), output_shape=(10, 15, 9488))
model = Model(input_result, outputs)

1 个答案:

答案 0 :(得分:0)

您没有正确使用Lambda层,应该像这样:

input_result = Input(shape=(int(outputs.shape[0]), int(outputs.shape[1]),int(outputs.shape[2])))
outputs = Lambda(self.convert_tensor, output_shape=(10, 15, 9488))(input_result)
model = Model(input_result, outputs)

您必须将函数传递给lambda,而不是函数调用,然后使用适当的输入“调用” lambda。

相关问题