I am currently testing the typical mnist code using Keras. The code seems to be working well, but the fitting log does not show the appropriate number of training data, i.e., 53 must be 60000. What is wrong with this code? I summarized the basic environment.
Thank you.
Below is the code
import osos.environ["CUDA_VISIBLE_DEVICES"] = "-1"os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"os.environ['TF_CPP_MIN_LOG_LEVEL']='1'import numpy as npfrom tensorflow import keras, configfrom tensorflow.keras import layersgpus = config.list_physical_devices(device_type = 'GPU')if len(gpus)>0: print(f">> GPU detected. {gpus[0].name}") config.experimental.set_memory_growth(gpus[0], True)else: print(">> GPU not detected.")# https://keras.io/examples/vision/mnist_convnet/# Model / data parametersnum_classes = 10input_shape = (28, 28, 1)# the data, split between train and test sets(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()# Scale images to the [0, 1] rangex_train = x_train.astype("float32") / 255x_test = x_test.astype("float32") / 255# Make sure images have shape (28, 28, 1)x_train = np.expand_dims(x_train, -1)x_test = np.expand_dims(x_test, -1)print("x_train shape:", x_train.shape)print(x_train.shape[0], "train samples")print(x_test.shape[0], "test samples")# convert class vectors to binary class matricesy_train = keras.utils.to_categorical(y_train, num_classes)y_test = keras.utils.to_categorical(y_test, num_classes)model = keras.Sequential( [ keras.Input(shape=input_shape), layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dropout(0.5), layers.Dense(num_classes, activation="softmax"), ])model.summary()batch_size = 1024epochs = 5model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])print(x_train.shape)print(y_train.shape)model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)score = model.evaluate(x_test, y_test, verbose=0)print("Test loss:", score[0])print("Test accuracy:", score[1])
Below is the output
>> GPU not detected.x_train shape: (60000, 28, 28, 1)60000 train samples10000 test samplesModel: "sequential"_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 26, 26, 32) 320 max_pooling2d (MaxPooling2D (None, 13, 13, 32) 0 ) conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 max_pooling2d_1 (MaxPooling (None, 5, 5, 64) 0 2D) flatten (Flatten) (None, 1600) 0 dropout (Dropout) (None, 1600) 0 dense (Dense) (None, 10) 16010 =================================================================Total params: 34,826Trainable params: 34,826Non-trainable params: 0_________________________________________________________________(60000, 28, 28, 1)(60000, 10)Epoch 1/553/53 [==============================] - 4s 70ms/step - loss: 1.1010 - accuracy: 0.6703 - val_loss: 0.2543 - val_accuracy: 0.9302Epoch 2/553/53 [==============================] - 4s 67ms/step - loss: 0.2943 - accuracy: 0.9111 - val_loss: 0.1349 - val_accuracy: 0.9638Epoch 3/553/53 [==============================] - 4s 67ms/step - loss: 0.1864 - accuracy: 0.9435 - val_loss: 0.0968 - val_accuracy: 0.9745Epoch 4/553/53 [==============================] - 4s 67ms/step - loss: 0.1463 - accuracy: 0.9554 - val_loss: 0.0784 - val_accuracy: 0.9775Epoch 5/553/53 [==============================] - 3s 66ms/step - loss: 0.1247 - accuracy: 0.9617 - val_loss: 0.0688 - val_accuracy: 0.9810Test loss: 0.07176525145769119Test accuracy: 0.9778000116348267
My environment is summarized below:
Ubuntu version: 20.04.4 LTS
Python version: Python 3.9.18
Keras (Tensorflow) version: 2.8.2
Conda version: 24.1.2
I think you can test the code by just copy and paste, and I would like to know the result in your environment.
Thank you.