Issue
I'm following along with the Tensorflow for Beginners tutorial:
And we come to our first line of code:
Build a machine learning model
Build a tf.keras.Sequential model:
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10) ])
And i'm confused by the first line:
tf.keras.layers.Flatten(input_shape=(28, 28))
Specifically about the input_shape
argument.
Why: Because i thought the Flatten
operation takes the previous layer and turns it into a 1-d vector; so what is the input_shape
argument mean?
So we consult the documentation
The documentation on Flatten()
: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Flatten
says:
Flattens the input. Does not affect the batch size.
And specifically i'm interested in the input_shape
argument:
Args
data_format
A string, one ofchannels_last
(default) orchannels_first
. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch, ..., channels
) while channels_first corresponds to inputs with shape (batch, channels, ...
). When unspecified, usesimage_data_format
value found in your Keras config file at~/.keras/keras.json
(if exists) else 'channels_last'. Defaults to 'channels_last'.
There is no argument called input_shape
.
So what does the argument mean?
Short Version
- If i were writing TF code from the documentation, how would I know that
Flatten
takes aninput_shape
argument? - Why is the
input_shape
argument omitted from the documention?
In other words: there must be some basic knowledge about TensorFlow that the documentation assumes you have, but does not document itself.
Can someone help me understand this undocumented behavior, and why it is undocumented?
Research Effort
In addition to consulting the documentation, we have:
- Keras input explanation: input_shape, units, batch_size, dim, etc (isn't my question, nor answers my question)
Solution
input_shape
is an optional argument that can be added to the first layer (Flatten
or not) of a Keras model. Keras uses its value to create an InputLayer
implicitly.
You model is equivalent to this:
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=(28, 28)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
To quote the documentation:
When using
InputLayer
with the Keras Sequential model, it can be skipped by moving theinput_shape
parameter to the first layer after theInputLayer
.
Answered By - Jafar Isbarov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.