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