0

I've tried define a simple architecture to use on MNIST dataset , I start defined my architecture like this :

model = Sequential()

model.add(Convolution2D(32,(3,3),activation='relu',input_shape = (1,28,28)));

in this step I checked my network out put :

model.output_shape

it gives me this :

(None, -1, 26, 32)

could someone explain to me what is the meaning of negative dimension (-1) ?

Javad Sameri
  • 1,218
  • 3
  • 17
  • 30
  • 1
    It means unknown dimension. It is unknown in the current context, but it will be inferred or learnt in the running such as after providing batch size – orabis May 18 '17 at 16:03

1 Answers1

2

Although a -1 can actually be an unknown size when working directly with tensors, Keras layers don't work like that. The unknown batch size in Keras is the None dimension.

For convolutions, Keras uses channels_last as data format, so you should shape your data as (28,28,1), which is (imageSide1, imageSide2, channels)

By shaping your data as (1,28,28), the convolution will think the first image side is 1 pixel. And removing the 2 pixels by the operation, it will result in -1. So, shape it as (28,28,1) to get an output of (None,26,26,32).

Alternatively, you can set the data_format parameter in the convolutional layer to channels_first, or even change the keras.json file to have channels_first as default.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • how did you learn this stuff :D ? could you introduce some useful resource for me to carry on ? – Javad Sameri May 19 '17 at 07:27
  • 1
    Had to call "Daniel.fit(trainingWithKeras,expectedResults,epochs=a lot)". I find it hard to come up with good, short and useful tutorials. All of them talking too much and explaining more theory than practical usage. Some resources: [Keras documentation](https://keras.io/) is very very useful if you're not starting from zero. For understanding dimensions, [I wrote this answer](http://stackoverflow.com/questions/44012309/keras-layers-toturial-and-samples/44014134#44014134). – Daniel Möller May 19 '17 at 12:16