大家好,我是微學(xué)AI,今天給大家介紹一下人工智能(pytorch)搭建模型12-pytorch搭建BiGRU模型,利用正態(tài)分布數(shù)據(jù)訓(xùn)練該模型。本文將介紹一種基于PyTorch的BiGRU模型應(yīng)用項(xiàng)目。我們將首先解釋BiGRU模型的原理,然后使用PyTorch搭建模型,并提供模型代碼和數(shù)據(jù)樣例。接下來(lái),我們將加載數(shù)據(jù)到模型中進(jìn)行訓(xùn)練,打印損失值與準(zhǔn)確率,并在訓(xùn)練完成后進(jìn)行測(cè)試。最后,我們將提供完整的文章目錄結(jié)構(gòu)和全套實(shí)現(xiàn)代碼。
目錄
- BiGRU模型原理
- 使用PyTorch搭建BiGRU模型
- 數(shù)據(jù)樣例
- 模型訓(xùn)練
- 模型測(cè)試
- 完整代碼
1. BiGRU模型原理
BiGRU(雙向門控循環(huán)單元)是一種改進(jìn)的循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)結(jié)構(gòu),它由兩個(gè)獨(dú)立的GRU層組成,一個(gè)沿正向處理序列,另一個(gè)沿反向處理序列。這種雙向結(jié)構(gòu)使得BiGRU能夠捕捉到序列中的長(zhǎng)距離依賴關(guān)系,從而提高模型的性能。
GRU(門控循環(huán)單元)是一種RNN變體,它通過(guò)引入更新門和重置門來(lái)解決傳統(tǒng)RNN中的梯度消失問(wèn)題。更新門負(fù)責(zé)確定何時(shí)更新隱藏狀態(tài),而重置門負(fù)責(zé)確定何時(shí)允許過(guò)去的信息影響當(dāng)前隱藏狀態(tài)。
BiGRU模型的數(shù)學(xué)原理可以用以下公式表示:
首先,對(duì)于一個(gè)輸入序列 X = x 1 x 2 , . . . , x T X = {x_1 x_2, ..., x_T} X=x1?x2?,...,xT?,BiGRU模型的前向計(jì)算可以表示為:
h t → = GRU ( h t ? 1 → , x t ) \overrightarrow{h_t} = \text{GRU}(\overrightarrow{h_{t-1}}, x_t) ht??=GRU(ht?1??,xt?)
h t ← = GRU ( h t + 1 ← , x t ) \overleftarrow{h_t} = \text{GRU}(\overleftarrow{h_{t+1}}, x_t) ht??=GRU(ht+1??,xt?)
其中, h t → \overrightarrow{h_t} ht?? 和 h t ← \overleftarrow{h_t} ht?? 分別表示從左到右和從右到左的隱藏狀態(tài), GRU \text{GRU} GRU 表示GRU單元, x t x_t xt? 表示輸入序列中的第 t t t 個(gè)元素。
然后,將兩個(gè)方向的隱藏狀態(tài)拼接在一起,得到最終的隱藏狀態(tài) h t h_t ht?:
h t = [ h t → ; h t ← ] h_t = [\overrightarrow{h_t}; \overleftarrow{h_t}] ht?=[ht??;ht??]
其中, [ ? ; ? ] [\cdot;\cdot] [?;?] 表示向量的拼接操作。
最后,將隱藏狀態(tài) h t h_t ht? 傳遞給一個(gè)全連接層,得到輸出 y t y_t yt?:
y t = softmax ( W h t + b ) y_t = \text{softmax}(W h_t + b) yt?=softmax(Wht?+b)
其中, W W W 和 b b b 分別表示全連接層的權(quán)重和偏置, softmax \text{softmax} softmax 表示 softmax \text{softmax} softmax激活函數(shù)。
2. 使用PyTorch搭建BiGRU模型
首先,我們需要導(dǎo)入所需的庫(kù):
import torch
import torch.nn as nn
接下來(lái),我們定義BiGRU模型類:
class BiGRU(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(BiGRU, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.gru = nn.GRU(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_size * 2, num_classes)
def forward(self, x):
# 初始化隱藏狀態(tài)
h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(device)
# 雙向GRU
out, _ = self.gru(x, h0)
out = out[:, -1, :]
# 全連接層
out = self.fc(out)
return out
3. 數(shù)據(jù)樣例
為了簡(jiǎn)化問(wèn)題,我們將使用一個(gè)簡(jiǎn)單的人造數(shù)據(jù)集。數(shù)據(jù)集包含10個(gè)樣本,每個(gè)樣本有8個(gè)時(shí)間步長(zhǎng),每個(gè)時(shí)間步長(zhǎng)有一個(gè)特征。標(biāo)簽是一個(gè)二分類問(wèn)題。
# 生成數(shù)據(jù)樣例
import numpy as np
# 均值為1的正態(tài)分布隨機(jī)數(shù)
data_0 = np.random.randn(50, 20, 1) + 1
# 均值為-1的正態(tài)分布隨機(jī)數(shù)
data_1 = np.random.randn(50, 20, 1) - 1
# 合并為總數(shù)據(jù)集
data = np.concatenate([data_0, data_1], axis=0)
# 將 labels 修改為對(duì)應(yīng)大小的數(shù)組
labels = np.concatenate([np.zeros((50, 1)), np.ones((50, 1))], axis=0)
4. 模型訓(xùn)練
首先,我們需要將數(shù)據(jù)轉(zhuǎn)換為PyTorch張量,并將其分為訓(xùn)練集和驗(yàn)證集。
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(data, labels, test_size=0.2, random_state=42)
X_train = torch.tensor(X_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.long)
X_val = torch.tensor(X_val, dtype=torch.float32)
y_val = torch.tensor(y_val, dtype=torch.long)
接下來(lái),我們定義訓(xùn)練和驗(yàn)證函數(shù):
def train(model, device, X_train, y_train, optimizer, criterion):
model.train()
optimizer.zero_grad()
output = model(X_train.to(device))
loss = criterion(output, y_train.squeeze().to(device))
loss.backward()
optimizer.step()
return loss.item()
def validate(model, device, X_val, y_val, criterion):
model.eval()
with torch.no_grad():
output = model(X_val.to(device))
loss = criterion(output, y_val.squeeze().to(device))
return loss.item()
現(xiàn)在,我們可以開始訓(xùn)練模型:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
input_size = 1
hidden_size = 32
num_layers = 1
num_classes = 2
num_epochs = 10
learning_rate = 0.01
model = BiGRU(input_size, hidden_size, num_layers, num_classes).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
train_loss = train(model, device, X_train, y_train, optimizer, criterion)
val_loss = validate(model, device, X_val, y_val, criterion)
print(f"Epoch [{epoch + 1}/{num_epochs}], Train Loss: {train_loss:.4f}, Validation Loss: {val_loss:.4f}")
5. 模型測(cè)試
在訓(xùn)練完成后,我們可以使用測(cè)試數(shù)據(jù)集評(píng)估模型的性能。這里,我們將使用訓(xùn)練過(guò)程中的驗(yàn)證數(shù)據(jù)作為測(cè)試數(shù)據(jù)。
def test(model, device, X_test, y_test):
model.eval()
with torch.no_grad():
output = model(X_test.to(device))
_, predicted = torch.max(output.data, 1)
correct = (predicted == y_test.squeeze().to(device)).sum().item()
accuracy = correct / y_test.size(0)
return accuracy
test_accuracy = test(model, device, X_val, y_val)
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")
6. 完整代碼
以下是本文中提到的完整代碼:
# 導(dǎo)入庫(kù)
import torch
import torch.nn as nn
import numpy as np
from sklearn.model_selection import train_test_split
# 定義BiGRU模型
class BiGRU(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(BiGRU, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.gru = nn.GRU(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_size * 2, num_classes)
def forward(self, x):
h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(device)
out, _ = self.gru(x, h0)
out = out[:, -1, :]
out = self.fc(out)
return out
# 生成數(shù)據(jù)樣例
# 均值為1的正態(tài)分布隨機(jī)數(shù)
data_0 = np.random.randn(50, 20, 1) + 1
# 均值為-1的正態(tài)分布隨機(jī)數(shù)
data_1 = np.random.randn(50, 20, 1) - 1
# 合并為總數(shù)據(jù)集
data = np.concatenate([data_0, data_1], axis=0)
# 將 labels 修改為對(duì)應(yīng)大小的數(shù)組
labels = np.concatenate([np.zeros((50, 1)), np.ones((50, 1))], axis=0)
# 劃分訓(xùn)練集和驗(yàn)證集
X_train, X_val, y_train, y_val = train_test_split(data, labels, test_size=0.2, random_state=42)
X_train = torch.tensor(X_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.long)
X_val = torch.tensor(X_val, dtype=torch.float32)
y_val = torch.tensor(y_val, dtype=torch.long)
# 定義訓(xùn)練和驗(yàn)證函數(shù)
def train(model, device, X_train, y_train, optimizer, criterion):
model.train()
optimizer.zero_grad()
output = model(X_train.to(device))
loss = criterion(output, y_train.squeeze().to(device))
loss.backward()
optimizer.step()
return loss.item()
def validate(model, device, X_val, y_val, criterion):
model.eval()
with torch.no_grad():
output = model(X_val.to(device))
loss = criterion(output, y_val.squeeze().to(device))
return loss.item()
# 訓(xùn)練模型
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
input_size = 1
hidden_size = 32
num_layers = 1
num_classes = 2
num_epochs = 10
learning_rate = 0.01
model = BiGRU(input_size, hidden_size, num_layers, num_classes).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
train_loss = train(model, device, X_train, y_train, optimizer, criterion)
val_loss = validate(model, device, X_val, y_val, criterion)
print(f"Epoch [{epoch + 1}/{num_epochs}], Train Loss: {train_loss:.4f}, Validation Loss: {val_loss:.4f}")
# 測(cè)試模型
def test(model, device, X_test, y_test):
model.eval()
with torch.no_grad():
output = model(X_test.to(device))
_, predicted = torch.max(output.data, 1)
correct = (predicted == y_test.squeeze().to(device)).sum().item()
accuracy = correct / y_test.size(0)
return accuracy
test_accuracy = test(model, device, X_val, y_val)
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")
運(yùn)行結(jié)果:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-490738.html
Epoch [1/10], Train Loss: 0.7157, Validation Loss: 0.6330
Epoch [2/10], Train Loss: 0.6215, Validation Loss: 0.5666
Epoch [3/10], Train Loss: 0.5390, Validation Loss: 0.4980
Epoch [4/10], Train Loss: 0.4613, Validation Loss: 0.4214
Epoch [5/10], Train Loss: 0.3825, Validation Loss: 0.3335
Epoch [6/10], Train Loss: 0.2987, Validation Loss: 0.2357
Epoch [7/10], Train Loss: 0.2096, Validation Loss: 0.1381
Epoch [8/10], Train Loss: 0.1230, Validation Loss: 0.0644
Epoch [9/10], Train Loss: 0.0581, Validation Loss: 0.0273
Epoch [10/10], Train Loss: 0.0252, Validation Loss: 0.0125
Test Accuracy: 100.00%
本文介紹了一個(gè)基于PyTorch的BiGRU模型應(yīng)用項(xiàng)目的完整實(shí)現(xiàn)。我們?cè)敿?xì)介紹了BiGRU模型的原理,并使用PyTorch搭建了模型。我們還提供了模型代碼和數(shù)據(jù)樣例,并展示了如何加載數(shù)據(jù)到模型中進(jìn)行訓(xùn)練和測(cè)試。希望能幫助大家理解和實(shí)現(xiàn)BiGRU模型。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-490738.html
到了這里,關(guān)于人工智能(pytorch)搭建模型12-pytorch搭建BiGRU模型,利用正態(tài)分布數(shù)據(jù)訓(xùn)練該模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!