6
$\begingroup$

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.

$\endgroup$
4
  • 2
    $\begingroup$ 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) $\endgroup$ Aug 2, 2017 at 3:52
  • $\begingroup$ @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. $\endgroup$ Aug 2, 2017 at 9:43
  • $\begingroup$ 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. $\endgroup$ Aug 2, 2017 at 11:38
  • $\begingroup$ @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. $\endgroup$ Aug 3, 2017 at 11:28

1 Answer 1

2
$\begingroup$

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)

In your case, this should work:

old_qval = model.predict( np.expand_dims(old_state_m, axis=0) )
$\endgroup$
2
  • $\begingroup$ I have made this a community Wiki, since it is just a copy of Mikhail Yurasov's comment. $\endgroup$ Aug 2, 2017 at 11:40
  • $\begingroup$ Thank you and very nice of you not to steal others credit props for you $\endgroup$ Aug 2, 2017 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.