[英]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.