-1
$\begingroup$

I'm doing a regular detection of numbers from photos with MNIST, but when i try to fit my model, it doesn't work, and it dispayed this message...

   import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from tensorflow.keras.datasets import mnist
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train.shape
plt.imshow(X_train[0].reshape([28,28]), cmap="gray")
plt.axis("off")

X_train = X_train / 255
X_test = X_test / 255

from tensorflow.keras.utils import to_categorical
num_classes = 10
Y_train_dummy = to_categorical(Y_train, num_classes)
Y_test_dummy = to_categorical(Y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation="relu", input_dim=X_train.shape[1]))
model.add(Dense(256, activation="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(num_classes, activation="softmax"))

model.compile(loss="categorical_crossentropy", optimize="sgd", metrics=["accuracy"])


model.fit(X_train, Y_train_dummy, epochs=20)

if someone can help i will be grateful, thanks for all

$\endgroup$
  • $\begingroup$ you reshaped x when you plot data. Could this be the issue $\endgroup$ – Peter Mar 27 at 21:56
  • $\begingroup$ no, it doesn't change anything... thanks $\endgroup$ – fiorelloccio Mar 27 at 22:31
0
$\begingroup$

You are trying to input 60000 training images with size 28 by 28 into a dense neural network. This will work since a dense neural network can only work with one dimensional data, with each input neuron of the network representing a pixel in the image. You therefore have to reshape your data first from (n_samples, height, width) to (n_samples, n_pixels) using numpy.reshape. The following code reshapes both your training and test input features, it keeps the number of samples the same and infers the second dimension from the data.

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from tensorflow.keras.datasets import mnist
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train.shape
plt.imshow(X_train[0].reshape([28,28]), cmap="gray")
plt.axis("off")

X_train = X_train / 255
X_test = X_test / 255

# Add these two lines to reshape the data
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))

from tensorflow.keras.utils import to_categorical
num_classes = 10
Y_train_dummy = to_categorical(Y_train, num_classes)
Y_test_dummy = to_categorical(Y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation="relu", input_dim=X_train.shape[1]))
model.add(Dense(256, activation="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(num_classes, activation="softmax"))

model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])


model.fit(X_train, Y_train_dummy, epochs=20)

Adding these two lines to your code (before training the model) allows me to train the network and achieve just over 98% accuracy after 10 epochs. For more info on how numpy.reshape works you can take a look at the documentation.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hey, firstly thanks for your answer... i added these two lines in my code, but the story doesn't change very much... it gave me this error: ValueError: Error when checking input: expected dense_4_input to have shape (28,) but got array with shape (784,) $\endgroup$ – fiorelloccio Mar 28 at 5:37
  • $\begingroup$ @fiorelloccio I've added the full code, which should work. $\endgroup$ – Oxbowerce Mar 28 at 10:04
0
$\begingroup$

Here is the full code

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from tensorflow.keras.datasets import mnist
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train = X_train / 255
X_test = X_test / 255
train_pixels=X_train.shape[1] * X_train.shape[2]
test_pixels=X_test.shape[1] * X_test.shape[2]
X_train=np.reshape(X_train, (X_train.shape[0],train_pixels))
X_test=np.reshape(X_test, (X_test.shape[0],test_pixels))
from tensorflow.keras.utils import to_categorical
num_classes = 10
Y_train_dummy = to_categorical(Y_train, num_classes)
Y_test_dummy = to_categorical(Y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation="relu", input_dim=train_pixels))
model.add(Dense(256, activation="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(num_classes, activation="softmax"))

model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])


model.fit(X_train, Y_train_dummy, epochs=10)


here are the results

> Train on 60000 samples
Epoch 1/10
60000/60000 [==============================] - 5s 87us/sample - loss: 0.5502 - accuracy: 0.8553
Epoch 2/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.2383 - accuracy: 0.9311
Epoch 3/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.1835 - accuracy: 0.9466
Epoch 4/10
60000/60000 [==============================] - 4s 72us/sample - loss: 0.1492 - accuracy: 0.9571
Epoch 5/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.1248 - accuracy: 0.9640
Epoch 6/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.1066 - accuracy: 0.9696
Epoch 7/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.0927 - accuracy: 0.9733
Epoch 8/10
60000/60000 [==============================] - 4s 71us/sample - loss: 0.0813 - accuracy: 0.9766
Epoch 9/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.0718 - accuracy: 0.9793
Epoch 10/10
60000/60000 [==============================] - 4s 73us/sample - loss: 0.0637 - accuracy: 0.9821
| improve this answer | |
$\endgroup$
  • $\begingroup$ I've already tried this way, but it gave me this error: ValueError: cannot reshape array of size 47040000 into shape (1680000,), by the way thank you... :( $\endgroup$ – fiorelloccio Mar 28 at 8:01
  • $\begingroup$ You also have to update input dim in NN first layer as per Gerry code. Otherwise this dimension conflict is inevitable. Better copy Gerry's full code and execute as it is in Colab. $\endgroup$ – Roshan Jha Mar 29 at 12:30
  • $\begingroup$ By the way although your training accuracy will be good you may find your accuracy on the test set would less accurate due to over fitting. I recommend adding a dropout layer after each of the first three dense layers.Documentation is at keras.io/layers/core $\endgroup$ – Gerry P Mar 30 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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