1.下载数据集
和CNN数据集一致,不多叙述
import torch
from torch import nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
# torch.manual_seed(1) #设置种子
# 超参
EPOCH = 1 # 迭代一次
BATCH_SIZE = 64
TIME_STEP = 28 # rnn time step / image height
INPUT_SIZE = 28 # rnn input size / image width
LR = 0.01 # learning rate
DOWNLOAD_MNIST = True # 下载数据集
train_data=dsets.MNIST(
root="./mnits/",
train=True,
transform=transforms.ToTensor(),
download=DOWNLOAD_MNIST,
)
#采用mini——batch
# mini——batch
train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
# 下载test数据
test_data = dsets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor())
test_x = test_data.test_data.type(torch.FloatTensor)[:2000]/255.
test_y = test_data.test_labels.numpy()[:2000] #取前2000个
print(train_data.train_data.size()) # (60000, 28, 28)
print(train_data.train_labels.size()) # (60000)
plt.imshow(train_data.train_data[3].numpy(),cmap="gray")
plt.title("%i"%train_data.train_labels[0])
plt.show()
LSTM(args, * kwargs)
input_size – 输入的特征维度
hidden_size – 隐状态的特征维度
num_layers – 层数(和时序展开要区分开)
bias – 如果为False,那么LSTM将不会使用
b
i
h
,
b
h
h
b_{ih},b_{hh}
bih,bhh,默认为True。
batch_first – 如果为True,那么输入和输出Tensor的形状为(batch, seq, feature)
dropout – 如果非零的话,将会在RNN的输出上加个dropout,最后一层除外。
bidirectional – 如果为True,将会变成一个双向RNN,默认为False。
LSTM输入: input, (h_0, c_0)
input (seq_len, batch, input_size): 包含输入序列特征的Tensor。也可以是packed variable
h_0 (num_layers * num_directions, batch, hidden_size):保存着batch中每个元素的初始化隐状态的Tensor
c_0 (num_layers * num_directions, batch, hidden_size): 保存着batch中每个元素的初始化细胞状态的Tensor
LSTM输出 output, (h_n, c_n)
output (seq_len, batch, hidden_size * num_directions): 保存RNN最后一层的输出的Tensor。
h_n (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的隐状态。
c_n (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的细胞状态。
class RNN(nn.Module):
def __init__(self):
super(RNN, self).__init__()
self.rnn = nn.LSTM(
input_size=INPUT_SIZE,
hidden_size=64, # rnn hidden unit
num_layers=1, # number of rnn layer
batch_first=True, # batch是第一维度. (batch, time_step, input_size)
)
self.out = nn.Linear(64, 10)
def forward(self, x):
# x shape (batch, time_step, input_size)
# r_out shape (batch, time_step, output_size)
# h_n shape (n_layers, batch, hidden_size)
# h_c shape (n_layers, batch, hidden_size)
r_out, (h_n, h_c) = self.rnn(x, None) # None代表hidden units初始值是0
# choose r_out at the last time step
out = self.out(r_out[:, -1, :])
return out
rnn = RNN()
print(rnn)
optimizer=torch.optim.Adam(rnn.parameters(),lr=LR)
loss_func=nn.CrossEntropyLoss()
for epoch in range(EPOCH):
for step,(b_x,b_y) in enumerate(train_loader):
b_x=b_x.view(-1,28,28)
output=rnn(b_x)
loss=loss_func(output,b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step%5==0:
test_output=rnn(test_x)
pred_y=torch.max(test_output,1)[1].data.numpy()
accuracy=float((pred_y==test_y).astype(int).sum())/float(test_y.size)
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
test_output=rnn(test_x[:10].view(-1,28,28))
pred_y=torch.max(test_output,1)[1].data.numpy()
print(pred_y, 'prediction number')
print(test_y[:10], 'real number')
上传一部分截图,准确率在明显的提升。
初学者包括我可能不是很懂LSTM的原理,我特地找了张图,内部结构可以参考吴恩达老师深度学习,一系列公式都在,我这里只讲输出,r_out, (h_n, h_c) = self.rnn(x, None)
out输出,(副线,主线),依次是上图的向上的ht,(ht(指向下一个Cell),Ct),一位out的形状是(batch,time_step,input_size),只要最后一个timestep的输出,所以是self.out(out[:,-1,:])
————————————————————————————————————————————————
我是一名机器学习的初学者,是万千小白中努力学习中的一员