None
means this dimension is variable.
The first dimension in a keras model is always the batch size. You don't need fixed batch sizes, unless in very specific cases (for instance, when working with stateful=True
LSTM layers).
That's why this dimension is often ignored when you define your model. For instance, when you define input_shape=(100,200)
, actually you're ignoring the batch size and defining the shape of "each sample". Internally the shape will be (None, 100, 200)
, allowing a variable batch size, each sample in the batch having the shape (100,200)
.
The batch size will be then automatically defined in the fit
or predict
methods.
Other None
dimensions:
Not only the batch dimension can be None
, but many others as well.
For instance, in a 2D convolutional network, where the expected input is (batchSize, height, width, channels)
, you can have shapes like (None, None, None, 3)
, allowing variable image sizes.
In recurrent networks and in 1D convolutions, you can also make the length/timesteps
dimension variable, with shapes like (None, None, featuresOrChannels)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…