欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lstm - pytorch

程序员文章站 2024-03-24 23:32:22
...

1 lstm做分类

2 lstm做回归

3 lstm的参数输入

 

1 图像分类任务:

train_loader中取出来的变量维度: torch.Size([64, 1, 28, 28])

需要将其改成:torch.Size([64, 28, 28]) 代表batchsize , height, weight 

lstm的参数设置:

input: 形状的输入(seq_len,batch,input_size)

h_0 :形状(num_layers * num_directions,batch,hidden_​​size):张量,包含批次中每个元素的初始隐藏状态。如果LSTM是双向的,则num_directions应该为2,否则应为1。

c_0 :形状(num_layers * num_directions,batch,hidden_​​size):张量,包含批次中每个元素的初始单元状态。

如果未提供(h_0,c_0),则h_0c_0均默认为零。

batch_first :如果为True,则输入和输出张量按(batch,seq,feature)提供

output:形状的输出(seq_len,batch,num_directions * hidden_​​size):张量,包含每个t的LSTM最后一层的输出特征(h_t)。如果已将a 用作输入,则输出也将是打包序列

import torch
from torch import nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import os

# torch.manual_seed(1)    # reproducible

EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
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 = False
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
    # not mnist dir or mnist is empyt dir
    DOWNLOAD_MNIST = True

# Mnist digital dataset
train_data = dsets.MNIST(
    root='./mnist/',
    train=True,                         # this is training data
    transform=transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
                                        # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
    download=DOWNLOAD_MNIST,            # download it if you don't have it
)

# plot one example
print(train_data.train_data.size())     # (60000, 28, 28)
print(train_data.train_labels.size())   # (60000)
plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
plt.title('%i' % train_data.train_labels[0])
plt.show()

# Data Loader for easy mini-batch return in training
train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)

# convert test data into Variable, pick 2000 samples to speed up testing
test_data = dsets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor())
test_x = test_data.test_data.type(torch.FloatTensor)[:2000]/255.   # shape (2000, 28, 28) value in range(0,1)
test_y = test_data.test_labels.numpy()[:2000]    # covert to numpy array

input = torch.randn(5, 30, 100)   # seq_len,batch,input_size
h0 = torch.randn(4, 30, 200)      # num_layers * num_directions,batch,hidden_​​size
c0 = torch.randn(4, 30, 200)      # num_layers * num_directions,batch,hidden_​​size
rnn = nn.LSTM(100, 200, 4)         # input_size , hidden_​​size ,num_layers
output, (hn, cn) = rnn(input, (h0, c0)) #seq_len,batch,num_directions * hidden_​​size
print(output.shape)

class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()
        self.rnn = nn.LSTM(         # if use nn.RNN(), it hardly learns
            input_size=28,
            hidden_size=64,         # rnn hidden unit
            num_layers=3,           # number of rnn layer
            batch_first=True,       # input & output will has batch size as 1s dimension. e.g. (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)
        print(x.shape)
        r_out, (h_n, h_c) = self.rnn(x, None)   # None represents zero initial hidden state
        print(r_out.shape)
        # 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)   # optimize all cnn parameters
loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted

# training and testing
for epoch in range(EPOCH):
    for step, (b_x, b_y) in enumerate(train_loader):        # gives batch data
        b_x = b_x.view(-1, 28, 28)              # reshape x to (batch, time_step, input_size)

        output = rnn(b_x)                               # rnn output
        loss = loss_func(output, b_y)                   # cross entropy loss
        optimizer.zero_grad()                           # clear gradients for this training step
        loss.backward()                                 # backpropagation, compute gradients
        optimizer.step()                                # apply gradients

        if step % 50 == 0:
            test_output = rnn(test_x)                   # (samples, time_step, input_size)
            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)

# print 10 predictions from test data
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')

2 Lstm回归

import torch
from torch import nn
import numpy as np
import matplotlib.pyplot as plt

TIME_STEP = 10      # rnn time step
INPUT_SIZE = 1      # rnn input size
LR = 0.02           # learning rate

# show data
# steps = np.linspace(0, np.pi*2, 100, dtype=np.float32)  # float32 for converting torch FloatTensor
# x_np = np.sin(steps)
# y_np = np.cos(steps)
# plt.plot(steps, y_np, 'r-', label='target (cos)')
# plt.plot(steps, x_np, 'b-', label='input (sin)')
# plt.legend(loc='best')
# plt.show()


class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()

        self.rnn = nn.RNN(
            input_size=1,
            hidden_size=32,     # rnn hidden unit
            num_layers=1,       # number of rnn layer
            batch_first=True,   # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
        )
        self.out = nn.Linear(32, 1)

    def forward(self, x, h_state):
        # x (batch, time_step, input_size)
        # h_state (n_layers, batch, hidden_size)
        # r_out (batch, time_step, hidden_size)
        r_out, h_state = self.rnn(x, h_state)     # output: torch.Size([1, 10, 32])

        # print('r_out:' , r_out.shape)
        # outs = []    # save all predictions
        # L1= r_out.size(1)
        # for time_step in range(L1):    # calculate output for each time step
        #     tmp1 = r_out[:, time_step, :]
        #     tmp2 = self.out(tmp1)
        #     outs.append(tmp2)
        #
        # outx = torch.stack(outs, dim=1)
        # return outx , h_state

        # instead, for simplicity, you can replace above codes by follows
        r_out = r_out.view(-1, 32)
        outs = self.out(r_out)
        outs = outs.view(-1, TIME_STEP, 1)
        return outs, h_state
        
        # or even simpler, since nn.Linear can accept inputs of any dimension 
        # and returns outputs with same dimension except for the last
        # outs = self.out(r_out)
        # return outs

rnn = RNN()
print(rnn)

optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)   # optimize all cnn parameters
loss_func = nn.MSELoss()

h_state = None      # for initial hidden state

# plt.figure(1, figsize=(12, 5))
# plt.ion()           # continuously plot

for step in range(100):
    start, end = step * np.pi, (step+1)*np.pi   # time range
    steps = np.linspace(start, end, TIME_STEP, dtype=np.float32, endpoint=False)  # float32 for converting torch FloatTensor
    x_np = np.sin(steps)
    y_np = np.cos(steps)

    x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis])    # shape (batch, time_step, input_size)
    # print('x:',x.shape)
    y = torch.from_numpy(y_np[np.newaxis, :, np.newaxis])
    # print('y:',y.shape)
    prediction, h_state = rnn(x, h_state)   # rnn output
    # print(prediction.shape)
    # !! next step is important !!
    h_state = h_state.data        # repack the hidden state, break the connection from last iteration

    loss = loss_func(prediction, y)         # calculate loss
    optimizer.zero_grad()                   # clear gradients for this training step
    loss.backward()                         # backpropagation, compute gradients
    optimizer.step()                        # apply gradients

    print('step: ',  step, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % 1)

    # plotting
    # plt.plot(steps, y_np.flatten(), 'r-')
    # plt.plot(steps, prediction.data.numpy().flatten(), 'b-')
    # plt.draw(); plt.pause(0.05)

plt.ioff()
plt.show()

 

 

 

相关标签: 音频