Reputation: 6039
I am trying to do a simple hello world with Keras and stuck. At the beginning I had 1 layer with 1 input and 1 output and it worked pretty well for a straight line approximation ;)
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import RMSprop
from keras.losses import mean_squared_error
mo = Sequential()
d = Dense(1, input_shape=(1,))
mo.add(d)
mo.summary()
mo.compile(loss=mean_squared_error, optimizer=RMSprop(lr=0.4), metrics=['accuracy'])
mo.trainable = True
for i in range(-100, 100):
mo.train_on_batch(x = [i], y = [i])
After that I've got bravery for 2 input parameters:
d = Dense(1, input_shape=(2,))
for i in range(-100, 100):
mo.train_on_batch(x = [np.array([i,i])], y = [i])
np.array([1,1]).shape # gives (2,)
Though I am getting an exception:
ValueError: Error when checking input: expected dense_53_input to have shape (2,) but got array with shape (1,)
I tried various combinations like [[i],[i]]
.
Upvotes: 0
Views: 1014
Reputation: 33410
The first dimension is always the batch dimension in Keras. Batch size refers to the number of samples processed in a pass (forward and backward). When you specify the input_shape
argument it does not include the batch dimension. Therefore, a network with input shape of (2,)
takes input data of shape (?,2)
where ?
refers to the batch size. So you must pass arrays of shape (?,2)
:
mo.train_on_batch(x=[np.array([[i,i]])], y=[i])
since:
np.array([[i,i]]).shape # it is (1,2)
Upvotes: 1