目录
1.RNNCell
2.RNN
3.RNN_Embedding
1.RNNCell
import torchinput_size = 4
hidden_size = 4
batch_size = 1idx2char = ['e', 'h', 'l', 'o']
x_data = [1, 0, 2, 2, 3] # 输入:hello
y_data = [3, 1, 2, 3, 2] # 期待:ohlol# 独热向量
one_hot_lookup = [[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]]
x_one_hot = [one_hot_lookup[x] for x in x_data]inputs = torch.Tensor(x_one_hot).view(-1, batch_size, input_size) # (seqLen,batchSize,inputSize)
labels = torch.LongTensor(y_data).view(-1, 1) # (seqLen,1)class Model(torch.nn.Module):def __init__(self, input_size, hidden_size, batch_size):super(Model, self).__init__()self.batch_size = batch_sizeself.input_size = input_sizeself.hidden_size = hidden_sizeself.rnncell = torch.nn.RNNCell(input_size=self.input_size,hidden_size=self.hidden_size)def forward(self, input, hidden):hidden = self.rnncell(input, hidden) # input:(batch, input_size) hidden:(batch, hidden_size)return hiddendef 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 = 0optimizer.zero_grad()hidden = net.init_hidden()print('Predicted string: ', end='')for input, label in zip(inputs, labels):hidden = net(input, hidden)loss += criterion(hidden, label)idx = torch.argmax(hidden, dim=1)print(idx2char[idx.item()], end='')loss.backward()optimizer.step()print(', Epoch [%d/15] loss=%.4f' % (epoch+1, loss.item()))
2.RNN
import torchinput_size = 4 # 输入的维度,例如hello为四个字母表示,其维度为四
hidden_size = 4 # 隐藏层维度
num_layers = 1 # number of layers
batch_size = 1
seq_len = 5idx2char = ['e', 'h', 'l', 'o']
x_data = [1, 0, 2, 2, 3] # 输入:hello
y_data = [3, 1, 2, 3, 2] # 期待:ohlol# 独热向量
one_hot_lookup = [[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]]
x_one_hot = [one_hot_lookup[x] for x in x_data]inputs = torch.Tensor(x_one_hot).view(seq_len, batch_size, input_size)
labels = torch.LongTensor(y_data) # (seqSize*batchSize, 1)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_layersself.batch_size = batch_sizeself.input_size = input_sizeself.hidden_size = hidden_sizeself.rnn = torch.nn.RNN(input_size=self.input_size,hidden_size=self.hidden_size,num_layers=num_layers)def forward(self, input):hidden = torch.zeros(self.num_layers,self.batch_size,self.hidden_size) # (numLayers, batchSize, hiddenSize)out, hidden_last = self.rnn(input, hidden) # out:(seqLen, batchSize, hiddenSize), hidden_last:最后一个hiddenreturn out.view(-1, self.hidden_size) # (seqLen×batchSize, hiddenSize)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, labels)loss.backward()optimizer.step()idx = torch.argmax(outputs, dim=1)print('Predicted string: ', ''.join([idx2char[i.item()] for i in idx]), end='')print(', Epoch [%d/15] loss = %.3f' % (epoch + 1, loss.item()))
3.RNN_Embedding
import torch# parameters
num_class = 4 # 引入线性层,不用不必要求一个输入就有一个输出,可以多个
input_size = 4
hidden_size = 8
embedding_size = 10
num_layers = 2
batch_size = 1
seq_len = 5idx2char = ['e', 'h', 'l', 'o']
x_data = [[1, 0, 2, 2, 3]] # (batch:1, seq_len:5)
y_data = [3, 1, 2, 3, 2] # (batch * seq_len)
inputs = torch.LongTensor(x_data)
labels = torch.LongTensor(y_data)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)# batchSize在第一位: (batchSize:一共几个句子, seqLen:每个句子有几个单词, inputSize:每个单词有多少特征)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) # (batch, seqLen, embeddingSize), 输入数据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, labels)loss.backward()optimizer.step()idx = torch.argmax(outputs, dim=1)print('Predicted string: ', ''.join([idx2char[i.item()] for i in idx]), end='')print(', Epoch [%d/15] loss = %.3f' % (epoch + 1, loss.item()))