Pytorch_RNN实例代码(低级篇)

本文旨在代码实现,具体内容讲解请参考刘老师的视频:(https://www.bilibili.com/video/BV1Y7411d7Ys?p=13)

构建RNNCell模型以及直接调用RNN的代码如下:

import torch
input_size=4
hidden_size=4
batch_size=1
num_layers=1
idx2char=['e','h','l','o']
x_input=[1,0,2,2,3]
y_output=[3,1,2,3,2]
one_hot_lookup=[[0,1,0,0],
                [1,0,0,0],
                [0,0,1,0],
                [0,0,0,1]]
x_ont_hot=[one_hot_lookup[x] for x in x_input]
inputs=torch.Tensor(x_ont_hot).view(-1,batch_size,input_size)
labers=torch.LongTensor(y_output).view(-1,1)
       #RNNcell模块
class Model(torch.nn.Module):
    def __init__(self,input_size,hidden_size,batch_size):
        super(Model,self).__init__()
        self.input_size=input_size
        self.hidden_size=hidden_size
        self.batch_size=batch_size
        self.runcell=torch.nn.RNNCell(input_size=self.input_size,hidden_size=
                                      self.hidden_size)
    def forward(self,inputs,hidden):
        hidden=self.runcell(inputs,hidden)
        return hidden
    def init_hidden(self):
        return torch.zeros(self.batch_size,self.hidden_size)
net=Model(input_size,hidden_size,batch_size)
criterion = torch.nn.CrossEntropyLoss()
optimizer =torch.optim.Adam(net.parameters(),lr=0.1)
for epoch in range(15):
    loss=0
    optimizer.zero_grad()
    hidden=net.init_hidden()
    print('predict string:',end='')
    for inp,laber in zip(inputs,labers):
        hidden=net(inp,hidden)
        loss+=criterion(hidden,laber)
        _,idx=hidden.max(dim=1)
        print(idx2char[idx.item()],end='')
    loss.backward()
    optimizer.step()
    print(',Epoch [%d/15] loss=%.4f' %(epoch+1,loss.item()))
   
#RNN模块
'''seq_len=5
inputs=torch.Tensor(x_ont_hot).view(seq_len,batch_size,input_size)
labers=torch.LongTensor(y_output)
class Model(torch.nn.Module):
    def __init__(self,input_size,hidden_size,batch_size,num_layers=1):
        super(Model,self).__init__()
        self.num_layers=num_layers
        self.input_size=input_size
        self.hidden_size=hidden_size
        self.batch_size=batch_size
        self.rnn=torch.nn.RNN(input_size=self.input_size,hidden_size=
                                      self.hidden_size,num_layers=num_layers)
    def forward(self,inputs):
        hidden=torch.zeros(self.num_layers,self.batch_size,self.hidden_size)
        out,_=self.rnn(inputs,hidden)
        return out.view(-1,self.hidden_size)
net=Model(input_size,hidden_size,batch_size,num_layers)
criterion = torch.nn.CrossEntropyLoss()
optimizer =torch.optim.Adam(net.parameters(),lr=0.05)
for epoch in range(15):
    optimizer.zero_grad()
    outputs=net(inputs)
    loss=criterion(outputs,labers)
    loss.backward()
    optimizer.step()
    _,idx=outputs.max(dim=1)
    idx=idx.data.numpy()
    print('predict string:',''.join([idx2char[x] for x in idx]),end='')
    print(',Epoch [%d/15] loss=%.3f' %(epoch+1,loss.item()))
    '''

下面的RNN_Embedding模型可以将高维稀疏的向量转变为低维稠密的向量:

import torch
num_class=4
input_size=4
hidden_size=8
embedding_size=10
batch_size=1
num_layers=2
seq_len=5
x_input=[[1,0,2,2,3]]
y_output=[3,1,2,3,2]
idx2char=['e','h','l','o']
inputs=torch.LongTensor(x_input)
labers=torch.LongTensor(y_output)        
class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.emb=torch.nn.Embedding(input_size,embedding_size)
        self.rnn=torch.nn.RNN(input_size=embedding_size,hidden_size=
                                      hidden_size,num_layers=num_layers,
                                      batch_first=True)
        self.fc=torch.nn.Linear(hidden_size,num_class)
    def forward(self,x):
        hidden=torch.zeros(num_layers,x.size(0),hidden_size)
        x=self.emb(x)
        x,_=self.rnn(x,hidden)
        x=self.fc(x)
        return x.view(-1,num_class)       
net=Model()
criterion = torch.nn.CrossEntropyLoss()
optimizer =torch.optim.Adam(net.parameters(),lr=0.05)
for epoch in range(15):
    optimizer.zero_grad()
    outputs=net(inputs)
    loss=criterion(outputs,labers)
    loss.backward()
    optimizer.step()
    _,idx=outputs.max(dim=1)
    idx=idx.data.numpy()
    print('predict string:',''.join([idx2char[x] for x in idx]),end='')
    print(',Epoch [%d/15] loss=%.3f' %(epoch+1,loss.item()))
上一篇:谁来接棒深度学习?


下一篇:【RNN实战进阶】手把手教你如何预测当天股票的最高点