在序列数据中,处理数据过大,linear层比卷积核的运算类要大很多
h0先验条件,如果用于图像生成文本,可以在h0前面加上cnn+fc
g:三维到五维(h0三维,输出五维),本质上是线性层
用tanh是因为取值在+1和-1之间
只需要输入特征数,和输出特征数就行了,因为本质上是一个线性层
因为文字非数字,无法计算,因此需要转换
inputsize最后一个表格的列数
输入向量是一个维度是4的独热向量,输出向量也是个维度是4的概率向量
seq_len序列长度(x1, x2, x3)
input_size输入数据每一个(x1)都是一个四维的向量
hidden_size每个隐层都是有两个元素
要把inputs和labels重新view,-1为自适应
inputs的格式为(seqlen, batchsize, inputsize)
lables的格式为 (seqlen,1)
seqlen其实就是循环次数
import torch
input_size = 4
hidden_size = 4
batch_size = 1
idx2char = ['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()))
改变了out的维度
改变了lables的维度
import torch
input_size = 4
hidden_size = 4
batch_size = 1
seq_len = 5
idx2char = ['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
改变网络结构