递归神经网络的及其不同的实现,例如长短期记忆网络LSTM以及门控循环单元gated recurrent unit GRU,它们为大多数深度学习模型提供文本和序列化数据,为序列化数据使用一维卷积。
(1)实现一个rnn模型
import torch.nn as nn
from torch.autograd import Variable
class RNN(nn.Module):
def __init__(self,input_size, hidden_size, output_size):
super(RNN, self).__init__():
self.hidden_size = hidden_size
self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
self.i2o = nn.Linear(input_size + hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden):
combined = torch.cat((input, hidden), 1)
hidden = self.i2h(combined)
output = self.i20(combined)
output = self.softmax(output)
return output, hidden
def initHidden(self):
return Variable(torch.zeros(1, self.hidden_size))