如何使用 Tensorflow 张量设置功能模型 Keras 层的输入?

2024-02-26

我想使用两个包,一个是用Keras1.2编写的,另一个是用tensorflow编写的。我想将在张量流中构建的架构的一部分使用到 Keras 模型中。

建议部分解决方案here https://stackoverflow.com/questions/42441431/how-to-set-the-input-of-a-keras-layer-with-a-tensorflow-tensor,但它适用于顺序模型。关于功能模型的建议——将预处理包装在 Lambda 层中——并没有奏效。

以下代码有效:

inp = Input(shape=input_shape)
def ID(x):
    return x
lam = Lambda(ID)  
flatten = Flatten(name='flatten')
output = flatten(lam(inp))
Model(input=[inp], output=output)

但是,更换时flatten(lam(inp))带有预处理的输出张量flatten(lam(TF_processed_layer)),我得到:“模型的输出张量必须是 Keras 张量。发现:Tensor("Reshape:0", shape=(?, ?), dtype=float32)"


您可以尝试将输入张量包装到 Keras 输入层中,并从那里继续构建模型。就像这样:

inp = Input(tensor=tftensor,shape=input_shape)
def ID(x):
    return x
lam = Lambda(ID)  
flatten = Flatten(name='flatten')
output = flatten(lam(inp))
Model(input=inp, output=output)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Tensorflow 张量设置功能模型 Keras 层的输入? 的相关文章

随机推荐