TensorFlow构建自编码器:如何设置input_shape以生成模型摘要
解决Autoencoder模型build时input_shape无效的问题
- 你的问题出在input_shape缺少了图像的通道维度。针对28x28的灰度图像(比如常见的MNIST数据集),TensorFlow需要明确指定通道数(单通道为1)。
- 正确的
input_shape设置应为(28, 28, 1),注意无需包含批量维度(None),因为build方法的input_shape仅需指定单个样本的形状。 - 调整后的代码示例:
autoencoder.build(input_shape=(28, 28, 1)) autoencoder.summary() - 此外,也可以通过传入符合形状的测试张量自动构建模型,示例如下:
import tensorflow as tf test_input = tf.random.normal([1, 28, 28, 1]) test_output = autoencoder(test_input) autoencoder.summary()
内容的提问来源于stack exchange,提问作者deckard
相关产品推荐
相关产品推荐

