Keras model.predict won't accept input of size one (scalar number)
<p>I'm new to Keras and python, now I'm working on Keras to find a model of data and use that model.predict for optimization, however the model.predict can only take input as numpy array of at least 2 elements.</p>

<p>My code is</p>

<pre><code>import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np

x = np.arange(-2,3.0,0.01)
y = x**2 - 2*x + 1

model = Sequential()
model.add(Dense(50, activation='sigmoid', 
                input_dim=1, init='uniform'))
model.add(Dense(1, activation='linear'))
sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=False)
model.compile(loss='mean_squared_error', 
              optimizer='sgd',
              metrics=['accuracy'])
model.fit(x,y,nb_epoch=300, batch_size = 5,verbose = 0)
</code></pre>

<p>The code can fit fine, but if I try to use model.predict for a scalar number it gives me error</p>

<pre><code>(Pdb) model.predict(0.0)
*** Exception: Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: 0.0...
</code></pre>

<p>I force it to be numpy array but still failed, and it said the input needs to be 2 dimensions!!!</p>

<pre><code>(Pdb) model.predict(np.asarray(0.0))
*** Exception: Error when checking : expected dense_input_1 to have 2 dimensions, but got array with shape ()
</code></pre>

<p>but if I input two numbers then it gives me the answer</p>

<pre><code>(Pdb) model.predict([0.0,0.0])
array([[ 1.07415712],
       [ 1.07415712]], dtype=float32)
</code></pre>

<p>I need the model.predict to take single number as input to use for optimization. I'm not sure any setting I use wrong. Please help, thanks.</p>

<p>Try: </p>

<pre><code>model.predict(np.asarray(0.0).reshape((1,1)))
</code></pre>

<p>In Keras first dimension is always connected with example number, so it must be provided.</p>

python keras