Home > Technology peripherals > AI > body text

Handwritten digit recognition using convolutional neural networks

PHPz
Release: 2024-01-23 21:03:22
forward
1083 people have browsed it

Handwritten digit recognition using convolutional neural networks

The MNIST data set is composed of handwritten digits and includes 60,000 training samples and 10,000 testing samples. Each sample is a 28x28 pixel grayscale image representing a number from 0 to 9.

Convolutional neural network (CNN) is a model used for image classification in deep learning. It extracts image features through convolutional layers and pooling layers, and uses fully connected layers for classification.

Below I will introduce how to use Python and TensorFlow to implement a simple CNN model to classify the MNIST data set.

First, we need to import the necessary libraries and MNIST dataset:

import tensorflow as tf
from tensorflow.keras.datasets import mnist

# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Copy after login

Next, we need to normalize the image data and convert the label data into unique Hot encoding format:

# 归一化图像数据
x_train = x_train / 255.0
x_test = x_test / 255.0

# 将标签数据转换为独热编码格式
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
Copy after login

Then, we define the CNN model. This model includes two convolutional layers and two pooling layers, as well as a fully connected layer. We use the ReLU activation function and the Softmax activation function in the last layer for classification. The code is as follows:

model = tf.keras.models.Sequential([
    # 第一个卷积层
    tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    # 第二个卷积层
    tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    # 将特征图展平
    tf.keras.layers.Flatten(),
    # 全连接层
    tf.keras.layers.Dense(units=128, activation='relu'),
    # 输出层
    tf.keras.layers.Dense(units=10, activation='softmax')
])
Copy after login

Next, we need to compile the model and specify the loss function, optimizer and evaluation metrics:

# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Copy after login

Finally, we train the model and test it:

# 训练模型
model.fit(x_train.reshape(-1, 28, 28, 1), y_train, epochs=5, batch_size=32)

# 测试模型
score = model.evaluate(x_test.reshape(-1, 28, 28, 1), y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Copy after login

After running the complete code, we can see that the model's test accuracy is approximately 99%.

To summarize, the steps to use a convolutional neural network to classify the MNIST dataset are as follows:

1. Load the MNIST dataset and proceed Preprocessing, including normalization and one-hot encoding;

2. Define the CNN model, including convolutional layer, pooling layer and fully connected layer, and specify the activation function;

3. Compile the model, specify the loss function, optimizer and evaluation indicators;

4. Train the model and test it on the test set.

The above is a simple example that can be modified and optimized according to specific circumstances.

The above is the detailed content of Handwritten digit recognition using convolutional neural networks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:163.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template