jayko03
jayko03

Reputation: 2481

which type of input_shape should I use in tensorflow.keras?

I am learning TensorFlow through its documentation and a little bit confused about the input_shape type in the first layer. Some of the examples have list, but usually, it is a tuple. Is there any specific case that I have to use a certain type?

# I am learning RNN and see this example.
tf.keras.layers.Dense(100, input_shape=[30])
tf.keras.layers.Dense(1)

vs

# This is what I usually see
tf.keras.layers.Dense(32, input_shape=(224, 224, 3)),
tf.keras.layers.Dense(32)

It seems like it depends on my data and some other facts, but I don't know which one determines its type.

Upvotes: 0

Views: 727

Answers (2)

Dr. Snoopy
Dr. Snoopy

Reputation: 56417

You can use either a list or a tuple to define the input shape, they both give the same result, see this example:

import tensorflow as tf

>>> tf.keras.Input(shape=(10,))
<tf.Tensor 'input_1:0' shape=(?, 10) dtype=float32>

>>> tf.keras.Input(shape=[10])
<tf.Tensor 'input_2:0' shape=(?, 10) dtype=float32>

>>> tf.keras.Input(shape=(32,32,3))
<tf.Tensor 'input_3:0' shape=(?, 32, 32, 3) dtype=float32>

>>> tf.keras.Input(shape=[32,32,3])
<tf.Tensor 'input_4:0' shape=(?, 32, 32, 3) dtype=float32>

It is up to you, there is no advantage or disadvantage of using the either. The same applies for input_shape in a layer.

Upvotes: 1

Innat
Innat

Reputation: 17239

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, )

Upvotes: 0

Related Questions