0

My model definition is:

inputs = keras.Input(shape=(28,28))
dense = keras.layers.Dense(64, activation="relu")
x = dense(inputs)
x = keras.layers.Dense(64, activation="relu")(x)
outputs = keras.layers.Dense(10)(x)
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")

And I will train the network with:

model.compile(optimizer="sgd", loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=10, epochs=30, verbose=2)

which x_train's shape is (55000, 28, 28) and y_train's shape is (55000,) and I get this error:

ValueError: Shape mismatch: The shape of labels (received (10, 1)) should equal the shape of logits except for the last dimension (received (10, 28, 10))
2
  • 2
    you miss a Flatten layer in your network to pass from 3D to 2D Commented Dec 27, 2020 at 23:43
  • 55000 is the number of samples not data shape Commented Dec 28, 2020 at 9:25

1 Answer 1

1

What @Marco Cerliani said means you should change your code to

inputs = keras.Input(shape=(28,28))
output = keras.layers.Flatten()(inputs)
output = keras.layers.Dense(64, activation="relu")(output)
output = keras.layers.Dense(64, activation="relu")(output)
outputs = keras.layers.Dense(10)(output)
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")

Please read the tensorflow tutorials and guide if you're a beginner . You will find the answer of your question if you read this .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.