# tf.keras.Sequential().compile error

**URL:** <https://discuss.ai.google.dev/t/tf-keras-sequential-compile-error/28614>\
**Category:** General Discussion\
**Tags:** epoc, tfkeras, machine-learning, model\_maker\
**Created:** [December 29, 2023, 3:58am UTC](https://discuss.ai.google.dev/t/tf-keras-sequential-compile-error/28614 "2023-12-29T03:58:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yogesh](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/yogesh/32/16261_2.png) [@Yogesh](https://discuss.ai.google.dev/u/Yogesh)\
**Post date:** [December 29, 2023, 3:58am UTC](https://discuss.ai.google.dev/t/tf-keras-sequential-compile-error/28614/1 "2023-12-29T03:58:22Z")

</div>

Hello guys,

I was trying to run classification model on fashion mnist dataset, following book " Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 3rd Edition" . Section 10. Introduction to Artificial Neural Networks with Keras.

There they ran the code:

```auto
model.compile(loss="sparse_categorical_crossentropy",
              optimizer="sgd",
              metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=30,
                    validation_data=(X_valid, y_valid))

```

it was running sucessfully.  
I changed model.compile code to:

```auto
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              optimizer=tf.keras.optimizers.SGD(),
              metrics=[tf.keras.metrics.Accuracy()])

```

on running fit method I am getting error:  
_ValueError: Shapes (None, 1) and (None, 10) are incompatible_

What I am doing wrong here? both chatgpt and bard unable to give me the satisfactory answer. Kindly help. Thanks

---

<div class="post-metadata">

**Author:** ![astle\_dsa](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/astle_dsa/32/8628_2.png) [@astle\_dsa](https://discuss.ai.google.dev/u/astle_dsa)\
**Post date:** [December 29, 2023, 6:24am UTC](https://discuss.ai.google.dev/t/tf-keras-sequential-compile-error/28614/3 "2023-12-29T06:24:06Z")

</div>

Seems like a fault in your output layer somehow ? Can you provide the entire code ?  
Other than that, your code seems without any errors.

---

<div class="post-metadata">

**Author:** ![Yogesh](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/yogesh/32/16261_2.png) [@Yogesh](https://discuss.ai.google.dev/u/Yogesh)\
**Post date:** [December 29, 2023, 10:28am UTC](https://discuss.ai.google.dev/t/tf-keras-sequential-compile-error/28614/5 "2023-12-29T10:28:35Z")

</div>

Found the error, the line `metrics=[tf.keras.metrics.Accuracy()]` is wrong rather one should use `metrics=tf.keras.metrics.sparse_categorical_accuracy`. problem got resolved. Thanks.

---

<div class="post-metadata">

**Author:** ![Jeff\_Corpac](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/jeff_corpac/32/6955_2.png) [@Jeff\_Corpac](https://discuss.ai.google.dev/u/Jeff_Corpac)\
**Post date:** [January 1, 2024, 5:13am UTC](https://discuss.ai.google.dev/t/tf-keras-sequential-compile-error/28614/7 "2024-01-01T05:13:37Z")

</div>

You might want to check out the shape of your output layer in the model that you’re training. Since this is an MNIST fashion example, it has 10 labels, and from the shape mismatch it looks like your output layer only has 1 output.
