深度學(xué)習(xí)中,神經(jīng)網(wǎng)絡(luò)是核心模型。今天我們用 Python 和 NumPy 構(gòu)建一個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)。
神經(jīng)網(wǎng)絡(luò)由多個(gè)神經(jīng)元組成,神經(jīng)元之間通過(guò)權(quán)重連接。我們構(gòu)建一個(gè)包含輸入層、隱藏層和輸出層的簡(jiǎn)單神經(jīng)網(wǎng)絡(luò)。
首先,導(dǎo)入必要的庫(kù):
收起
python
import numpy as np
定義激活函數(shù) Sigmoid:
收起
python
def sigmoid(x): return 1 / (1 + np.exp(-x))
定義神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)和參數(shù)初始化:
收起
python
# 輸入層節(jié)點(diǎn)數(shù) input_size = 2 # 隱藏層節(jié)點(diǎn)數(shù) hidden_size = 3 # 輸出層節(jié)點(diǎn)數(shù) output_size = 1 # 初始化權(quán)重,使用隨機(jī)數(shù) weights1 = np.random.randn(input_size, hidden_size) weights2 = np.random.randn(hidden_size, output_size)
前向傳播函數(shù):
收起
python
def forward_propagation(inputs): hidden_layer = sigmoid(np.dot(inputs, weights1)) output_layer = sigmoid(np.dot(hidden_layer, weights2)) return output_layer
假設(shè)我們有一個(gè)輸入數(shù)據(jù):
收起
python
# 示例輸入 inputs = np.array([0.5, 0.3]) output = forward_propagation(inputs) print(f"神經(jīng)網(wǎng)絡(luò)的輸出: {output}")
在這個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)中,輸入數(shù)據(jù)通過(guò)權(quán)重矩陣與隱藏層和輸出層進(jìn)行計(jì)算,經(jīng)過(guò)激活函數(shù)處理后得到最終輸出。雖然這只是一個(gè)簡(jiǎn)單的示例,但理解其原理是深入學(xué)習(xí)深度學(xué)習(xí)的基礎(chǔ)。
審核編輯 黃宇
-
神經(jīng)網(wǎng)絡(luò)
+關(guān)注
關(guān)注
42文章
4779瀏覽量
101160 -
深度學(xué)習(xí)
+關(guān)注
關(guān)注
73文章
5513瀏覽量
121540
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論