简体   繁体   English

TensorFlow 自动编码器尝试:ValueError:功能模型的输出张量必须是 TensorFlow `Layer` 的输出

[英]Tensorflow autoencoder attempt: ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer`

I am encountering the following error while trying to write an encoder model for a variational autoencoder:我在尝试为变分自动编码器编写编码器模型时遇到以下错误:

ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: [[-0.02135764 -0.01809833  0.00880998 ... -0.02307652  0.00028993 0.00441882]...

Below is a simplified version of my code.下面是我的代码的简化版本。 The issue appears to be coming from the type of x, which is apparently <class 'tensorflow.python.framework.ops.EagerTensor'> .问题似乎来自 x 的类型,显然是<class 'tensorflow.python.framework.ops.EagerTensor'>

def encoder(inputs, latent_dim):

encoder_inputs = tf.keras.Input(shape=(None, 248, 40, 1), name="input_layer")
x = tf.keras.layers.ZeroPadding2D(padding=(3, 0))(inputs)
x = tf.keras.layers.Conv2D(32, (9, 1), strides=(5, 1), batch_size=batch_size)(x)

model = tf.keras.Model(encoder_inputs, x, name="encoder")
return model

I have tried creating the model with output x.output instead of simply x , but EagerTensors have no such attribute.我尝试使用输出x.output而不是简单地x创建模型,但 EagerTensors 没有这样的属性。 :( What am I doing wrong? Thanks in advance. :( 我做错了什么?提前谢谢。

Some additional code to provide more context.一些额外的代码来提供更多的上下文。 I'm calling the function as follows, where X_train is a 20x248x40x1 numpy array (20 inputs of shape 248x40x1).我按如下方式调用该函数,其中 X_train 是一个 20x248x40x1 numpy 数组(20 个输入,形状为 248x40x1)。

model1 = encoder(X_train, 100)

It should be它应该是

encoder_inputs

on the first line like here:在第一行像这里:

encoder_inputs = keras.Input(shape=(None, None, 3))
processed = keras.layers.RandomCrop(width=32, height=32)(encoder_inputs)
conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed)
pooling = keras.layers.GlobalAveragePooling2D()(conv)
feature = keras.layers.Dense(10)(pooling)

full_model = keras.Model(encoder_inputs, feature)
backbone = keras.Model(processed, conv)
activations = keras.Model(conv, feature)

Or more specific it is about what you are trying to do ... you want an model that does the computation so create the model than add stuff to it.或者更具体地说,它是关于你正在尝试做的事情......你想要一个进行计算的模型,所以创建模型而不是添加东西。 So i define your encoder and its layer structure所以我定义了你的编码器及其层结构

def encoder():

  encoder_inputs = tf.keras.Input(shape=(None, 248, 40, 1), 
                                  name="input_layer")
  x = tf.keras.layers.ZeroPadding2D(padding=(3, 0))(encoder_inputs)
  x = tf.keras.layers.Conv2D(32, (9, 1), strides=(5, 1), 
  batch_size=batch_size)(x)

  model = tf.keras.Model(encoder_inputs, x, name="encoder")
  return model

then inputs is used later like here, for example to minimize the Mean square error between your inputs and some targets with the encoder model:然后输入稍后像这里一样使用,例如使用编码器模型最小化输入和一些目标之间的均方误差:

  inputs = your_inputs
  targets = yout targets
  model = encoder()
  model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])
  model.fit(inputs,targets)

More info can be found at tf.keras.Model更多信息可以在tf.keras.Model找到

暂无
暂无

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

相关问题 Tensorflow - ValueError:模型的输出张量必须是 TensorFlow `Layer` 的输出 - Tensorflow - ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` 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) 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` 模型的输出张量必须是模型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).? 张量流自动编码器的输出始终相同 - Always same output for tensorflow autoencoder TypeError:模型的输出张量必须是Keras张量 - TypeError: Output tensors to a Model must be Keras tensors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM