pytorch(八) RNN迴圈神經網路 分類

2021-08-22 15:07:05 字數 3487 閱讀 5463

import torch

import torch.nn as nn

import torchvision.transforms as transforms

from torch.autograd import variable

import matplotlib.pyplot as plt

import torchvision.datasets as dsets

# 超引數

epoch = 1 # 為了節省時間,只訓練一趟

batch_size = 64

time_step = 28

input_size = 28

lr = 0.01

download_mnist = true

# mnist數字資料集

train_data = dsets.mnist(

root='./mnist/',

train=true,

transform=transforms.totensor(),

download=download_mnist,

)# 列印出其中乙個例子

print(train_data.train_data.size()) # (60000, 28, 28)

print(train_data.train_labels.size()) # (60000)

plt.imshow(train_data.train_data[1], cmap='gray')

plt.title('%i' % train_data.train_labels[1])

plt.show()

# 資料載入機

train_loader = torch.utils.data.dataloader(

dataset=train_data,

batch_size=batch_size,

shuffle=true,

)# 把test_data轉化成variable, 拿出前2000個作為檢測

test_data = dsets.mnist(

root='./mnist/',

train=false,

transform=transforms.totensor(),

)test_x = variable(test_data.test_data, volatile=true).type(torch.floattensor)[:2000]/255

test_y = test_data.test_labels.numpy().squeeze()[:2000]

class rnn(nn.module):

def __init__(self):

super(rnn, self).__init__()

self.rnn = nn.lstm( # 如果使用nn.rnn(),它幾乎無法學習

input_size=input_size,

hidden_size=64, # rnn隱藏單位

num_layers=1, # rnn層的數量

batch_first=true, # input&output的批量大小是1維度。 例如 (batch,time_step,input_size)

)self.out = nn.linear(64, 10)

def forward(self, x):

# x shape (batch, time_step, input_size)

# r_out shape (batch, time_step, output_size)

# h_n shape (n_layers, batch, hidden_size)

# h_c shape (n_layers, batch, hidden_size)

r_out, (h_n, h_c) = self.rnn(x, none) # none表示零初始隱藏狀態

# choose r_out at the last time step

out = self.out(r_out[:, -1, :])

return out

rnn = rnn()

print(rnn)

optimizer = torch.optim.adam(rnn.parameters(), lr=lr)

loss_func = nn.crossentropyloss()

# 訓練並測試神經網路

for epoch in range(epoch):

for step, (x, y) in enumerate(train_loader): # gives batch data

b_x = variable(x.view(-1, 28, 28)) # reshape x to (batch, time_step, input_size)

b_y = variable(y) # batch y

output = rnn(b_x) # rnn output

loss = loss_func(output, b_y) # cross entropy loss

optimizer.zero_grad() # clear gradients for this training step

loss.backward() # backpropagation, compute gradients

if step % 50 == 0:

test_output = rnn(test_x) # (samples, time_step, input_size)

pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()

accuracy = sum(pred_y == test_y) / float(test_y.size)

print('epoch: ', epoch, '| train loss: %.4f' % loss.data[0], '| test accuracy: %.2f' % accuracy)

# 列印前10個測試值

test_output = rnn(test_x[:10].view(-1, 28, 28))

pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()

print(pred_y, 'prediction number')

print(test_y[:10], 'real number')

執行結果:

RNN 迴圈神經網路 分類 pytorch

import torch from torch import nn import torchvision.datasets as dsets import torchvision.transforms as transforms import matplotlib.pyplot as plt imp...

RNN迴圈神經網路

評測乙個句子出現的可能性 文字自動生成 rnn迴圈處理相同的任務,就是 句子接下來的單詞是什麼。rnn迴圈記錄前面所有文字的資訊 也叫做記憶 作為 當前詞的乙個輸入 1.語言模型和文字生成 語言模型中,輸入是經過編碼的詞向量序列,輸出是一系列 的詞 2.機器翻譯 機器翻譯必須等待所有輸入結束後才能輸...

迴圈神經網路 RNN

from torchnlp.word to vector import glove vectors glove vectors hello torch.nn.utils.clip grad norm p,10 10就是最大梯度的模閾值,即保留原來梯度的方向,但梯度大小縮放至10對與梯度瀰散現象,考慮...