简体   繁体   English

ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata)

[英]ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata)

I'm having trouble with building a Functional Keras model with a customized Keras layer.我在使用自定义 Keras 层构建功能 Keras model 时遇到问题。 The goal is to get a model whose output is that of a regular function expressed in terms of a customized Keras layer.目标是获得一个 model,其 output 是常规 function 的 function,以自定义 ZCBAFEE7BB63EFA729 层表示。 My code is based on the ComputeSum Layer class found here .我的代码基于此处找到的ComputeSum层 class。

Imports:进口:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Layer, Input

Customized Layer (non-trainable):自定义层(不可训练):

class ComputeSum(Layer):

  def __init__(self, input_dim):
      super(ComputeSum, self).__init__()
      # Create a non-trainable weight.
      self.total = tf.Variable(initial_value=tf.zeros((input_dim,)),
                               trainable=False)

  def call(self, inputs):
      self.total.assign_add(tf.reduce_sum(inputs, axis=0))
      return self.total

Model: Model:

def ComputeSumModel(tensor):
    inputs = Input(shape = tf.shape(tensor))
    outputs = ComputeSum(len(tensor))(tensor)
    
    model = keras.Model(inputs = inputs, outputs = outputs)

    return model

Testing directly on customized Layer:直接在自定义层上测试:

these = tf.ones((4,4))

that = ComputeSum(len(these))

those = that(these)

those.numpy()

Result: array([4., 4., 4., 4.], dtype=float32)结果: array([4., 4., 4., 4.], dtype=float32)

Testing using model function:使用 model function 进行测试:

those = ComputeSumModel(these)

Result: ValueError: Output tensors of a Functional model must be the output of a TensorFlow 'Layer' (thus holding past layer metadata). Found: <tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([4., 4., 4., 4.], dtype=float32)> Result: ValueError: Output tensors of a Functional model must be the output of a TensorFlow 'Layer' (thus holding past layer metadata). Found: <tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([4., 4., 4., 4.], dtype=float32)> ValueError: Output tensors of a Functional model must be the output of a TensorFlow 'Layer' (thus holding past layer metadata). Found: <tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([4., 4., 4., 4.], dtype=float32)>

What am I doing wrong here?我在这里做错了什么? It knows what the output of my customized layer should be, but it doesn't seem to like that output for the Functional model.它知道我的自定义层的 output 应该是什么,但它似乎不喜欢功能 model 的 output。 Any help or suggestions would be appreciated.任何帮助或建议将不胜感激。 I'm still new to Keras and TensorFlow in general.一般来说,我对 Keras 和 TensorFlow 还是新手。

You first need to create your model object before passing data into it.在将数据传递到其中之前,您首先需要创建model object。 Also your model's input has to be fed to the ComputeSum layer.您的模型的input也必须馈送到ComputeSum层。 Try something like this:尝试这样的事情:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Layer, Input

class ComputeSum(Layer):

  def __init__(self, input_dim):
      super(ComputeSum, self).__init__()
      # Create a non-trainable weight.
      self.total = tf.Variable(initial_value=tf.zeros((input_dim,)),
                               trainable=False)

  def call(self, inputs):
      self.total.assign_add(tf.reduce_sum(inputs, axis=0))
      return self.total

def ComputeSumModel(input_shape):
    inputs = Input(shape = input_shape)
    outputs = ComputeSum(input_shape[0])(inputs)
    
    model = keras.Model(inputs = inputs, outputs = outputs)

    return model

these = tf.ones((4,4))
that = ComputeSum(len(these))
those = that(these)
print(those)
model = ComputeSumModel((tf.shape(these)[1],))
print(model(these))
<tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([4., 4., 4., 4.], dtype=float32)>
<tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([4., 4., 4., 4.], dtype=float32)>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模型的输出张量必须是模型Api Tensorfow中的TensorFlow`Layer`(因此保留了过去的层元数据)的输出 - Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata) in Model Api Tensorfow 模型的输出张量必须是 Keras `Layer` 的输出(因此保存过去的层元数据)。 CNN LSTM 使用函数式 api 时 - Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). When using functional api for CNN LSTM 如何解决 ValueError:模型的输出张量必须是 Keras `Layer` 的输出(因此保存过去的层元数据)。? - How can I resolve ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata).? 模型的Keras输出张量必须是Keras层的输出(因此保留了过去的层元数据) - Keras Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata) TensorFlow 自动编码器尝试:ValueError:功能模型的输出张量必须是 TensorFlow `Layer` 的输出 - Tensorflow autoencoder attempt: ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` Tensorflow - ValueError:模型的输出张量必须是 TensorFlow `Layer` 的输出 - Tensorflow - ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` ValueError:模型的输出张量必须是TensorFlow`Layer`的输出 - ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` ValueError:模型的输出张量必须是带有tf.keras Lambda层的TensorFlow层的输出 - ValueError: Output tensors to a Model must be the output of a TensorFlow Layer with tf.keras Lambda layer Keras输出张量到模型必须是TensorFlow`Telay`的输出 - Keras Output tensors to a Model must be the output of a TensorFlow `Layer` tensorflow-gpu ValueError:未知层:功能 - tensorflow-gpu ValueError: Unknown layer: Functional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM