python - 如何将变量转换为keras层

标签 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
的输出 (因此持有过去的层元数据)。
发现:keras.layers.core.Lambda 对象位于 0x7fad52faf550

这是我所做的:

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)

最佳答案

你没有正确使用 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。

关于python - 如何将变量转换为keras层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52731302/

相关文章:

python - 语句 : with and tarfile

keras - 如何为Convolution2D设置权重?

javascript - 在 tensorflow js api 0.15.3 中设置卷积层的配置

python - Keras 中局部连接层的维度

Python json.load JSONDecodeError : Extra data error when trying load multiple json dictionaries

python - 如何根据位置计算箱子的数量?

python - Keras 和 TensorBoard - AttributeError : 'Sequential' object has no attribute '_get_distribution_strategy'

python - 以 tensorflow 变量作为输入的 keras 前向传递

python - 根据numpy数组中一行中的重复值删除列

python - 如何检查你的模型在 Django 中没有错误