日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 卷积神经网络 >内容正文

卷积神经网络

cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念

發(fā)布時間:2023/12/15 卷积神经网络 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

cnn卷積神經(jīng)網(wǎng)絡應用

In this tutorial, we’ll work through the core concepts of convolutional neural networks (CNNs). To do this, we’ll use a common dataset — the MNIST dataset—and a standard deep learning task—image classification

在本教程中,我們將研究卷積神經(jīng)網(wǎng)絡(CNN)的核心概念。 為此,我們將使用一個通用的數(shù)據(jù)集-MNIST數(shù)據(jù)集-和一個標準的深度學習任務-圖像分類

The goal here is to walk through an example that will illustrate the processes involved in building a convolutional neural network. The skills you will learn here can easily be transferred to a separate dataset.

這里的目標是通過一個示例來說明構建卷積神經(jīng)網(wǎng)絡所涉及的過程。 您將在此處學習的技能可以輕松地轉移到單獨的數(shù)據(jù)集中。

So let’s go deeper…pun intended.

因此,讓我們更深入……雙關語。

建立 (Setup)

In this step, we need to import Keras and other packages that we’re going to use in building our CNN. Import the following packages:

在這一步中,我們需要導入Keras和其他將用于構建CNN的軟件包。 導入以下軟件包:

  • Sequential is used to initialize the neural network.

    順序用于初始化神經(jīng)網(wǎng)絡。

  • Conv2D is used to make the convolutional network work with images.

    Conv2D用于使卷積網(wǎng)絡處理圖像。

  • MaxPooling2D is used to add the pooling layers.

    MaxPooling2D用于添加池化層。

  • Flatten is the function that converts the pooled feature map to a single column, which is then passed to the fully connected layer.

    Flatten是將合并的要素映射轉換為單列,然后將其傳遞到完全連接的層的功能。

  • Dense adds the fully connected layer to the neural network.

    密集將完全連接的層添加到神經(jīng)網(wǎng)絡。

  • Reshape is used to change the shape of the input array

    重塑用于 改變輸入數(shù)組的形狀

import tensorflow as tffrom tensorflow.keras import Sequentialfrom tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,Reshape

導入數(shù)據(jù)集 (Import the Dataset)

Next, we’ll import the mnist dataset from Keras. We load in the training set and the testing set. We have to scale our data so that it will be compatible with our network. In this case, we scale the images by dividing them by 255. This will ensure that the array values are numbers between 0 and 1.

接下來,我們將從mnist導入mnist 數(shù)據(jù)集 。 我們加載訓練集和測試集。 我們必須擴展數(shù)據(jù),以使其與我們的網(wǎng)絡兼容。 在這種情況下,我們通過將圖像除以255來縮放圖像。這將確保數(shù)組值是介于0和1之間的數(shù)字。

mnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
validation_data = x_test, y_test

可視化數(shù)字 (Visualize a digit)

We can visualize a single digit using Matplotlib. This is important so that we can see a sample of what the digits in the dataset look like.

我們可以使用Matplotlib可視化一位數(shù)。 這很重要,因此我們可以看到數(shù)據(jù)集中的數(shù)字樣本。

import matplotlib.pyplot as pltimage = x_train[2]fig = plt.figureplt.imshow(image, cmap=’gray’)plt.show()

建立 (Setup)

In this step, we need to import Keras and other packages that we’re going to use in building the CNN. Import the following packages:

在這一步中,我們需要導入Keras和其他將用于構建CNN的軟件包。 導入以下軟件包:

  • Sequential is used to initialize the neural network.

    順序用于初始化神經(jīng)網(wǎng)絡。

  • Conv2D is used to make the convolutional network that deals with the images.

    Conv2D用于制作處理圖像的卷積網(wǎng)絡。

  • MaxPooling2D layer is used to add the pooling layers.

    MaxPooling2D層用于添加池化層。

  • Flatten is the function that converts the pooled feature map to a single column that is passed to the fully connected layer.

    Flatten是將合并的要素映射轉換為傳遞到完全連接的圖層的單個列的功能。

  • Dense adds the fully connected layer to the neural network.

    密集將完全連接的層添加到神經(jīng)網(wǎng)絡。

  • Reshape for changing the shape of the input array

    重塑用于改變輸入陣列的形狀

import tensorflow as tf
from tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,Reshape

導入數(shù)據(jù)集 (Import the Dataset)

Next, we’ll import the mnist dataset from Keras. We load in the training set and the testing set. We’ve seen previously that we have to scale our data. In this case, we scale the images by dividing by 255. This will ensure that the array values are numbers between 0 and 1.

接下來,我們將從Keras導入mnist數(shù)據(jù)集。 我們加載訓練集和測試集。 之前我們已經(jīng)看到我們必須擴展數(shù)據(jù)。 在這種情況下,我們將圖像除以255進行縮放。這將確保數(shù)組值是介于0和1之間的數(shù)字。

mnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
validation_data = x_test, y_test

可視化數(shù)字 (Visualize a Number)

We can visualize a single digit using Matplotlib.

我們可以使用Matplotlib可視化一位數(shù)。

import matplotlib.pyplot as pltimage = x_train[2]fig = plt.figureplt.imshow(image, cmap=’gray’)plt.show()

Machine learning is rapidly moving closer to where data is collected — edge devices. Subscribe to the Fritz AI Newsletter to learn more about this transition and how it can help scale your business.

機器學習正Swift向收集數(shù)據(jù)的地方(邊緣設備)靠近。 訂閱Fritz AI新聞通訊以了解有關此過渡及其如何幫助您擴展業(yè)務的更多信息 。

建立網(wǎng)絡 (Building the Network)

Before we start building the network, let’s confirm the shape of the training data. This is important because the shape will be needed by the convolution layer. We’ll use this shape later to reshape the size of the input data.

在開始構建網(wǎng)絡之前,讓我們確認訓練數(shù)據(jù)的形狀。 這很重要,因為卷積層將需要該形狀。 稍后我們將使用此形狀來調整輸入數(shù)據(jù)的大小。

x_train.shape(60000, 28, 28)

We kick off by initializing the sequential model. Then we pass in our layers as a list. The first layer is the reshape layer for changing the shape of the input data. We already know that each image has a size of 28 by 28. We also pass 1 to the reshape layer to ensure that the input data has a single color channel.

我們通過初始化順序模型開始。 然后,我們將圖層作為列表傳遞。 第一層是用于改變輸入數(shù)據(jù)形狀的重塑層。 我們已經(jīng)知道每個圖像的尺寸為28 x28。我們還將1傳遞給重塑圖層,以確保輸入數(shù)據(jù)具有單個顏色通道。

Next, we create the convolution layer. This layer takes in a couple of parameters:

接下來,我們創(chuàng)建卷積層。 該層接受幾個參數(shù):

  • filters — the number of output filters in the convolution, i.e the number of feature detectors. 32 is a common number of feature detectors used.

    過濾器 -卷積中輸出過濾器的數(shù)量,即特征檢測器的數(shù)量。 32是常用的特征檢測器。

  • kernel_size — the dimensions of the feature detector matrix. This is the width and height of the convolution window.

    kernel_size —特征檢測器矩陣的尺寸。 這是卷積窗口的寬度和高度。

  • input_shape — the shape of the input data. Since we included a reshape layer, we don’t need to pass this.

    input_shape —輸入數(shù)據(jù)的形狀。 由于我們包含了一個重塑層,因此我們不需要傳遞它。

  • activation — the activation function to be uses. The ReLu activation function is a common choice.

    激活 -要使用的激活功能。 ReLu激活功能是常見的選擇。

The next item on the list is to add the pooling layer. In this step, the size of the feature map is reduced by taking the maximum value during the pooling process. 2 by 2 is a commonly-used pool size. This will reduce the size of the feature map while maintaining the most important features needed to identify the digits.

列表上的下一項是添加池化層。 在此步驟中,通過在合并過程中取最大值來減小特征圖的大小。 2 x 2是常用的池大小。 這將減少要素圖的大小,同時保留識別數(shù)字所需的最重要的要素。

After that, we flatten the feature maps. This will convert the feature maps into a single column.

之后,我們將要素圖展平 。 這將轉換 要素映射到單列。

The results obtained above are then passed to the dense layer. The parameters passed to this layer are the number of neurons and the activation function. In this case, those are 128 and ReLu, respectively.

然后將上面獲得的結果傳遞到致密層 。 傳遞給該層的參數(shù)是神經(jīng)元數(shù)量和激活函數(shù)。 在這種情況下,它們分別是128和ReLu。

Finally, we created our output layer. 10 is used here, because our dataset has 10 classes. We’ll use the softmax activation function because classes are mutually exclusive—an image can’t be, for example, classified as numbers 0 and 1 at the same time. If the case was otherwise, we’d use the sigmoid activation function.

最后,我們創(chuàng)建了輸出層 。 這里使用10,因為我們的數(shù)據(jù)集有10個類。 我們將使用softmax激活功能,因為 類是互斥的-例如,圖像不能同時分類為數(shù)字0和1。 如果不是這種情況,我們將使用S型激活函數(shù)

model = Sequential([Reshape((28, 28, 1)),Conv2D(filters=64,kernel_size=(3,3),activation=’relu’),MaxPooling2D(pool_size=(2,2)),Flatten(),Dense(128, activation=’relu’),Dense(10, activation=’softmax’)])

編譯網(wǎng)絡 (Compiling the Network)

We now need to apply gradient descent to the number. This is done at the compile stage. We pass the following parameters:

現(xiàn)在,我們需要對數(shù)字應用梯度下降 。 這是在編譯階段完成的。 我們傳遞以下參數(shù):

  • optimizer — the type of gradient descent to use; in this case, we’ll use stochastic gradient descent

    優(yōu)化器 -要使用的梯度下降類型; 在這種情況下,我們將使用隨機梯度下降

  • loss — the cost function to use. We use sparse_categorical_crossentropy because the classes are integer-encoded. Had we performed one-hot encoding, we’d have to use the categorical cross-entropy loss function.

    損失 —使用的成本函數(shù)。 我們使用sparse_categorical_crossentropy 因為這些類是整數(shù)編碼的。 如果我們執(zhí)行了一次熱編碼,則必須使用分類交叉熵損失函數(shù)。

  • metrics — the evaluation metrics to monitor; in this case, accuracy

    指標 —要監(jiān)視的評估指標; 在這種情況下,準確性

model.compile(optimizer=’sgd’,loss=’sparse_categorical_crossentropy’,metrics=[‘a(chǎn)ccuracy’,])

安裝CNN (Fitting the CNN)

Next, we fit the training set to the network. epochs is the number of rounds the data will pass through the network. validation_data is the test dataset that will be used to evalutate the performance of the model during training.

接下來,我們將訓練集適合網(wǎng)絡。 epochs是數(shù)據(jù)將通過網(wǎng)絡的輪數(shù)。 validation_data是測試數(shù)據(jù)集,將用于評估訓練期間模型的性能。

model.fit(x=x_train,y=y_train,epochs=5,validation_data=validation_data)

評估網(wǎng)絡 (Evaluating the Network)

We can now check the performance of the network. We do so using the evaluate function, passing in the test set. The first item in the result produced is the loss, and the second is the accuracy.

現(xiàn)在,我們可以檢查網(wǎng)絡的性能。 我們使用evaluate函數(shù)來傳遞測試集。 產(chǎn)生的結果中的第一項是loss ,第二項是accuracy 。

model.evaluate(x_test,y_test)
313/313 [==============================] - 2s 8ms/step - loss: 0.1116 - accuracy: 0.9668
[0.1115519180893898, 0.9667999744415283]

檢查預測 (Checking the Predictions)

Let’s now see if our network is able to correctly identify a digit. We kick off by making predictions:

現(xiàn)在讓我們看看我們的網(wǎng)絡是否能夠正確識別數(shù)字。 我們通過做出預測開始:

predictions = model.predict(x_test)

For example, let’s check the digit at index 2 in the test set.

例如,讓我們檢查測試集中索引2處的數(shù)字。

image = x_test[2]fig = plt.figureplt.imshow(image, cmap=’gray’)plt.show()

This is clearly the digit one. Let’s compare that with the prediction made for digit 1.

這顯然是數(shù)字。 讓我們將其與數(shù)字1的預測進行比較。

import numpy as np
np.set_printoptions(suppress=True)
predictions[2]*100array([ 0.00077352, 99.63969 , 0.07214971, 0.03056707, 0.00606834,
0.00301009, 0.02507676, 0.11558129, 0.10332227, 0.00378496],
dtype=float32)

From the predictions below, we can see that the digit 1 — at index 1 in the prediction array— has the highest prediction.

從下面的預測中,我們可以看到數(shù)字1(在預測數(shù)組的索引1處)具有最高的預測。

0.00077352 — digit 0 prediction

0.00077352 —數(shù)字0預測

99.63969 — digit 1 prediction

99.63969 —數(shù)字1的預測

0.07214971 — digit 2 prediction

0.07214971-數(shù)字2預測

0.03056707 — digit 3 prediction

0.03056707 —數(shù)字3預測

0.00606834 — digit 4 prediction

0.00606834 —數(shù)字4預測

0.00301009 — digit 5 prediction

0.00301009-數(shù)字5預測

0.02507676 — digit 6 prediction

0.02507676 —數(shù)字6的預測

0.11558129 — digit 7 prediction

0.11558129-數(shù)字7預測

0.10332227 — digit 8 prediction

0.10332227 —數(shù)字8預測

0.00378496 — digit 9 prediction

0.00378496 —數(shù)字9預測

We can check another prediction by plotting the digit at index 0 in the testing set. Let’s try number 7.

我們可以通過在測試集中的索引0處繪制數(shù)字來檢查另一個預測。 讓我們嘗試數(shù)字7。

image = x_test[0]
fig = plt.figure
plt.imshow(image, cmap=’gray’)
plt.show()

Next, we’ll look at the prediction at index 0 to see if it’s also number 7. Clearly, 7 has the highest prediction.

接下來,我們將看一下索引0處的預測,看看它是否也是7。顯然,7具有最高的預測。

import numpy as npnp.set_printoptions(suppress=True)predictions[0]*100array([ 0.00032742, 0.00000064, 0.00919174, 0.09352361, 0.00000077, 0.00045173, 0.00000015, 99.88985 , 0.00224745, 0.00440894], dtype=float32)

結論 (Conclusion)

Well done for making it this far! In this article, we’ve used CNN core concepts to develop a working convolutional neural network. We have seen the steps you’ll need to take, and we’ve evaluated our trained model. Now you can try this out with a different dataset to see if you can reproduce the network we built here.

到現(xiàn)在為止做得很好! 在本文中,我們使用了CNN核心概念來開發(fā)有效的卷積神經(jīng)網(wǎng)絡。 我們已經(jīng)看到了您需要采取的步驟,并且已經(jīng)評估了我們訓練有素的模型。 現(xiàn)在,您可以使用其他數(shù)據(jù)集進行嘗試,以查看是否可以重現(xiàn)我們在此處構建的網(wǎng)絡。

Editor’s Note: Heartbeat is a contributor-driven online publication and community dedicated to exploring the emerging intersection of mobile app development and machine learning. We’re committed to supporting and inspiring developers and engineers from all walks of life.

編者注: 心跳 是由貢獻者驅動的在線出版物和社區(qū),致力于探索移動應用程序開發(fā)和機器學習的新興交集。 我們致力于為各行各業(yè)的開發(fā)人員和工程師提供支持和啟發(fā)。

Editorially independent, Heartbeat is sponsored and published by Fritz AI, the machine learning platform that helps developers teach devices to see, hear, sense, and think. We pay our contributors, and we don’t sell ads.

Heartbeat在編輯上是獨立的,由以下機構贊助和發(fā)布 Fritz AI ,一種機器學習平臺,可幫助開發(fā)人員教設備看,聽,感知和思考。 我們向貢獻者付款,并且不出售廣告。

If you’d like to contribute, head on over to our call for contributors. You can also sign up to receive our weekly newsletters (Deep Learning Weekly and the Fritz AI Newsletter), join us on Slack, and follow Fritz AI on Twitter for all the latest in mobile machine learning.

如果您想做出貢獻,請繼續(xù)我們的 呼吁捐助者 。 您還可以注冊以接收我們的每周新聞通訊(《 深度學習每周》 和《 Fritz AI新聞通訊》 ),并加入我們 Slack ,然后繼續(xù)關注Fritz AI Twitter 提供了有關移動機器學習的所有最新信息。

翻譯自: https://heartbeat.fritz.ai/convolutional-neural-networks-cnns-core-concepts-applied-3c12e60e2c40

cnn卷積神經(jīng)網(wǎng)絡應用

總結

以上是生活随笔為你收集整理的cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。