深度学习第十一次作业(LSTM反向推导和算子实现)

1、使用Numpy实现LSTM算子

import numpy as np

#定义激活函数
def sigmoid(x):
    return 1/(1+np.exp(-x))

def tanh(x):
    return (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))

#权重
input_weight=np.array([1,0,0,0])
inputgate_weight=np.array([0,100,0,-10])
forgetgate_weight=np.array([0,100,0,10])
outputgate_weight=np.array([0,0,100,-10])

#输入
input=np.array([[1,0,0,1],[3,1,0,1],[2,0,0,1],[4,1,0,1],[2,0,0,1],[1,0,1,1],[3,-1,0,1],[6,1,0,1],[1,0,1,1]])

y=[]   #输出
c_t=0  #内部状态

for x in input:
    g_t=tanh(np.matmul(input_weight,x)) #候选状态
    i_t=np.round(sigmoid(np.matmul(inputgate_weight,x)))  #输入门
    after_inputgate=g_t*i_t       #候选状态经过输入门
    f_t=np.round(sigmoid(np.matmul(forgetgate_weight,x))) #遗忘门
    after_forgetgate=f_t*c_t      #内部状态经过遗忘门
    c_t=np.add(after_inputgate,after_forgetgate) #新的内部状态
    o_t=np.round(sigmoid(np.matmul(outputgate_weight,x))) #输出门
    after_outputgate=o_t*tanh(c_t)     #激活后新的内部状态经过输出门
    y.append(round(after_outputgate,2))   #输出

print('输出:',y)

2、使用nn.LSTMCell实现

import numpy as np
import torch
import torch.nn as nn

#实例化
input_size=4
hidden_size=1
cell=nn.LSTMCell(input_size=input_size,hidden_size=hidden_size)
#修改模型参数 weight_ih.shape=(4*hidden_size, input_size),weight_hh.shape=(4*hidden_size, hidden_size),
#weight_ih、weight_hh分别为输入x、隐层h分别与输入门、遗忘门、候选、输出门的权重
cell.weight_ih.data=torch.tensor([[0,100,0,-10],[0,100,0,10],[1,0,0,0],[0,0,100,-10]],dtype=torch.float32)
cell.weight_hh.data=torch.zeros(4,1)
print('cell.weight_ih.shape:',cell.weight_ih.shape)
print('cell.weight_hh.shape',cell.weight_hh.shape)
#初始化h_0,c_0
h_t=torch.zeros(1,1)
c_t=torch.zeros(1,1)
#模型输入input_0.shape=(batch,seq_len,input_size)
input_0=torch.tensor([[[1,0,0,1],[3,1,0,1],[2,0,0,1],[4,1,0,1],[2,0,0,1],[1,0,1,1],[3,-1,0,1],[6,1,0,1],[1,0,1,1]]],dtype=torch.float32)
#交换前两维顺序,方便遍历input.shape=(seq_len,batch,input_size)
input=torch.transpose(input_0,1,0)
print('input.shape:',input.shape)
output=[]
#调用
for x in input:
    h_t,c_t=cell(x,(h_t,c_t))
    output.append(np.around(h_t.item(), decimals=3))#保留3位小数
print('output:',output)

3、使用nn.LSTM实现

#LSTM
#实例化
import torch
from torch import nn

input_size=4
hidden_size=1
lstm=nn.LSTM(input_size=input_size,hidden_size=hidden_size,batch_first=True)
#修改模型参数
lstm.weight_ih_l0.data=torch.tensor([[0,100,0,-10],[0,100,0,10],[1,0,0,0],[0,0,100,-10]],dtype=torch.float32)
lstm.weight_hh_l0.data=torch.zeros(4,1)
#模型输入input.shape=(batch,seq_len,input_size)
input=torch.tensor([[[1,0,0,1],[3,1,0,1],[2,0,0,1],[4,1,0,1],[2,0,0,1],[1,0,1,1],[3,-1,0,1],[6,1,0,1],[1,0,1,1]]],dtype=torch.float32)
#初始化h_0,c_0
h_t=torch.zeros(1,1,1)
c_t=torch.zeros(1,1,1)
#调用
output,(h_t,c_t)=lstm(input,(h_t,c_t))
rounded_output = torch.round(output * 1000) / 1000  # 保留3位小数
print(rounded_output)

 

参考博客:

  1. 李宏毅机器学习笔记:RNN循环神经网络_李宏毅机器学习课程笔记-CSDN博客
  2. NNDL 作业十一 LSTM-CSDN博客
  3. LSTM网络中参数梯度解析:避免梯度消失与爆炸 (csdn.net)
  4. LSTM参数梯度推导与实现:对抗梯度消失, (csdn.net)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值