溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python人工智能算法之人工神經(jīng)網(wǎng)絡(luò)怎么使用

發(fā)布時(shí)間:2023-03-21 13:55:03 來(lái)源:億速云 閱讀:88 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“python人工智能算法之人工神經(jīng)網(wǎng)絡(luò)怎么使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

人工神經(jīng)網(wǎng)絡(luò)

(Artificial Neural Network,ANN)是一種模仿生物神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)和功能的數(shù)學(xué)模型,其目的是通過(guò)學(xué)習(xí)和訓(xùn)練,在處理未知的輸入數(shù)據(jù)時(shí)能夠進(jìn)行復(fù)雜的非線性映射關(guān)系,實(shí)現(xiàn)自適應(yīng)的智能決策??梢哉f(shuō),ANN是人工智能算法中最基礎(chǔ)、最核心的一種算法。

ANN模型的基本結(jié)構(gòu)包含輸入層、隱藏層和輸出層。輸入層接收輸入數(shù)據(jù),隱藏層負(fù)責(zé)對(duì)數(shù)據(jù)進(jìn)行多層次、高維度的變換和處理,輸出層對(duì)處理后的數(shù)據(jù)進(jìn)行輸出。ANN的訓(xùn)練過(guò)程是通過(guò)多次迭代,不斷調(diào)整神經(jīng)網(wǎng)絡(luò)中各層的權(quán)重,從而使得神經(jīng)網(wǎng)絡(luò)能夠?qū)斎霐?shù)據(jù)進(jìn)行正確的預(yù)測(cè)和分類。

人工神經(jīng)網(wǎng)絡(luò)算法示例

接下來(lái)看看一個(gè)簡(jiǎn)單的人工神經(jīng)網(wǎng)絡(luò)算法示例:

import numpy as np
class NeuralNetwork():
    def __init__(self, layers):
        """
        layers: 數(shù)組,包含每個(gè)層的神經(jīng)元數(shù)量,例如 [2, 3, 1] 表示 3 層神經(jīng)網(wǎng)絡(luò),第一層 2 個(gè)神經(jīng)元,第二層 3 個(gè)神經(jīng)元,第三層 1 個(gè)神經(jīng)元。
        weights: 數(shù)組,包含每個(gè)連接的權(quán)重矩陣,默認(rèn)值隨機(jī)生成。
        biases: 數(shù)組,包含每個(gè)層的偏差值,默認(rèn)值為 0。
        """
        self.layers = layers
        self.weights = [np.random.randn(a, b) for a, b in zip(layers[1:], layers[:-1])]
        self.biases = [np.zeros((a, 1)) for a in layers[1:]]
    def sigmoid(self, z):
        """Sigmoid 激活函數(shù)."""
        return 1 / (1 + np.exp(-z))
    def forward_propagation(self, a):
        """前向傳播."""
        for w, b in zip(self.weights, self.biases):
            z = np.dot(w, a) + b
            a = self.sigmoid(z)
        return a
    def backward_propagation(self, x, y):
        """反向傳播."""
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        a = x
        activations = [x]
        zs = []
        for w, b in zip(self.weights, self.biases):
            z = np.dot(w, a) + b
            zs.append(z)
            a = self.sigmoid(z)
            activations.append(a)
        delta = self.cost_derivative(activations[-1], y) * self.sigmoid_prime(zs[-1])
        nabla_b[-1] = delta
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())
        for l in range(2, len(self.layers)):
            z = zs[-l]
            sp = self.sigmoid_prime(z)
            delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
            nabla_b[-l] = delta
            nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
        return (nabla_w, nabla_b)
    def train(self, x_train, y_train, epochs, learning_rate):
        """訓(xùn)練網(wǎng)絡(luò)."""
        for epoch in range(epochs):
            nabla_w = [np.zeros(w.shape) for w in self.weights]
            nabla_b = [np.zeros(b.shape) for b in self.biases]
            for x, y in zip(x_train, y_train):
                delta_nabla_w, delta_nabla_b = self.backward_propagation(np.array([x]).transpose(), np.array([y]).transpose())
                nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
                nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
            self.weights = [w-(learning_rate/len(x_train))*nw for w, nw in zip(self.weights, nabla_w)]
            self.biases = [b-(learning_rate/len(x_train))*nb for b, nb in zip(self.biases, nabla_b)]
    def predict(self, x_test):
        """預(yù)測(cè)."""
        y_predictions = []
        for x in x_test:
            y_predictions.append(self.forward_propagation(np.array([x]).transpose())[0][0])
        return y_predictions
    def cost_derivative(self, output_activations, y):
        """損失函數(shù)的導(dǎo)數(shù)."""
        return output_activations - y
    def sigmoid_prime(self, z):
        """Sigmoid 函數(shù)的導(dǎo)數(shù)."""
        return self.sigmoid(z) * (1 - self.sigmoid(z))

使用以下代碼示例來(lái)實(shí)例化和使用這個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)類:

x_train = [[0, 0], [1, 0], [0, 1], [1, 1]]
y_train = [0, 1, 1, 0]
# 創(chuàng)建神經(jīng)網(wǎng)絡(luò)
nn = NeuralNetwork([2, 3, 1])
# 訓(xùn)練神經(jīng)網(wǎng)絡(luò)
nn.train(x_train, y_train, 10000, 0.1)
# 測(cè)試神經(jīng)網(wǎng)絡(luò)
x_test = [[0, 0], [1, 0], [0, 1], [1, 1]]
y_test = [0, 1, 1, 0]
y_predictions = nn.predict(x_test)
print("Predictions:", y_predictions)
print("Actual:", y_test)

輸出結(jié)果:

Predictions: [0.011602156431658403, 0.9852717774725432, 0.9839448924887225, 0.020026540429992387]
Actual: [0, 1, 1, 0]

“python人工智能算法之人工神經(jīng)網(wǎng)絡(luò)怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI