python - 类型错误 : 'Tensor' object is not callable | Keras Autoencoder

标签 python tensorflow keras

我在 this tutorial 之后在 Keras 中创建一个自动编码器我不断收到以下错误。

decoder = tf.keras.Model(encoded_input, decoded(input_img))
TypeError: 'Tensor' object is not callable

我认为这与由于此类对象的性质而无法以这种方式使用张量有关,但我在理解为什么以及如何解决这个问题方面存在一些差距。

这是我的代码的一个最小工作示例:

# input_img input placeholder
input_img = tf.keras.layers.Input(shape=(16, 16, 1), name ="input")
encoded = tf.keras.layers.Dense(encoding_dim, activation='relu')(input_img)
decoded = tf.keras.layers.Dense(256, activation='sigmoid')(encoded)
autoencoder = tf.keras.Model(input_img, decoded)
encoder = tf.keras.Model(input_img, encoded)
encoded_input = tf.keras.layers.Input(shape=(encoding_dim,))
decoder = tf.keras.Model(input_img, decoded(encoded_input))

最佳答案

Keras Model 期望输入和输出参数为,而不是张量。本教程是正确的 - 您似乎错过了 decoder = 之前的一行: -- decoder_layer = autoencoder.layers[-1]

一起,decoder = 适当改变:

input_img = tf.keras.layers.Input(shape=(16, 16, 256), name ="input")
encoded   = tf.keras.layers.Dense(encoding_dim, activation='relu')(input_img)
decoded   = tf.keras.layers.Dense(256, activation='sigmoid')(encoded)

autoencoder = tf.keras.Model(input_img, decoded)
encoder     = tf.keras.Model(input_img, encoded)

encoded_input = tf.keras.layers.Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1] # gets 'decoded' = last layer of 'autoencoder'

decoder = tf.keras.Model(encoded_input, decoder_layer(encoded_input))

关于python - 类型错误 : 'Tensor' object is not callable | Keras Autoencoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614513/

相关文章:

python - 获取 pandas groupby 中组的所有值

python - Keras 加载彩色图像

python - MNIST图像预测模型

python - 如何在 TensorFlow 图中添加 if 条件?

python - Keras ImageDataGenerator 具有用于旋转和平移的中心裁剪

python - Keras:Binary_crossentropy 具有负值

python - 使用 ruamel.yaml 更新包含多个 yaml 的 yaml 文件中的 yaml block

python - Pygame 圆及其关联的用于碰撞检测的矩形

android - 如何保存图像分类模型并将其用于 android

machine-learning - Keras 目标尺寸不匹配