Keras shape of features for training
<p>I'm trying to train nn with keras <code>train_on_batch</code> function. I have 39 features and want a batch to contain 32 samples. So I have a list of 32 numpy arrays for every training iteration. </p>

<p>So here is my code(here every batch_x is a list of 32 numpy array each containing 39 features):</p>

<pre><code>input_shape = (39,)

model = Sequential()
model.add(Dense(39, input_shape=input_shape)) # show you is only first layer
... 

for batch_x, batch_y in train_gen:
    model.train_on_batch(batch_x, batch_y)
</code></pre>

<p>But suddenly I got an error:</p>

<pre><code>Exception: Error when checking model input: the list of Numpy arrays
that you are passing to your model is not the size the model expected.
Expected to see 1 arrays but instead got the following list of 32 arrays:
</code></pre>

<p>I'm not really sure what's wrong.</p>

<p>P.S. I also tried different <code>input_shape</code> such as (32, 39), (39, 32) etc.</p>

<p>In Keras, the <strong>output</strong> not the <strong>input</strong> dimension is the first arg. The <a href="http://keras.io" rel="nofollow">Keras docs</a> front-page example is pretty clear:</p>

<pre><code>model.add(Dense(output_dim=64, input_dim=100))
</code></pre>

<p>Adjusting that example to match what I guess are your requirements:</p>

<pre><code>model.add(Dense(output_dim=39, input_dim=39))
</code></pre>

<p>In your code, the first positional arg in your <code>Dense</code> layer is <code>39</code> which sets the <strong>output</strong> to be 39-D, not the input, as you probably assumed. You said that you had 39 input features. That first layer (in my attempt to duplicate what you were intending) doesn't do any compression or feature extraction from your 39-dimension input feature vectors.</p>

<p>Why don't you just set the dimensions of your input and output arrays for each layer (as in the example) and leave the input_<strong>shape</strong> alone? Just reshape your inputs (and labels) to match the default assumptions? Also, you might try running the basic <code>fit</code> method on your input data set (or some portion of it) before moving on to more complicated arrangements, like manually training in batches as you've done.</p>

<p>Here's an example for a toy problem with your feature dimension:</p>

<pre><code>from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.regularizers import l1l2

X = np.random.randn(1000, 39)
y = np.array([X[i,7:13].sum() for i in range(X.shape[0])])

nn = Sequential()
nn.add(Dense(output_dim=1, input_dim=39))
nn.compile('sgd', 'mse')
nn.fit(X, y, nb_epoch=10)
</code></pre>

<p>Which gives:</p>

<pre><code>Epoch 1/10
1000/1000 [==============================] - 0s - loss: 4.6266      
...    
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 1.4048e-04
</code></pre>

python python-2.7 machine-learning keras training-data