偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

快速學(xué)習(xí)一個(gè)算法,CNN

人工智能
與將輸入視為平面像素陣列的傳統(tǒng)神經(jīng)網(wǎng)絡(luò)不同,CNN 利用圖像的空間結(jié)構(gòu)來提取分層特征。這種能力使 CNN 在圖像分類、對(duì)象檢測(cè)和分割等任務(wù)中特別有效。

大家好,我是小寒

今天給大家介紹一個(gè)強(qiáng)大的算法模型,CNN

卷積神經(jīng)網(wǎng)絡(luò) (CNN) 是一類深度學(xué)習(xí)算法,主要用于處理和分析視覺數(shù)據(jù)。

它們徹底改變了計(jì)算機(jī)視覺領(lǐng)域,使圖像識(shí)別、物體檢測(cè)和各種其他應(yīng)用取得了突破。

什么是卷積神經(jīng)網(wǎng)絡(luò)?

卷積神經(jīng)網(wǎng)絡(luò) (CNN) 是一種人工神經(jīng)網(wǎng)絡(luò),專門用于處理結(jié)構(gòu)化網(wǎng)格數(shù)據(jù)(例如圖像)。

與將輸入視為平面像素陣列的傳統(tǒng)神經(jīng)網(wǎng)絡(luò)不同,CNN 利用圖像的空間結(jié)構(gòu)來提取分層特征。這種能力使 CNN 在圖像分類、對(duì)象檢測(cè)和分割等任務(wù)中特別有效。

圖片圖片

關(guān)鍵組件

1.卷積層

卷積層是 CNN 的核心組件,用于提取輸入數(shù)據(jù)的特征。

卷積層通過濾波器(卷積核)在輸入數(shù)據(jù)上滑動(dòng),進(jìn)行卷積操作,生成特征圖(Feature Map)。每個(gè)卷積核負(fù)責(zé)捕捉圖像的不同特征,如邊緣、紋理等。

圖片圖片


  • 卷積核
    檢測(cè)特定特征(如邊緣、紋理或圖案)的小矩陣。常見尺寸為 3x3 或 5x5。
  • 步幅
    卷積核在輸入圖像上移動(dòng)的步長。步幅為 1 表示卷積核每次移動(dòng)一個(gè)像素。
  • 填充
    在輸入圖像的邊框周圍添加額外的像素,以確保卷積核正確適配。

圖片圖片

以下是使用 NumPy 的簡單實(shí)現(xiàn)。

import numpy as np

def convolve(image, kernel, stride=1, padding=0):
    # Add padding to the input image
    image_padded = np.pad(image, [(padding, padding), (padding, padding)], mode='constant', constant_values=0)
    
    # Calculate output dimensions
    output_height = (image.shape[0] - kernel.shape[0] + 2 * padding) // stride + 1
    output_width = (image.shape[1] - kernel.shape[1] + 2 * padding) // stride + 1
    
    # Initialize output
    output = np.zeros((output_height, output_width))
    
    # Perform convolution
    for i in range(0, output_height, stride):
        for j in range(0, output_width, stride):
            output[i, j] = np.sum(image_padded[i:i+kernel.shape[0], j:j+kernel.shape[1]] * kernel)
    
    return output

2.池化層

池化層通常用于降低特征圖的空間尺寸(高度和寬度),減少參數(shù)數(shù)量和計(jì)算復(fù)雜性,同時(shí)使特征檢測(cè)更加魯棒。

池化操作主要有兩種類型:

  • 最大池化:從特征圖的每個(gè)塊中獲取最大值。
  • 平均池化:從特征圖的每個(gè)塊中取平均值。

圖片圖片

def max_pooling(image, size=2, stride=2):
    output_height = (image.shape[0] - size) // stride + 1
    output_width = (image.shape[1] - size) // stride + 1
    output = np.zeros((output_height, output_width))
    
    for i in range(0, output_height, stride):
        for j in range(0, output_width, stride):
            output[i, j] = np.max(image[i:i+size, j:j+size])
    
    return output

3.全連接層

經(jīng)過幾個(gè)卷積層和池化層之后,神經(jīng)網(wǎng)絡(luò)中的高級(jí)推理通過全連接層完成。

這些層將扁平化的特征圖作為輸入,并將其用于最終分類。

圖片圖片

CNN 的執(zhí)行過程

讓我們通過一個(gè)例子來了解 CNN 如何處理圖像:

  1. 輸入圖像
    考慮 32x32x3 RGB 圖像(高度、寬度、深度)。
  2. 卷積層
    對(duì)圖像應(yīng)用多個(gè)過濾器(例如 3x3),從而產(chǎn)生多個(gè)特征圖。假設(shè)我們使用 10 個(gè)過濾器;輸出將為 32x32x10。
  3. 池化層
    使用 2x2 最大池化,步長為 2,以減少空間維度。輸出將為 16x16x10。
  4. 額外的卷積和池化層
    根據(jù)需要重復(fù)卷積、激活和池化操作。
  5. 全連接層
    將最后一個(gè)池化層的輸出展平(例如,8x8x10 變?yōu)?640 維向量)并將其連接到密集層以進(jìn)行分類。
  6. 輸出層
    應(yīng)用softmax函數(shù)獲取類別的概率分布。

下面我們將使用 TensorFlow 構(gòu)建一個(gè)簡單的 CNN,用于 MNIST 數(shù)據(jù)集(手寫數(shù)字?jǐn)?shù)據(jù)集)的圖像分類。

首先我們導(dǎo)入必要的庫

import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt

然后加載和預(yù)處理數(shù)據(jù)

# Load the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

# Normalize the images
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

構(gòu)建 CNN 模型

model = models.Sequential([
    layers.Conv2D(32, (3, 3), activatinotallow='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activatinotallow='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activatinotallow='relu'),
    layers.Flatten(),
    layers.Dense(64, activatinotallow='relu'),
    layers.Dense(10, activatinotallow='softmax')
])
model.summary()

編譯并訓(xùn)練模型

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=5, 
                    validation_data=(test_images, test_labels))

評(píng)估模型

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()

圖片圖片

責(zé)任編輯:武曉燕 來源: 程序員學(xué)長
相關(guān)推薦

2024-07-19 08:21:24

2024-06-06 09:44:33

2024-08-21 08:21:45

CNN算法神經(jīng)網(wǎng)絡(luò)

2024-08-12 00:00:05

集成學(xué)習(xí)典型算法代碼

2024-07-12 08:38:05

2024-08-08 12:33:55

算法

2024-08-22 08:24:51

算法CNN深度學(xué)習(xí)

2024-09-09 23:04:04

2024-06-20 08:52:10

2024-08-22 08:21:10

算法神經(jīng)網(wǎng)絡(luò)參數(shù)

2024-09-24 07:28:10

2024-08-02 10:28:13

算法NLP模型

2024-12-19 00:16:43

2024-06-03 08:09:39

2024-06-19 09:47:21

2024-09-06 12:52:59

2024-08-29 09:18:55

2024-11-11 00:00:02

卷積神經(jīng)網(wǎng)絡(luò)算法

2024-12-04 10:33:17

2024-06-12 10:18:33

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)