# Keras input dimension bug?

Keras has a problem with the input dimension. My first layer looks like this:

model.add(Dense(128, batch_size=1, input_shape=(150,), kernel_initializer="he_uniform", kernel_regularizer=regularizers.l2(0.01), activation="elu"))


As you can see the input dimension should be (150,) and with the fixed batch_size it is (1, 150)

My data has dimension (150,) and could be for example a numpy array with 150 zeros.

old_qval = model.predict(old_state_m)


Here I call the model to make a prediction. Normally Keras should automatically add the batch size as an extra dimension so I should end up with (1, 150) which would work. But Keras adds the dimension for the batch size at the wrong place and I end up with (150, 1). I tried tensorflow and theano backend.

Do I have a bug in my code or is it a problem with Keras?

How can I fix the problem? I could reshape my input data but it already has the needed shape of (150,) and should be fine. What else could I do?

If I should provide more data or code feel free to ask.

• When you do model.predict(X) the first axis in X is always an index in a batch. So if you want to predict on one sample, do something like X = np.expand_dims(X, axis=0) – Mikhail Yurasov Aug 2 '17 at 3:52
• @MikhailYurasov thank you so much. My mistake was I thought keras would automatically add this extra dimension and then I got confused cause it was in the wrong order. Nevertheless I really appreciate your fast help. Since it is a comment I cant mark it as the right answers sorry for that. – Ilovescience Aug 2 '17 at 9:43
• It is generally a bad idea to put messages like "moderators please don't close my question" on a Stack Exchange site, so I have edited that out. If your question was bad in some way, this should make absolutely no difference to mod behaviour - no-one is making value judgements about your need to get an answer, stuff gets closed based on qualities of the question, not you personally. In your case there doesn't seem to be problem to me, the question is a practical concern with a library used for data science, and that topic is one of those central to this site. – Neil Slater Aug 2 '17 at 11:38
• @NeilSlater thank you very much for your tip and help. In the future I wont write anything like "mods please dont close this question" in my question. Thinking about it you are totally right and it is stupid and doesnt add any value. I am sorry for my mistake, it wont happen again. – Ilovescience Aug 3 '17 at 11:28

When you do model.predict(X) the first axis in X is always an index in a batch. So if you want to predict on one sample, do something like X = np.expand_dims(X, axis=0)
old_qval = model.predict( np.expand_dims(old_state_m, axis=0) )