0
$\begingroup$

I am trying to feed a Sequential model in batches. To be reproducible my example, suppose my data is:

X=np.random.rand(24,432)
Y=np.random.rand(24,432)

My goal is to feed the model in batches. 24 points at a time (24 x 1 vector), 432 times.


I built my model as:

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=12)

model = keras.Sequential([
      keras.layers.Flatten(input_shape=(1,)),
      keras.layers.Dense(64, activation=tf.nn.relu),
      keras.layers.Dense(32, activation=tf.nn.relu),
      keras.layers.Dropout(0.25),
      keras.layers.Dense(2, activation=tf.nn.sigmoid),
  ])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])



#train_generator = batch_generator(X_train, y_train, 12)
history=model.fit(X_train, y_train, epochs=200, batch_size=12, validation_split=0.3)

early_stopping_monitor = EarlyStopping(patience=100)
history=model.fit_generator(train_generator,epochs=20)
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Model loss:',test_loss, 'Model accuracy: ',test_acc)

However, I get this error:

WARNING:tensorflow:Model was constructed with shape (None, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name='flatten_24_input'), name='flatten_24_input', description="created by layer 'flatten_24_input'"), but it was called on an input with incompatible shape (None, 432)

EDIT:

X=np.array(X[0:10368]).reshape(24,432)
Y=np.array(Y[0:10368]).reshape(24,432)

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=12)

model = keras.Sequential([
      keras.layers.Flatten(batch_input_shape=(None, 432, 2)),
      keras.layers.Dense(64, activation=tf.nn.relu),

      keras.layers.Dense(2, activation=tf.nn.sigmoid),
  ])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])


history=model.fit(X_train, y_train, epochs=200, batch_size=32, validation_split=0.3)
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Model loss:',test_loss, 'Model accuracy: ',test_acc)

NOW RAISES:

ValueError: Input 0 of layer dense_17 is incompatible with the layer: expected axis -1 of input shape to have value 864 but received input with shape (None, 432)
$\endgroup$

1 Answer 1

0
$\begingroup$

The main issue here is that your labels - Y values you initialize randomly at the beginning is 432 dimensional, while your final layer is 2 dimensional and you are using binary cross-entropy. Which means you are trying to predict a 432 dimensional vector as a binary classification.

But that is not reason you are receiving an error message for. To fix the error, you can use batch_input_shape(None, 432, 1) instead of input_shape(1, ). Once you fix this, you will get a value error:

ValueError: logits and labels must have the same shape ((None, 2) vs (None, 432))

Which in turn fixed in the way describe above.

$\endgroup$
1
  • $\begingroup$ Hi, thanks for your reply and your explanation. After fixing what you said, a new value error is raised relative to the shape of my data again. See the EDIT above please. $\endgroup$ Oct 15, 2021 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.