Home  >  Article  >  Backend Development  >  Convolutional neural network algorithm example in Python

Convolutional neural network algorithm example in Python

王林
王林Original
2023-06-11 12:37:371485browse

Convolutional Neural Network (CNN) is a neural network algorithm widely used in image recognition, speech recognition, natural language processing and other fields. It draws on the structure of biological neurons, processes the two-dimensional spatial structure of the input data, and adopts a weight sharing method in the convolution layer and pooling layer, which greatly reduces the number of parameters that need to be optimized, thereby improving Generalization ability and efficiency of the model.

Python is a programming language widely used in the fields of scientific computing, machine learning and deep learning. It has the advantages of being simple and easy to use, open source and free, and rich in third-party libraries. It is used by more and more researchers. and engineers' choice as a development tool. In Python, we can use a variety of deep learning frameworks to implement convolutional neural network algorithms, including Keras, TensorFlow, PyTorch, etc.

Below, we will use an example to introduce how to use the Keras framework to implement the convolutional neural network algorithm in Python.

Dataset Introduction

This example uses the CIFAR-10 data set, which contains 60,000 32x32 color images in 10 categories, and the number of images in each category is 6,000. These images are divided into a training set and a test set, with 50,000 images in the training set and 10,000 images in the test set.

In Python, we can use the cifar10.load_data function provided by the Keras library to load the CIFAR-10 data set, as shown below:

from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

Among them, x_train and x_test are the training set and x_test respectively. Image data of the test set, y_train and y_test are the labels of the training set and test set respectively.

Model construction

The convolutional neural network model used in this example contains multiple convolution layers, pooling layers and fully connected layers. The specific structure is as follows:

  1. Input layer: The size of the input image is 32x32x3, where 3 represents the three RGB channels.
  2. Convolutional layer 1: Use 32 convolution kernels of size 3x3, the stride is 1, and the activation function is ReLU.
  3. Convolutional layer 2: Use 64 convolution kernels of size 3x3, the stride is 1, and the activation function is ReLU.
  4. Pooling layer 1: Use a pooling kernel with a size of 2x2 and a stride of 2.
  5. Convolutional layer 3: Use 128 convolution kernels of size 3x3, the stride is 1, and the activation function is ReLU.
  6. Pooling layer 2: Use a pooling kernel with a size of 2x2 and a stride of 2.
  7. Fully connected layer 1: contains 128 neurons, and the activation function is ReLU.
  8. Fully connected layer 2: contains 10 neurons, corresponding to 10 categories, and the activation function is Softmax.

In Python, we can build the model layer by layer through the Sequential class provided by the Keras library, as shown below:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=(32, 32, 3)))
model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=10, activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Among them, the Conv2D class represents the convolution layer and the MaxPooling2D class Represents the pooling layer, the Flatten class represents the one-dimensional multidimensional input, and the Dense class represents the fully connected layer. The model.compile function is used to compile the model and specify the optimization algorithm, loss function, evaluation index, etc.

Model training and evaluation

After the model is built, we can use the training set data to train the model. In Python, we can use the fit function to implement model training, as shown below:

from keras.utils import np_utils

y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)

model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))

Among them, the np_utils.to_categorical function is used to convert labels to one-hot encoding. The fit function is used to train the model, specifying the number of training rounds, batch size, validation set data, etc. After training is completed, we can use the evaluate function to evaluate the model, as shown below:

loss, accuracy = model.evaluate(x_test, y_test)

In this example, after 10 rounds of training, the accuracy of the model on the test set is 0.7318.

Summary

This article introduces an example of using the Keras framework to implement the convolutional neural network algorithm in Python. By using the CIFAR-10 dataset and specific model structures, we can learn how to build, train, and evaluate convolutional neural networks in Python. In addition, the performance of the model can be further improved by adjusting the model structure and parameters.

The above is the detailed content of Convolutional neural network algorithm example in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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