卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Networks, CNN)是一類特別適用于處理圖像數(shù)據(jù)的深度學(xué)習(xí)模型。在Python中,我們可以使用流行的深度學(xué)習(xí)庫(kù)TensorFlow和Keras來(lái)創(chuàng)建和訓(xùn)練一個(gè)CNN模型。在本文中,我們將介紹如何使用Keras創(chuàng)建一個(gè)簡(jiǎn)單的CNN模型,并用它對(duì)手寫數(shù)字進(jìn)行分類。
1. 準(zhǔn)備數(shù)據(jù)集
我們將使用MNIST數(shù)據(jù)集,這是一個(gè)常用的手寫數(shù)字?jǐn)?shù)據(jù)集。Keras庫(kù)提供了一個(gè)方便的函數(shù)來(lái)加載MNIST數(shù)據(jù)集。數(shù)據(jù)集包含60000個(gè)訓(xùn)練樣本和10000個(gè)測(cè)試樣本,每個(gè)樣本是一個(gè)28x28的灰度圖像。
python
復(fù)制代碼
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
接下來(lái),我們需要對(duì)數(shù)據(jù)進(jìn)行預(yù)處理。我們將圖像數(shù)據(jù)歸一化到0-1之間,并將標(biāo)簽數(shù)據(jù)進(jìn)行one-hot編碼:
python
復(fù)制代碼
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype("float32") / 255
from tensorflow.keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
2. 創(chuàng)建CNN模型
我們將使用Keras創(chuàng)建一個(gè)簡(jiǎn)單的CNN模型,包括卷積層、池化層、全連接層等。模型的結(jié)構(gòu)如下:
- 卷積層:使用32個(gè)3x3的卷積核,激活函數(shù)為ReLU;
- 池化層:使用2x2的最大池化;
- 卷積層:使用64個(gè)3x3的卷積核,激活函數(shù)為ReLU;
- 池化層:使用2x2的最大池化;
- 全連接層:包含128個(gè)神經(jīng)元,激活函數(shù)為ReLU;
- 輸出層:包含10個(gè)神經(jīng)元,激活函數(shù)為softmax。
python
復(fù)制代碼
from tensorflow.keras import layers
from tensorflow.keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dense(10, activation="softmax"))
3. 訓(xùn)練CNN模型
我們將使用訓(xùn)練數(shù)據(jù)集訓(xùn)練CNN模型,并在測(cè)試數(shù)據(jù)集上評(píng)估模型性能。我們將使用交叉熵?fù)p失函數(shù)和Adam優(yōu)化器,訓(xùn)練10個(gè)epoch。
python
復(fù)制代碼
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=10, batch_size=64)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print("Test accuracy: {:.2f}%".format(test_acc * 100))
4. 使用CNN模型進(jìn)行預(yù)測(cè)
訓(xùn)練好CNN模型后,我們可以用它對(duì)新的圖像數(shù)據(jù)進(jìn)行預(yù)測(cè)。下面我們將隨機(jī)選擇一個(gè)測(cè)試圖像,并使用模型進(jìn)行預(yù)測(cè)。
python
復(fù)制代碼
import numpy as np
import matplotlib.pyplot as plt
index = np.random.randint(0, len(test_images))
image = test_images[index]
plt.imshow(image.reshape(28, 28), cmap="gray")
plt.show()
predictions = model.predict(np.expand_dims(image, axis=0))
predicted_label = np.argmax(predictions)
print("Predicted label:", predicted_label)
上述代碼將展示一個(gè)隨機(jī)選擇的手寫數(shù)字圖像,并輸出模型預(yù)測(cè)的結(jié)果。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-812638.html
這就是如何在Python中使用Keras創(chuàng)建和訓(xùn)練一個(gè)簡(jiǎn)單的CNN模型進(jìn)行手寫數(shù)字分類。在實(shí)際應(yīng)用中,可以根據(jù)需求調(diào)整CNN模型的結(jié)構(gòu)和參數(shù)以優(yōu)化性能。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-812638.html
到了這里,關(guān)于Python中的卷積神經(jīng)網(wǎng)絡(luò)(CNN)入門的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!