用LSTM来做下那个预测问题
LSTM要初始化的参数:input_size,hidden_size,num_layers,batch_first
import torch
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb = torch.nn.Embedding(input_size, embedding_size)
self.lstm = torch.nn.LSTM(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)
# 因为LSTM输入限制,要多弄一个c的输入,和hidden一起放进去
c = torch.zeros(num_layers, x.size(0), hidden_size)
x = self.emb(x) #这样就可以变成稠密的向量
x, _ = self.lstm(x, (hidden, c))
x = self.fc(x)
return x.view(-1,num_class)
#一堆参数
num_class = 4
input_size = 4
hidden_size = 8
embedding_size = 10
num_layers = 2
batch_size = 1
seq_len = 5
#准备数据
idx2char = ['e', 'h', 'l', 'o']
x_data = [[1, 0, 2, 2, 3]] # (batch, seq_len)
y_data = [3, 1, 2, 3, 2] # (batch * seq_len)
inputs = torch.LongTensor(x_data).view(batch_size, seq_len)
labels = torch.LongTensor(y_data)
#实例化模型并构造损失函数和优化器
net = Model()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.05)
#训练
for epoch in range(20):
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
_, idx = outputs.max(dim=1)
idx = idx.data.numpy()
print('Predicted string:', ''.join([idx2char[x] for x in idx]), end='')
print(', Epoch[%d/20] loss=%.4f' % (epoch+1, loss.item()))
注意,LSTM的输入是input,(h_0,c_0),所以要再多弄一个c_0
用GRU做
GRU要初始化的参数:input_size,hidden_size
import torch
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb = torch.nn.Embedding(input_size, embedding_size)
self.gru = torch.nn.GRU(embedding_size, hidden_size)
self.fc = torch.nn.Linear(hidden_size, num_class)
def forward(self, x):
hidden = torch.zeros(num_layers, x.size(1), hidden_size)
x = self.emb(x)
x, _ = self.gru(x, hidden)
x = self.fc(x)
return x.view(-1, num_class)
num_class = 4
input_size = 4
hidden_size = 8
embedding_size = 10
num_layers = 1
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]
inputs = torch.LongTensor(x_data).view(batch_size, seq_len)
labels = torch.LongTensor(y_data)
net = Model()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.05)
for epoch in range(20):
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
_, idx = outputs.max(dim=1)
idx = idx.data.numpy()
print('Predicted string:', ''.join([idx2char[x] for x in idx]), end='')
print(', Epoch[%d/20] loss=%.4f' % (epoch+1, loss.item()))
这里不知道为啥要改成x.size(1),而且最后的结果都不是想要的结果。。。