Search code examples
machine-learningkerasdimensionskeras-layer

Error in Input Dimensions - Keras


I'm training a neural net with keras with input data that has a shape of (116, 2, 3, 58) and output data that has a shape of (116, 2). I'm getting this error:

ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (116, 2)

What could I be doing wrong? Here is my code:

trainingInput = np.load("trainingInput.npy")
trainingOutput = np.load("trainingOutput.npy")

inp = Input(batch_shape=(116, 2, 3, 58))
d1 = Dense(16, activation='relu')(inp)
d2 = Dense(32, activation='relu')(d1)
out = Dense(2, activation='softmax')(d2)

model = Model(inputs=inp, outputs=out)
lrSet = SGD(lr=0.01)

model.compile(loss='categorical_crossentropy', optimizer=lrSet, metrics=['accuracy'])
model.fit(trainingInput, trainingOutput, batch_size=16, epochs=50, verbose=1, validation_split=0.1)

Solution

  • Dense is a full-connected layer. It will not change the shape of Input. If you want use Dense, you should resize (116,2,3,68) -> (116,2*3*68)