天天看點

Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)

資料集連結:https://pan.baidu.com/s/1bIKasCIDAaT-_EwB6hcAMQ

提取碼:4fij

任務:使用RNN通過訓練name資料集來預測name屬于哪個country.

RNN,LSTM,GRU都是循環神經網絡。

網絡模型:

Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)

 最後隻需要一個Linear Layer來得出整個name序列的預測結果。

 資料準備

Name序列處理步驟:

1.Name轉成序列List,即Maclean→['M', 'a', 'c', 'l', 'e', 'a', 'n']

2.用ASCⅡ碼表示List中的元素,即[77 97 99 108 101 97 110],裡面不是代表數值,而是one-hot向量,比如77是指共128維的向量,隻有Idex77是1,其他都是0

Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
3.Padding将一個batch_size的序列填充成長度相同,即[77 97 99 108 101 97 110 0 0 0]
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
4.将Padding後的矩陣轉置
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
 5.按照序列長度降序排列
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)

 同時要獲得對應順序的序列長度清單[10, 8, 7, 7, 7, 7,6, 6, 5]

 Country的處理步驟:

Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)

 給每個Country一個索引,構成字典{‘country’: idx},在name排序之後,也要按照相同的排序将name對應的Country标簽排序。

就是說,在拿到一個name後,就能根據name在batch_size中的位置找到country,再進一步找到分類标簽idx。

雙向RNN

Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
 從
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
算一遍
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
,再從
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
算一遍
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
,将相應的
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
拼接起來,最終得到的hidden=[
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
,
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)

]。

是否雙向在代碼中通過bidirectional = 1或2 展現。

 torch.nn.GRU()

1.次元

input:(seqlen, batch_size, hidden_size)

output:(seqlen, batch_size, hidden_size * nDirections)

hidden:(nLayers * nDirections, batch_size, hidden_size)

2.參數設定

 torch.nn.GRU(hidden_size, hidden_size, num_layers, bidirectional=_)

 torch.nn.utils.rnn.pack_padded_sequence()

1.作用:提升GRU的運作效率

2.用法:經過embadding之後的次元(seqlen, baych_size, hidden_size)

Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)
 随後通過rnn.pack_padded_sequence(embadding, seq_lengths)對非零的值進行打包
Pytorch深度學習實踐 第十三講 循環神經網絡(進階篇)

示例代碼:

import torch
import gzip
import csv
from torch.utils.data import DataLoader
import numpy as np
from torch.nn.utils import rnn
from torch.utils.data import Dataset
import time
import matplotlib.pyplot as plt
import math

#參數初始化
Hidden_size = 100
Batch_size = 256
Num_layers = 2
Num_epoch = 10
Num_chars = 128
use_gpu = False

class NameDataset(Dataset):
    '構造資料集'
    def __init__(self, is_train_set=True):
        super(NameDataset, self).__init__()
        filename = 'E:/image-dataset/names_sets/names_train.csv.gz' if is_train_set else 'E:/image-dataset/names_sets/names_test.csv.gz'
        with gzip.open(filename, 'rt') as f:
            reader = csv.reader(f)
            rows = list(reader) #rows=[(name,country),(name,country),...]
        self.names = [row[0] for row in rows]
        self.len = len(self.names)
        self.countries = [row[1] for row in rows]
        self.country_list = list(sorted(set(self.countries))) #經過去重、排序後得到country清單
        self.country_dict = self.getCountryDict() #用此函數将list轉成對應的dict,['country1':idx分類号,'country2':idx分類号,...]
        self.country_num = len(self.country_list)
    def __getitem__(self, index):
        '為了資料集能提供索引通路,傳回:索引對應的name字元串,索引對應的country的分類号'
        return self.names[index], self.country_dict[self.countries[index]]
    def __len__(self):
        return self.len
    def getCountryDict(self):
        country_dict = dict()
        for idx, country_name in enumerate(self.country_list, 0):
            country_dict[country_name] = idx
        return country_dict
    def idx2country(self,index):
        return self.country_list[index]
    def getCountryNum(self):
        return self.country_num

#資料準備
train_sets = NameDataset(is_train_set=True)
train_loader = DataLoader(train_sets, batch_size = Batch_size, shuffle=True)

test_sets = NameDataset(is_train_set=False)
test_loader = DataLoader(test_sets, batch_size=Batch_size, shuffle=False)
Num_country = train_sets.getCountryNum() #模型最終的分類類别數

def create_tensor(tensor):
    if use_gpu:
        device = torch.device("cuda:0")
        tensor = tensor.to(device)
    return tensor

def name2list(name):
    "讀取名字的每個字元 對應的 的ASC碼值,将名字list變成由ASC表示的清單.輸出元組:名字的ASC表示 和 名字長度"
    arr = [ord(c) for c in name]
    return arr, len(arr)

def make_tensors(names, countries):
    sequences_and_lengths = [name2list(name) for name in names]
    name_sequences = [sl[0] for sl in sequences_and_lengths]
    seq_lengths = torch.LongTensor([sl[1] for sl in sequences_and_lengths])
    countries = countries.long()

    # make tensor of name, Batchsize*seqlen
    seq_tensor = torch.zeros(len(name_sequences), seq_lengths.max()).long()
    for idx, (seq, seq_len) in enumerate(zip(name_sequences, seq_lengths), 0):
        seq_tensor[idx, :seq_len] = torch.LongTensor(seq) #idx對應的位置中,從0到name長度将name填進去

    #按照name長度進行排序
    seq_lengths, perm_idx = seq_lengths.sort(dim=0, descending=True)
    seq_tensor = seq_tensor[perm_idx] #将這兩個也按照相同的idx排序
    countries = countries[perm_idx]

    return create_tensor(seq_tensor),\
        create_tensor(seq_lengths),\
        create_tensor(countries)
class RNNClassifier(torch.nn.Module):
    '構造RNN分類器模型'
    def __init__(self, input_size, hidden_size, output_size, n_layers=1,bidirectional=True):
        super(RNNClassifier, self).__init__()
        self.hidden_size = hidden_size
        self.n_layers = n_layers
        self.directions = 2 if bidirectional else 1

        self.embadding = torch.nn.Embedding(input_size, hidden_size)
        self.gru = torch.nn.GRU(hidden_size, hidden_size, n_layers, bidirectional=bidirectional)
        self.fc = torch.nn.Linear(hidden_size * self.directions, output_size)
    def _init_hidden(self,batch_size):
        hidden = torch.zeros(self.n_layers * self.directions, batch_size, self.hidden_size)
        return create_tensor(hidden)
    def forward(self,input, seq_lengths):
        'input:所有序列,seq_lengths:每個序列的長度'
        input = input.t() #将input做轉置,由batch×seq_len到seq_len×batch
        batch_size = input.size(1) #用它來構造最初的隐層H0

        hidden = self._init_hidden(batch_size)
        embadding = self.embadding(input) #shape:(seq_len,batchsize,hiddensize)

        # 将序列按照長度降序打包
        gru_input = rnn.pack_padded_sequence(embadding, seq_lengths)

        output, hidden = self.gru(gru_input, hidden)
        if self.directions==2:
            hidden_cat = torch.cat([hidden[-1], hidden[-2]], dim=1)
        else:
            hidden_cat = hidden[-1]
        fc_output = self.fc(hidden_cat)
        return fc_output

def time_since(since):
    s = time.time()-since
    m = math.floor(s/60)
    s -= m * 60
    return '%dm %ds' % (m, s)
def trainModel():
    total_loss = 0
    for i, (names,countries) in enumerate(train_loader,1):
        inputs, seq_lengths, targets = make_tensors(names,countries)
        output = classifier(inputs, seq_lengths)
        loss = criterion(output, targets)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        total_loss += loss.item()
        if i % 10 == 0:
            print(f'[{time_since(start)}] Epoch {epoch}', end='')
            print(f'[{i * len(inputs)}/{len(train_sets)}]',end='')
            print(f'loss={total_loss / (i * len(inputs))}')
    return total_loss
def testModel():
    correct = 0
    total = len(test_sets)
    print('evaluating trained model...')
    with torch.no_grad():
        for i, (names, countries) in enumerate(test_loader, 1):
            inputs, seq_lengths, targets = make_tensors(names, countries)
            output = classifier(inputs, seq_lengths)
            pred = output.max(dim=1, keepdim=True)[1]
            correct += pred.eq(targets.view_as(pred)).sum().item()
        percent = '%.2f' % (100 * correct / total)
        print(f'Test set:Accuracy {correct} / {total}  {percent}%')
    return correct / total
#參數初始化

if __name__=="__main__":
    classifier = RNNClassifier(Num_chars, Hidden_size, Num_country, Num_layers)
    if use_gpu:
        device = torch.device('cuda:0')
        classifier.to(device)

    criterion = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(classifier.parameters(), lr=0.001)

    start = time.time()
    print('Training for %d epochs...' % Num_epoch)
    acc_list = []
    for epoch in range(1,Num_epoch+1):
        trainModel()
        acc = testModel()
        acc_list.append(acc)

    epoch = np.arange(1, len(acc_list) + 1, 1)
    acc_list = np.array(acc_list)
    plt.plot(epoch, acc_list)
    plt.xlabel('Epoch')
    plt.ylabel('Accuracy')
    plt.grid()
    plt.show()
           

說明:m.sort(dim = _, descending = _),dim=0或1,0是按列排序,1是按行排序;descending=True是由大到小,false是由小到大。最後傳回:1.排序後的序列;2.排序後對應的原來的idx。

繼續閱讀