Reputation: 3281
I just read the official tutorial in this page. And the code example to create a model is below:
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
# Create an instance of the model
model = MyModel()
My question is, why in the code above we didn't need to specify an input shape in the first layer (Conv2D)? Is there any official documentation that mention this behaviour?
Because if I read the official docs about Conv Layer it said:
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format="channels_last"
Upvotes: 0
Views: 261
Reputation: 197
Everything is working as intended.
Convolution as an operation works regardless of the input shape. Just the channels of the input needs to match. You can see this behavior work as expected in tf.nn.conv2d. (Which is what your code snippet is using)
Now, you are linking a reference to keras.conv2d which forces the user to specify input shape to make the code more readable and 'validate' user input.
Upvotes: 1