In Keras
, the input layer itself is not a layer, it is a tensor. It's the starting tensor we send to the first hidden layer. A Keras input_shape
argument requires a subscribable object in which the size of each dimension could be stored as an integer. Following are all the valid approaches:
tfd = tf.keras.layers.Dense(1, input_shape=(3,))
x = tfd(tf.ones(shape=(5, 3)))
print(x.shape) # (5, 1)
or,
tfd = tf.keras.layers.Dense(1, input_shape=[3])
x = tfd(tf.ones(shape=(5, 3)))
print(x.shape) # (5, 1)
Note, we can't pass only input_shape=3
as it's not subscribable. Likewise,
tfd = tf.keras.layers.Dense(1, input_shape=(224, 224, 3))
x = tfd(tf.ones(shape=(5, 3)))
print(x.shape) # (5, 1)
or,
tfd = tf.keras.layers.Dense(1, input_shape=[224, 224, 3])
x = tfd(tf.ones(shape=(5, 3)))
print(x.shape) # (5, 1)
This tensor must have the same shape as our training data. When you set input_shape=(224, 224, 3)
that means you have training data which is an RGB image with the shape of 224 x 224
. The model never knows this shape at first, so we need to manually set it. This is mostly a general picture for Image modeling. And same as this goes to the RNN or sequence modeling: input_shape=(None, features)
or input_shape=(features, )