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