Keras, shapes are incompatible

Viewed 678

I made a keras classification model, and I have inputs with different lengths, so I'm using train_on_batch. And I'm getting a ValueError: Shapes (1, 5) and (1, 36329, 5) are incompatible .

Each input is a set of 2D points.

X_train.shape >>> (2680,) X_train[0].shape >>> (36329, 2) X_train[5].shape >>> (40233, 2)

For the output shape :

y_train.shape >>> (2680, 5)
# y_train[0] >>> array([0, 0, 0, 1, 0])

The full code:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(array, classes_bi, test_size=0.33, random_state=69)
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(10000, activation='LeakyReLU'))
model.add(Dense(1000, activation='LeakyReLU'))
model.add(Dense(100, activation='LeakyReLU'))
model.add(Dense(5, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
epochs=5
for epoch in range(5):
    for diag,output in zip(X_train,y_train):
        diag = np.expand_dims(diag,axis=0) #add the batch size = 1
        output = np.expand_dims(output,axis=0) ##add batch size = 1
        #print(diag.shape) >>> (1, 36329, 2)
        #print(output.shape) >>> (1, 5)
        model.train_on_batch(diag,output)

Error :

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9248/1106303834.py in <module>
      6         print(diag.shape)
      7         print(output.shape)
----> 8         model.train_on_batch(diag,output)
...

ValueError: in user code:

...
   
ValueError: Shapes (1, 5) and (1, 36329, 5) are incompatible

I tried to expand the dimsention of output twice to get a shape of (1, 1, 5) with (1, 36329, 5) but it didnt work.

1 Answers

For the model you are building, the dimensions of your training data needs to be constant - it cannot vary from one training example to the other.

When you create a model with Sequential(), the input shape of your model will be defined when you do the training for the first time by calling model.fit or model.train_on_batch.

For example, if your first training batch has dimension (36329, 2), your model will assume that each of your training examples have the dimension (2, ) and this particular batch has 36329 training examples - so you need 36329 labels.

The batch size can change from batch to batch, but the dimension (2, ) needs to maintain.

This might be your case:

I think your problem is because your batch of 2D points contain thousands of examples but only 1 label per batch.

If each of the 36329 training examples in X_train[0] correspond to the same label y_train[0] >>> array([0, 0, 0, 1, 0]), all you need to do is broadcast the label so it has the same number of training examples as the input.

for epoch in range(5):
    for diag, output in zip(X_train,y_train):
        training_examples = diag.shape[0]
        broadcast_arr = np.ones((training_examples, 1))
        output = output * broadcast_arr
        model.train_on_batch(diag,output)

PS: I wanted to ask a couple of questions by commenting, but I don't have enough reputation to do so, that's why I'm posting this answer as my best understanding of your question.

Related