Bilibili-刘二大人《Pytorch深度学习实践》第13讲 Advanced RNN实验代码

'''''''''
构建一个RNN分类器
任务:一个名称分类器,根据输入的名字判断其国籍,数据集有Name与Country
在这个场景中,由于输出无法通过线性层映射到某个维度,所以可以只用hn来连接线性层,对这个输入做一个18维的分类
'''''''''
import csv
import gzip
import torch
import matplotlib.pyplot as plt
import numpy as np
from torch.nn.utils.rnn import pack_padded_sequence
from torch.utils.data import Dataset, DataLoader

device = torch.device('cuda:0')

HIDDEN_SIZE = 100
BATCH_SIZE = 256
N_LAYER = 2
N_EPOCHS = 100
N_CHARS = 128 # 字符集字典维度

class NameDataset(Dataset): #数据集类
    def __init__(self,is_train_set = True):
        filename = 'F:\\mnist\\names_train.csv.gz' if is_train_set else 'F:\\mnist\\names_test.csv.gz'
        with gzip.open(filename,'rt') as f:
            reader = csv.reader(f)
            rows = list(reader)
        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))) # unique国家列表
        self.country_dict = self.getCountryDict()
        self.country_num = len(self.country_list) # unique国家数
    def __getitem__(self,index):
        return self.names[index],self.country_dict[self.countries[index]]
        # 返回名称和国家,国家先通过index找到国家,再通过字典映射返回国家的序号
    def __len__(self):
        return self.len
    def getCountryDict(self): # 把unique国家做成字典
        country_dict = dict()
        for idx,counrty_name in enumerate(self.country_list,0):
            country_dict[counrty_name] = idx
        return country_dict
    def idx2country(self,index):
        return self.country_list[index]
    def getCountriesNum(self):
        return self.country_num # 返回unique国家数

trainset = NameDataset(is_train_set=True)
trainloader = DataLoader(trainset,batch_size=BATCH_SIZE,shuffle=True)
testset = NameDataset(is_train_set=False)
testloader = DataLoader(testset,batch_size=BATCH_SIZE,shuffle=False)

N_COUNTRY = trainset.getCountriesNum()
class RNNClassifier(torch.nn.Module):
    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.n_directions = 2 if bidirectional else 1

        self.embedding = torch.nn.Embedding(input_size,hidden_size) # inputs_size是名称字符集长度
        self.gru = torch.nn.GRU(hidden_size,hidden_size,n_layers,bidirectional=bidirectional)
        self.fc = torch.nn.Linear(hidden_size*self.n_directions,output_size) # output_size是N_COUNTRY

    def _init_hidden(self,batch_size):
        hidden = torch.zeros(self.n_layers*self.n_directions,batch_size,self.hidden_size)
        #layers*batch_size*hidden_size
        return hidden.to(device)

    def forward(self,input,seq_lengths):
        input = input.t() # b*s to s*b
        batch_size = input.size(1)
        hidden = self._init_hidden(batch_size)
        embedding = self.embedding(input)

        gru_input = pack_padded_sequence(embedding,seq_lengths.cpu())
        # 这是gru和lstm可以接受的一种输入, PackedSequence object
        output,hidden = self.gru(gru_input,hidden)
        if self.n_directions == 2:
            hidden_cat = torch.cat((hidden[-1],hidden[-2]),dim=1)
        else:
            hiddden_cat = hidden[-1]
        fc_output = self.fc(hidden_cat)
        return fc_output

def name2list(name):
    arr = [ord(c) for c in name]
    return arr,len(arr)
    # 返回输入名字的asci码值的列表,和名字长度
def make_tensors(names,countries): # 把输入数据处理为tensor
    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]) # seq_lengths to longTensor
    countries = countries.long() # index of country to longTensor
    # make tensor of name, batchsize*seqlen
    seq_tensor = torch.zeros(len(name_sequences),seq_lengths.max()).long()
    # 这一句先生成一个二维全0张量,高是名称序列数,宽是最长的名字长度,做成longTensor
    for idx,(seq,seq_len) in enumerate(zip(name_sequences,seq_lengths),0):
        seq_tensor[idx, :seq_len] = torch.LongTensor(seq) # 复制到上面的全0张量中
    # sort sequences by length to use pack_padded_sequence
    seq_lengths,perm_idx = seq_lengths.sort(dim=0,descending=True)
    # torch中tensor类的tensor返回的是排完序列和索引
    seq_tensor = seq_tensor[perm_idx]
    countries = countries[perm_idx]
    return seq_tensor.to(device),seq_lengths.to(device),countries.to(device)

classifier = RNNClassifier(N_CHARS,HIDDEN_SIZE,N_COUNTRY,N_LAYER,True).to(device)
#classifier = classifier.to(device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(classifier.parameters(),lr=0.001)
#start = time.time()

def trainModel():
    total_loss = 0
    for i,(names,countries) in enumerate(trainloader,1): # i从1开始数
        inputs,seq_lengths,target = make_tensors(names,countries)
        output = classifier(inputs,seq_lengths) # 这是送的forward的参数.
        loss = criterion(output,target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        total_loss += loss.item()
        if i%10 == 0:
            print(f'Epoch{epoch}',end='')
            print(f'[{i*len(inputs)}/{len(trainset)}]',end='')
            print(f'loss={total_loss/(i*len(inputs))}')
    return total_loss

def testModel():
    correct = 0
    total = len(testset)
    print('evaluating trained model ...')
    with torch.no_grad():
        for i,(names,countries) in enumerate(testloader,1):
            inputs,seq_lengths,target = make_tensors(names,countries)
            output = classifier(inputs,seq_lengths)
            pred = output.max(dim=1,keepdim=True)[1]
            correct += pred.eq(target.view_as(pred)).sum().item()
        percent = '%.2f' % (100*correct/total)
        print(f'Test set: Accuracy{correct}/{total} {percent}%')
    return  correct/total



print('Training for %d epochs...' % N_EPOCHS)
acc_list = []
for epoch in range(1,30):
    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()


你可能感兴趣的:(nlp,深度学习,gru)