以往的网络DNN(Dense稠密网络)
在序列数据中,处理数据过大,linear层比卷积核的运算类要大很多
RNN模型
h0先验条件,如果用于图像生成文本,可以在h0前面加上cnn+fc
g:三维到五维(h0三维,输出五维),本质上是线性层
用tanh是因为取值在+1和-1之间
pytorch中的RNN Cell
只需要输入特征数,和输出特征数就行了,因为本质上是一个线性层
RNN的输入即输出
利用numlayers构建多层
例子
step1 按字母序建立词典再转换为独热向量
因为文字非数字,无法计算,因此需要转换
inputsize最后一个表格的列数
输入向量是一个维度是4的独热向量,输出向量也是个维度是4的概率向量
step2 loss
参数设置
seq_len序列长度(x1, x2, x3)
input_size输入数据每一个(x1)都是一个四维的向量
hidden_size每个隐层都是有两个元素
数据定义
要把inputs和labels重新view,-1为自适应
inputs的格式为(seqlen, batchsize, inputsize)
lables的格式为 (seqlen,1)
seqlen其实就是循环次数
代码,将hello变成ohlol(RNNCell)
import torchinput_size = 4hidden_size = 4batch_size = 1idx2char = ['e', 'h', 'l', 'o']x_data = [1, 0, 2, 2, 3]y_data = [3, 1, 2, 3, 2]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)lables = torch.LongTensor(y_data).view(-1, 1)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.rnncell = torch.nn.RNNCell(self.input_size, self.hidden_size) def forward(self, inputs, hidden): hidden = self.rnncell(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 hidden = net.init_hidden() print('Predicted string: ', end='\n') for input, lable in zip(inputs, lables): hidden = net(input, hidden) loss += criterion(hidden, lable) _, idx = hidden.max(dim=1) print(idx2char[idx.item()], end='') optimizer.zero_grad() loss.backward() optimizer.step() print(', Epoch[%d/15] loss = %.4f' %(epoch + 1, loss.item()))
代码,采用pytorch中的RNN
改变了out的维度
改变了lables的维度
import torchinput_size = 4hidden_size = 4batch_size = 1seq_len = 5idx2char = ['e', 'h', 'l', 'o']x_data = [1, 0, 2, 2, 3]y_data = [3, 1, 2, 3, 2]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)lables = torch.LongTensor(y_data)class Model(torch.nn.Module): def __init__(self, input_size, hidden_size, batch_size, num_layers=1): super(Model, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.batch_size = batch_size self.num_layers = num_layers self.rnn = torch.nn.RNN(input_size=self.input_size, hidden_size=self.hidden_size, num_layers=self.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)criterion = torch.nn.CrossEntropyLoss()optimizer = torch.optim.Adam(net.parameters(), lr=0.1)for epoch in range(15): optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, lables) loss.backward() optimizer.step() _, idx = outputs.max(dim=1) idx = idx.data.numpy() print('Predicted: ', ''.join([idx2char[x] for x in idx]), end='') print(',Epoch [%d / 15] loss = %.3f' %(epoch + 1, loss.item()))
采用Embedding vectors
独热向量降维为Embedding vectors
四维转换为5维
例如原来维度中的第二个,找到第二行,然后输出就行了
改变网络结构