Home > Article > Backend Development > Convolutional neural network algorithm example in Python
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:
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!