0

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
jayko03
  • 2,329
  • 7
  • 28
  • 51

2 Answers2

1

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.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
0

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

Innat
  • 16,113
  • 6
  • 53
  • 101
  • Check [this](https://stackoverflow.com/questions/38714959/understanding-keras-lstms/50235563#50235563) answer for more sequence modeling approaches. Hope that helps. – Innat Mar 13 '21 at 07:31
  • 1
    This does not answer what the question is actually asking. – Dr. Snoopy Mar 13 '21 at 09:57
  • Please let the OP decide this. I felt this answered what he asked. If he confirms it's not what he wanted, I'll remove it. – Innat Mar 13 '21 at 10:08
  • No, we can also read, the question asks about type of the input shape (tuple or list), and your answer does not deal with that at all. – Dr. Snoopy Mar 13 '21 at 10:10
  • Yes, I informed it in the first paragraph of my answer. Or should I include more explanation? – Innat Mar 13 '21 at 10:20
  • Yes you could mention that a list and tuple both serve the same purpose and it does not matter which you use in this case – Dr. Snoopy Mar 13 '21 at 10:21
  • I'm not sure if the value of `input_shape` can be set as `list` rather than `tuple`. Please correct me. As I already asked OP, his `input_shape=[30]` statement confuses me a bit. – Innat Mar 13 '21 at 10:30
  • That detail is exactly what the question is about, which goes back to my first comment. – Dr. Snoopy Mar 13 '21 at 13:05
  • I know it is starting tensor which I have to pass specifically to the my layers. But I am confused with its type. As I asked in my question, I saw some example uses `input_shape=[ ]`, but I also saw `input_shape=( )`. So I was wondering which type of argument does `input_shape` take. – jayko03 Mar 13 '21 at 17:29
  • Thanks to @Dr.Snoopy. I updated my answer. – Innat Mar 15 '21 at 04:02