问题
I currently have a keras model that looks like this:
model = keras.Sequential()
model.add(keras.layers.Dense(100, activation=tf.nn.relu))
model.add(keras.layers.Dense(100, activation=tf.nn.relu))
model.add(keras.layers.Dense(len(labels), activation=tf.nn.softmax))
The Keras documentation tells me:
The model needs to know what input shape it should expect. For this reason, the first layer in a Sequential model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape
However, the model as it is actually trains fine, without errors, even though I never specified the shape of the inputs.
How does it know what shape to expect? What is the default behaviour if I don't provide an input shape? How will it affect my model?
edit: this is using tf.keras
, aka the Tensorflow backend for keras
回答1:
Nice observation - I believe the Keras documentation should be updated. When the input shape is not provided, Keras infers it from the argument x
of Model.fit
and only then it builds the whole model. Concretely, this is what's happening:
- When adding Keras layers in the
Sequential
model, since the argumentinput_shape
(and, by extension,batch_input_shape
) is never set, the attributeModel.inputs
remainsNone
(see Sequential.add). - Then, in
Model.fit
, they check whetherModel.inputs
has been set (see Model.fit and Model._standardize_user_data) and, when it hasn't, they infer the input shape from the provided input array. - Finally, in
Model._set_inputs
, they build the whole model with the inferredinput_shape
(see Model._set_inputs).
This can be verified by printing some weights (e.g. print(model.layers[0].get_weights())
) before fitting the model. You will see that, when the argument input_shape
or batch_input_shape
is not provided to the first layer of the model, the weight's array is empty as the model is yet to be built.
回答2:
There appears to be a miscorrelation between the TensorFlow version and the Keras documentation.
As far as I can see, the documentation that you are referring to is the one from Keras, while the TensorFlow version that you use is 2.0.0 >= alpha version, whose Keras internal version is different.
You should check the Keras documentation within TensorFlow 2.0, otherwise you might very well run into mismatches.
来源:https://stackoverflow.com/questions/57871463/keras-sequential-without-providing-input-shape