Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow-ValueError:模型的输出张量必须是Tensorflow`层的输出`_Python_Tensorflow_Keras - Fatal编程技术网

Python Tensorflow-ValueError:模型的输出张量必须是Tensorflow`层的输出`

Python Tensorflow-ValueError:模型的输出张量必须是Tensorflow`层的输出`,python,tensorflow,keras,Python,Tensorflow,Keras,我已经在TensorFlow 2.0中使用Keras函数API创建了一个RNN,下面的代码在其中工作 sum\u input=keras.input(形状=(单位大小,256),名称='sum') x=tf.unstack(总和输入,轴=2,数量=256) t_sum=x[0] 对于范围内的i(len(x)-1): t_sum=keras.layers.Add() sum\u m=keras.Model(输入=sum\u输入,输出=t\u sum,name='sum\u Model') 然后我

我已经在TensorFlow 2.0中使用Keras函数API创建了一个RNN,下面的代码在其中工作

sum\u input=keras.input(形状=(单位大小,256),名称='sum')
x=tf.unstack(总和输入,轴=2,数量=256)
t_sum=x[0]
对于范围内的i(len(x)-1):
t_sum=keras.layers.Add()
sum\u m=keras.Model(输入=sum\u输入,输出=t\u sum,name='sum\u Model')
然后我不得不改为Tensorflow 1.13,这给了我以下错误

ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: Tensor("add_254/add:0", shape=(?, 40), dtype=float32)
我不明白为什么输出张量不是来自Tensorflow层,因为t_sum是keras.layers.Add的输出


我曾尝试按照中的建议将部分代码包装到keras.layers.Lambda中,但它似乎对我不起作用

问题不在于
Add()
层,而在于
tf.unstack()
-它不是
keras.layers.layer()的实例。您可以将其包装为自定义图层:

将tensorflow导入为tf
类取消堆叠(tf.keras.layers.Layer):
定义初始化(自):
super(Unstack,self)。\uuuu init\uuuuu()
def调用(自我,输入,num=256):
返回tf.unstack(输入,轴=2,数量=num)
x=取消堆叠()(总和输入)
或者,您可以使用
Lambda
layer来代替子类化:

x=tf.keras.layers.Lambda(Lambda t:tf.unstack(t,axis=2,num=256))(总和输入)

我们可以使用
Lambda
层来包装
tf.unstack()
?@chandresh当然可以,
x=tf.keras.layers.Lambda(Lambda t:tf.unstack(t,axis=2,num=256))(输入)