Reputation: 489
I'm getting started with Keras and I think I'm missing something regarding how layers interact together.
I have this shape of data :
x shape : (696, 5, 6)
y shape : (696, 5, 2)
And a really simple model that I'm just trying to make work to keep exploring
inputShape = (xtrain.shape[1], xtrain.shape[2])
batchSize = xtrain.shape[0] / 6
outputDim = ytrain.shape[2]
model = Sequential()
model.add(Dense(500, activation='relu', input_shape=inputShape, batch_size=batchSize))
model.add(Dense(500, activation='relu'))
model.add(Dense(outputDim, activation='softmax'))
model.compile(optimizer='rmsprop', loss='mse')
And I can't figure out where is the 32
coming from in this error
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [116,5] vs. [32,5]
Upvotes: 1
Views: 605
Reputation: 11895
You almost have it, Vincent. Let me explain you what is going on:
batch_size=batchSize
to the first Dense
layer, the model is expecting all the inputs to have batch size batchSize
.However, when you fit your model via:
model.fit(xtrain, ytrain)
the error is raised, because the default value for the argument batch_size
of model.fit
is 32, which is what model.fit
uses to create the batches from xtrain
and ytrain
.
There are two possible solutions to fix this issue:
batch_size
of model.fit
to batchSize
.batch_size
(or set batch_size=None
) of the first Dense
layer. This allows to have dynamic batch sizes - the network works for all batch sizes (e.g. you can set batch_size
of model.fit
or model.predict
to an arbitrary value).Upvotes: 2