python-kerasHow can I use Python and Keras to build a model for the MNIST dataset?
- First, import the necessary libraries for the task:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
- Then, load the MNIST dataset from Keras:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
- Next, reshape the data to fit the model:
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
- Then, normalize the data:
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
- After that, one-hot encode the labels:
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
- Finally, build and compile the model:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])
- Finally, fit the model:
model.fit(x_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(x_test, y_test))
Output example
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 15s 252us/step - loss: 0.2796 - accuracy: 0.9187 - val_loss: 0.1162 - val_accuracy: 0.9648
Epoch 2/10
60000/60000 [==============================] - 5s 81us/step - loss: 0.0911 - accuracy: 0.9737 - val_loss: 0.0791 - val_accuracy: 0.9761
...
This is an example of how to use Python and Keras to build a model for the MNIST dataset. The code is composed of the following parts:
- Import the necessary libraries for the task:
keras
,mnist
,Sequential
,Dense
,Dropout
,Flatten
,Conv2D
,MaxPooling2D
andK
. - Load the MNIST dataset from Keras.
- Reshape the data to fit the model.
- Normalize the data.
- One-hot encode the labels.
- Build and compile the model.
- Fit the model.
Helpful links
More of Python Keras
- How do I use zero padding in Python Keras?
- How can I use Python and Keras to create a Variational Autoencoder (VAE)?
- How do I use Python and Keras to create a VGG16 model?
- How do I get the version of Keras I am using in Python?
- How do I use TensorFlow, Python, Keras, and utils to_categorical?
- How do I write a Python Keras example code?
- How can I use Python Keras to create a neural network with zero hidden layers?
- How do I use Python Keras to zip a file?
- How can I use YOLO with Python and Keras?
- How can I use word2vec and Keras to develop a machine learning model in Python?
See more codes...