0
votes

According to the official document on tf.keras.layers.Conv2D,

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".

but actually without input_shape it does work in both graph execution and eager execution environment.

In graph execution,

import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Flatten, Dense

class CNN(tf.keras.Model):

    def __init__(self):
        super(CNN, self).__init__()
        self.conv = Conv2D(1, 3, padding='same', data_format='channels_first')
        self.flatten = Flatten()
        self.dense = Dense(1)

    def call(self, inputs):
        x = self.conv(inputs)
        x = self.flatten(x)
        return self.dense(x)


cnn = CNN()
inputs = tf.random_uniform([2, 3, 16, 16])
outputs = cnn(inputs)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    outputs = sess.run(outputs)
    print(outputs)

works without any error and in eager execution,

import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Flatten, Dense
tf.enable_eager_execution()

class CNN(tf.keras.Model):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv = Conv2D(1, 3, padding='same', data_format='channels_first')
        self.flatten = Flatten()
        self.dense = Dense(1)

    def call(self, inputs):
        x = self.conv(inputs)
        x = self.flatten(x)
        return self.dense(x)


cnn = CNN()
inputs = tf.random_uniform([2, 3, 16, 16])
outputs = cnn(inputs)
print(outputs)

also does.

Q1: Does tf.keras.layers.Conv2D as the first layer in a model truly need to specifying input_shape?

Q2: If not, when is it needed and why is it mentioned so in the official document?

UPDATE1: Tutorial on tf.keras says

The number of input dimensions is often unnecessary, as it can be inferred the first time the layer is used, but it can be provided if you want to specify it manually, which is useful in some complex models.

UPDATE2: git blame of docstring in TensorFlow source revealed that this document is copied from Keras API (which is not TensorFlow keras API).

1

1 Answers

1
votes

Convolution does generally does not need an input shape. Actually you can feed the same network different input shapes, but it is much faster, when you give tensorflow an input shape. I think that the reason, why its stated in the docs.

The original method of tensorflow does not even have an argument for input shapes.