机器学习周报(9.30-10.6)

摘要

进一步学习了循环神经网络(RNN)原理、种类、优缺点,使用Pytorch中所提供的RNN模型,并根据公式进行了手写实现RNN的计算原理。继续补充学习pytorch中其他函数的使用

Abstract

I further learn the principle, types, advantages and disadvantages of recurrent neural network (RNN), use the RNN model provided in Pytorch, and implement the calculation principle of RNN by hand according to the formula. Continue to supplement the use of other functions in pytorch

1 RNN

优点:

  • 可以处理变长序列
  • 模型大小与序列长度无关
  • 计算量与序列长度呈线性增长
  • 考虑历史信息
  • 便于流氏输出
  • 权重在每一时刻都不改变

缺点:

  • 串行计算比较慢
  • 无法获取太长的历史信息

1. one to n
在这里插入图片描述

还有一种结构是把输入信息X作为每个阶段的输入:

在这里插入图片描述

这种 one-to-n 的结构可以处理的问题有:

  • 从图像生成文字(image caption),此时输入的X就是图像的特征,而输出的y序列就是一段句子,就像看图说话等
  • AI诗歌(歌词)生成

2. n to one

要处理的问题输入是一个序列,输出是一个单独的值而不是序列,

在这里插入图片描述

这种结构通常用来处理序列分类问题,如:文本情感分类;输入一段文字判别它所属的类别,输入一个句子判断其情感倾向,输入一段视频并判断它的类别等

3. n to n

最经典的RNN结构,输入、输出都是等长的序列数据。
在这里插入图片描述

该结构可用于词法识别等问题领域

4. n to m

输入、输出为不等长的序列。

这种结构是Encoder-Decoder模型,也叫Seq2Seq模型,是RNN的一个重要变种

在这里插入图片描述

在这里插入图片描述

机器翻译

该结构可应用于:

  • 机器翻译:Encoder-Decoder的最经典应用,事实上这结构就是在机器翻译领域最先提出的。
  • 文本摘要:输入是一段文本序列,输出是这段文本序列的摘要序列。
  • 阅读理解:将输入的文章和问题分别编码,再对其进行解码得到问题的答案。
  • 语音识别:输入是语音信号序列,输出是文字序列。

1.1 使用pytorch RNN

隐含状态计算函数:
在这里插入图片描述

x t x_t xt当前时刻的输入
w i h w_{ih} wih对当前时刻输入 x t x_t xt的权重
b i h b_{ih} bih对当前舒克输入 x t x_t xt的偏置值
h t − 1 h_{t-1} ht1上一时刻的输出(隐含状态)
w h h w_{hh} whh对上一时刻输出的权重
b h h b_{hh} bhh对上一时刻输出的偏置值

每一时刻的输出(隐含状态) h t h_t ht,不仅和当前的输入 x t x_t xt有关,还跟上一时刻的隐含状态 h t − 1 h_{t-1} ht1有关; h 0 h_0 h0表示初始的隐含状态

实例化RNN类,需要传递的参数:

input_size:输入数据的特征维度
hidden_size:隐含状态 h t h_t ht的大小
num_layers:默认值为1,大于1,表示多个RNN堆叠起来
nonlinearity:默认是tanh,使用‘tanh’还是‘relu’非线性激活函数
batch_first:默认是False;若为True,输入输出格式为:(batch, seq, feature) ;若为False:输入输出格式为: (seq, batch, feature);
bidirectional:默认为False;若为True,则是双向RNN,同时输出长度为2*hidden_size

输入值:

  1. input:当batch_first=True 输入格式为(N,L, H i n H_{in} Hin);当batch_first=False 输入格式为(L,N, H i n H_{in} Hin);
  2. h 0 h_0 h0:默认输入值为0;输入格式为:(D*num_layers,N, H o u t H_{out} Hout

其中:

N = batch size
L = sequence length
D = 2 if bidirectional=True otherwise 1
H i n H_{in} Hin = input_size
H o u t H_{out} Hout = hidden_size

输出值:

  1. output:当batch_first=True 输出格式为(N,L,D* H o u t H_{out} Hout);当batch_first=False 输出格式为(L,N,D* H o u t H_{out} Hout);
  2. h_n:输出格式为:(D*num_layers,N, H o u t H_{out} Hout

1.2 单向、单层RNN

import torch
import torch.nn as nn

# 单层、单向RNN
single_RNN = nn.RNN(input_size=4, hidden_size=3, num_layers=1, batch_first=True)
input = torch.randn(1, 2, 4)    # batch_size, seq, feature

# 输入:input,h_0,  h_0默认为0
output, h_n = single_RNN(input)
print(output)
print(output.shape) # torch.Size([1, 2, 3])
#   (1, 2, 3) batch_size, sequence_length(输入的序列长度), 1*hidden_size
'''
tensor([[[ 0.0776, -0.1944,  0.2900],
         [ 0.5634,  0.4408, -0.1425]]], grad_fn=<TransposeBackward1>)
'''

# 最后一个时刻的隐含状态,在简单的RNN中最后时刻的隐含状态等于最后时刻的输出的
print(h_n)  # tensor([[[ 0.5634,  0.4408, -0.1425]]], grad_fn=<StackBackward0>)
print(h_n.shape)    # torch.Size([1, 1, 3])	

1.3 双向、单层RNN

# 双向、单层 RNN
bidirectional_rnn = nn.RNN(input_size=4, hidden_size=3, num_layers=1, batch_first=True, bidirectional=True)
input = torch.randn(1, 3, 4)    # 对应:batch_size, sequence_length,input_size
output, h_n = bidirectional_rnn(input)
print(output)
'''
tensor([[[ 0.2334,  0.4457, -0.7746, -0.8840, -0.4599,  0.3842],
         [ 0.2279,  0.7657, -0.6831, -0.4505, -0.8027,  0.7260],
         [ 0.8037, -0.2561,  0.4805, -0.9449, -0.1073, -0.3663]]],
       grad_fn=<TransposeBackward1>)
'''
print(output.shape) # torch.Size([1, 3, 6]) ,对应:batch_size, 输入的sequence_length,2*hidden_size
print(h_n)
'''
tensor([[[ 0.8037, -0.2561,  0.4805]],

        [[-0.8840, -0.4599,  0.3842]]], grad_fn=<StackBackward0>)
'''
print(h_n.shape)    # torch.Size([2, 1, 3])  对应:2*num_layers,batch_size,hidden_size

1.4 手写一个rnn_forward函数 实现单向RNN的计算原理

在这里插入图片描述

# batch_size, sequence_length
bs, T = 2, 3 #批大小,输入序列长度
# input_size, hidden_size, num_layers
input_size, hidden_size = 2, 3  #输入特征大小,隐含层特征大小

input = torch.randn(bs, T, input_size)  # 随机初始化一个输入特征序列
h_prev = torch.zeros(bs, hidden_size)   # 初始隐含状态
  1. step1 调用pytorch RNN API
# step1 调用一下pytorch RNN API
py_RNN = nn.RNN(input_size=input_size, hidden_size=hidden_size, batch_first=True)
rnn_output, rnn_h_n = py_RNN(input, h_prev.unsqueeze(0))
print(rnn_output)
print(rnn_h_n)
  1. step2 手写一个rnn_forward函数 实现单向RNN的计算原理
# step2 手写一个rnn_forward函数 实现单向RNN的计算原理
def rnn_forward(input, weight_ih, weight_hh, h_prev, bias_ih, bias_hh):
        # 获取一下维度
        bs, T, input_size = input.shape
        h_dim = weight_ih.shape[0]

        # 初始化一个输出(状态)矩阵
        h_out = torch.zeros(bs, T, h_dim)

        #使用循环进行计算
        # RNN的计算复杂度跟序列程度呈线性关系 故对序列长度进行循环即可
        for t in range(T):
            x = input[:, t, :].unsqueeze(2)  # 获取当前时刻输入特征   bs*input_size *1
            weight_ih_batch = weight_ih.unsqueeze(0).tile(bs, 1, 1) #bs*h_dim*input_size
            weight_hh_batch = weight_hh.unsqueeze(0).tile(bs, 1, 1) #bs*h_dim*h_dim

            w_times_x = torch.bmm(weight_ih_batch, x).squeeze(-1)   # bs*h_dim
            w_times_h = torch.bmm(weight_hh_batch, h_prev.unsqueeze(2)).squeeze(-1) # bs*h_dim

            h_prev = torch.tanh(w_times_x + bias_hh + w_times_h + bias_ih)

            h_out[:, t, :] = h_prev

        return h_out, h_prev.unsqueeze(0)

获取 pytorch RNN内置参数

for k, v in py_RNN.named_parameters():
    print(k, v)

输出为:

weight_ih_l0 Parameter containing:
tensor([[-0.3953,  0.1414],
        [ 0.2219,  0.3725],
        [ 0.4715,  0.4406]], requires_grad=True)
weight_hh_l0 Parameter containing:
tensor([[ 0.2937, -0.1251, -0.0135],
        [-0.0135,  0.1731,  0.4419],
        [-0.4208, -0.2223,  0.3648]], requires_grad=True)
bias_ih_l0 Parameter containing:
tensor([-0.3374,  0.3580, -0.2634], requires_grad=True)
bias_hh_l0 Parameter containing:
tensor([-0.5208, -0.0641, -0.5621], requires_grad=True)
  1. step3 调用forward函数验证结果
c_output, c_h_n = rnn_forward(input, py_RNN.weight_ih_l0, py_RNN.weight_hh_l0, h_prev, py_RNN.bias_ih_l0, py_RNN.bias_hh_l0)
print("rnn_forward function output:")
print(c_output)
print(c_h_n)

在这里插入图片描述

2 pytorch中tensor相关函数学习使用

  • squeeze unsqueeze 使用

squeeze: 删除输入张量中大小为1的指定维度

  1. 未指定参数dim,将张量中,维度为1的
a = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])

print(a.shape) # torch.Size([2, 3])
a = torch.reshape(a, [2, 1, 3, 1, 1])
print(a.shape)  # torch.Size([2, 1, 3, 1, 1])
b = torch.squeeze(a)
print(b.shape)  # torch.Size([2, 3])
  1. 参数dim取整型int,删除指定一个位置的维度1
a = torch.reshape(a, [2, 1, 3, 1, 1])
b = torch.squeeze(a, dim=1)
print(b.shape)  # torch.Size([2, 3, 1, 1])
  1. 参数dim取list,删除指定多个位置的维度1
a = torch.reshape(a, [2, 1, 3, 1, 1])
b = torch.squeeze(a, dim=[1, 3])
print(b.shape)  # torch.Size([2, 3, 1])

unsqueeze: 对输入张量中指定维度位置,增加大小为1的维度

注意:对于torch.unsqueeze的使用,必须声明dim的值且是整型int,不能是list型

a = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])

print(a.shape) # torch.Size([2, 3])

# 参数dim的值必须是int,不能是list
b = torch.unsqueeze(a, dim=1)
print(b.shape)  # torch.Size([2, 1, 3])
  • torch.flip使用

沿着指定维度上,反转张量的顺序

a = torch.arange(0, 12).view([2, 2, 3])
print(a)
a = torch.flip(a, [0, 1])
print(a)

翻转前:

tensor([[[ 0,  1,  2],
         [ 3,  4,  5]],

        [[ 6,  7,  8],
         [ 9, 10, 11]]])

翻转后(第0维度上进行翻转,第一维度上进行翻转):

tensor([[[ 9, 10, 11],
         [ 6,  7,  8]],

        [[ 3,  4,  5],
         [ 0,  1,  2]]])

总结

对于通过手写RNN的计算原理,有了更深刻的印象,下周将继续手写双向RNN的计算原理,并对LSTM更深入的学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值