简体   繁体   English

为什么只为第一个 Conv2D 层指定 Conv2D 层的 input_shape 属性?

[英]Why should the input_shape property of a Conv2D layer be specified only for the first Conv2D layer?

I am new to AI/ML stuff.我是 AI/ML 的新手。 I'm learning TensorFlow.我正在学习 TensorFlow。 In some tutorial, I noticed that the input_shape argument of a Conv2D layer was specified only for the first.在一些教程中,我注意到Conv2D层的input_shape参数仅用于第一个。 Code looked kinda like this:代码看起来有点像这样:

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16, (3,3), activation='relu',
                           input_shape=(300,300,3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

In many examples, not only in the above, the instructor didn't include that argument in there.在许多示例中,不仅在上述示例中,讲师并未在其中包含该论点。 Is there any reason for that?有什么理由吗?

The next layers derive the required shape from the output of the previous layer.下一层从上一层的 output 派生出所需的形状。 That is, the MaxPooling2D layer derives its input shape based on the output of the Conv2D layer and so on.MaxPooling2D层根据Conv2D层的output等推导出其输入形状。 Note that in your sequential model, you don't even need to define an input_shape in the first layer.请注意,在您的顺序 model 中,您甚至不需要在第一层定义 input_shape。 It is able to derive the input_shape if you feed it real data, which gives you a bit more flexibility since you don't have to hard-code the input shape:如果您提供真实数据,它能够导出input_shape ,这为您提供了更多的灵活性,因为您不必对输入形状进行硬编码:

import tensorflow as tf
tf.random.set_seed(1)

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16, (3,3), activation='relu',),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

print(model(tf.random.normal((1, 300, 300, 3))))
tf.Tensor([[0.6059081]], shape=(1, 1), dtype=float32)

If data with an incorrect shape, for example (300, 3) instead of (300, 300, 3), is passed to your model, an error occurs because a Conv2D layer requires a 3D input excluding the batch dimension.如果将形状不正确的数据(例如 (300, 3) 而不是 (300, 300, 3))传递到 model,则会发生错误,因为Conv2D层需要 3D 输入(不包括批处理维度)。 If your model does not have an input_shape , you will, however, not be able to call model.summary() to view your network.如果您的 model 没有input_shape ,您将无法调用model.summary()来查看您的网络。 First you would have to build your model with an input shape:首先,您必须使用输入形状构建 model:

model.build(input_shape=(1, 300, 300, 3))
model.summary()

暂无
暂无

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

相关问题 Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: 如何在 Tensor Flow 2.0、keras 的 Conv2D 层中指定 input_shape - How to specify input_shape in Conv2D layer in Tensor Flow 2.0, keras 弄清楚如何为自己的数据集在Keras的Conv2D层中定义input_shape - Trouble figuring out how to define the input_shape in the Conv2D layer in Keras for my own dataset Tensorflow/Keras Conv2D 层中的输入形状错误 - Input shape error in Tensorflow/Keras Conv2D layer 为什么conv2d层需要ndim = 4输入? - Why is the conv2d layer requiring a ndim=4 input? 为什么此Keras Conv2D层与输入不兼容? - Why is this Keras Conv2D layer not compatible with the input? 用于转换和手动加载图像的Keras input_shape - Keras input_shape for conv2d and manually loaded images “conv2d”层的输入 0 与输入形状的预期轴 -1 层不兼容,其值为 3 - Input 0 of layer "conv2d" is incompatible with the layer expected axis -1 of input shape to have value 3 了解keras中conv2d层的输出形状 - Understanding the output shape of conv2d layer in keras 与输入形状维度相关的 MRI 分割错误(层 conv2d 的输入 0 与该层不兼容) - MRI Segmentation Error related to input shape dimension (Input 0 of layer conv2d is incompatible with the layer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM