门控循环单元(GRU)【动手学深度学习v2】

news2025/2/21 16:50:43

理论

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
候选隐藏状态。
圆圈 表示 按元素乘法。

这里面的 在这里插入图片描述 这个符号值得是 按元素相乘。
Rt理解为 和Ht 长度一样的一维向量。(这么理解)
在这里插入图片描述
这里如果Rt长的像0的话,那么乘出来的结果,就也像0。

要是像0 的话,相当于是说 把上一刻的隐藏转态给遗忘掉。

在这里Rt是可以学的。
全是0的话,相当于回到初始转态。
全是1的话,都保留。

真正的隐状态:

在这里插入图片描述
假设Zt都等于1,那么就相当于只看过去的状态,不看现在的状态,
如果Zt都等于0,那么相当于只看当前状态,

Zt可以理解为 过去隐藏状态 ,在当前隐藏状态中,占了多少的权重。

在这里插入图片描述

感觉GRU里reset和update这个命名挺精髓的:
1.理论上Whh完全具备reset的能力,使得公式退化为全连接(Whh全为0),但是专门加了一个reset,且sigmoid为激活函数(特性是输出值0-1,且模仿的是0到1的阶跃),给我的感觉是在设计网络的时候专门引导网络更容易重置Ht-1项,在这里reset名副其实。
2.而update的设计,让我想到的是resnet里面引入X的操作,只是这里引入的是Ht-1,而且通过update门来控制两边的权重;如果忽略update门的权重,可以看做resnet当中关于Ht和Ht-1的残差块(resnet是f(x)与x的块)。但是在这里因为update门也是sigmoid函数,使得函数以较大的可能性在使用Ht-1和ht两种情况中做选择,相对于RNN当中,它给的是输出Ht-1的可能,那这个update门是不是叫remember门更为合适。
欢迎指正,特别是老师!

特别的:
Z里面全0,
R里面全1的情况,相当于把前面的隐藏状态直接拿过来用。
就是RNN。

代码实现

其实实现 不算是 复杂,
就是上面图里面的公式,拿过来到
从零实现:

import torch
from torch import nn
from d2l import torch as d2l

batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)


# 初始化模型参数
def get_params(vocab_size, num_hiddens, device):
    num_inputs = num_outputs = vocab_size

    def normal(shape):
        return torch.randn(size=shape, device=device) * 0.01

    def three():
        return (normal(
            (num_inputs, num_hiddens)), normal((num_hiddens, num_hiddens)),
                torch.zeros(num_hiddens, device=device))

    W_xz, W_hz, b_z = three()  # GRU多了这两行
    W_xr, W_hr, b_r = three()  # GRU多了这两行
    W_xh, W_hh, b_h = three()
    W_hq = normal((num_hiddens, num_outputs))
    b_q = torch.zeros(num_outputs, device=device)
    params = [W_xz, W_hz, b_z, W_xr, W_hr, b_r, W_xh, W_hh, b_h, W_hq, b_q]
    for param in params:
        param.requires_grad_(True)
    return params

# 定义隐藏状态的初始化函数
def init_gru_state(batch_size, num_hiddens, device):
    return (torch.zeros((batch_size, num_hiddens), device=device),)
# 定义门控循环单元模型
def gru(inputs, state, params):
    W_xz, W_hz, b_z, W_xr, W_hr, b_r, W_xh, W_hh, b_h, W_hq, b_q = params
    H, = state
    outputs = []
    for X in inputs:
        Z = torch.sigmoid((X @ W_xz) + (H @ W_hz) + b_z)
        R = torch.sigmoid((X @ W_xr) + (H @ W_hr) + b_r)
        H_tilda = torch.tanh((X @ W_xh) + ((R * H) @ W_hh) + b_h)
        H = Z * H + (1 - Z) * H_tilda
        Y = H @ W_hq + b_q
        outputs.append(Y)
    return torch.cat(outputs, dim=0), (H,)
# 训练
vocab_size, num_hiddens, device = len(vocab), 256, d2l.try_gpu()
num_epochs, lr = 500, 1
model = d2l.RNNModelScratch(len(vocab), num_hiddens, device, get_params,
                           init_gru_state, gru)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)

简洁实现:

# 简洁实现
num_inputs = vocab_size
gru_layer = nn.GRU(num_inputs, num_hiddens)
model = d2l.RNNModel(gru_layer, len(vocab))
model = model.to(device)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)

运行结果:

time traveller                                                  
<Figure size 350x250 with 1 Axes>
time traveller ate ate te ate ate te ate ate te ate ate te ate a
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time traveller an the the the the the the the the the the the th
<Figure size 350x250 with 1 Axes>
time travellerererererererererererererererererererererererererer
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time travellere the the the the the the the the the the the the 
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the thered the the thered the the thered the 
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the the the the the the the the the the the the t
<Figure size 350x250 with 1 Axes>
time traveller the time traveller the time traveller the time tr
<Figure size 350x250 with 1 Axes>
time traveller the the thing the time the traceller the thing th
<Figure size 350x250 with 1 Axes>
time traveller the three dimensions of the time trave all the ti
<Figure size 350x250 with 1 Axes>
time traveller the three dimensions of the time traveller the tr
<Figure size 350x250 with 1 Axes>
time traveller the proples that is a somient and the three and t
<Figure size 350x250 with 1 Axes>
time traveller the three dimensions of the three dimensions of t
<Figure size 350x250 with 1 Axes>
time traveller than a man a cour a courar this is in time travel
<Figure size 350x250 with 1 Axes>
time traveller than a man existery light of shace and they tound
<Figure size 350x250 with 1 Axes>
time traveller that is the time travellerit s ageat on the than 
<Figure size 350x250 with 1 Axes>
time traveller scollised of ho s all phace and they thinkness ge
<Figure size 350x250 with 1 Axes>
time travellerist four dimensions of space why is thend whing th
<Figure size 350x250 with 1 Axes>
time travellerit s agee trantionirg inewald the ment light of sh
<Figure size 350x250 with 1 Axes>
time travellerit that is the stacl proceeded anyreal inds reccan
<Figure size 350x250 with 1 Axes>
time traveller spoce the about in the foom the prowel instimited
<Figure size 350x250 with 1 Axes>
time traveller proceeded anyreal thing so move about in all only
<Figure size 350x250 with 1 Axes>
time traveller smiling and they it isspone of the grove is a mom
<Figure size 350x250 with 1 Axes>
time traveller sot a on thisknow of cheirumone fire iftine in an
<Figure size 350x250 with 1 Axes>
time travellerit soug i now have abeet in wilb have no means of 
<Figure size 350x250 with 1 Axes>
time traveller for so it will be convenient to speak of himwas e
<Figure size 350x250 with 1 Axes>
time traveller proceeded anyreal body must have extension in fou
<Figure size 350x250 with 1 Axes>
time traveller for so it will be convenient to speak of himwas e
<Figure size 350x250 with 1 Axes>
time travelleryou can show black is white by argument said filby
<Figure size 350x250 with 1 Axes>
time travelleryou can show black is white by argument said filby
<Figure size 350x250 with 1 Axes>
time travelleryou can show black is white by argument said filby
<Figure size 350x250 with 1 Axes>
time traveller for so it will be convenient to speak of himwas e
<Figure size 350x250 with 1 Axes>
time travelleryou can show black is white by argument said filby
<Figure size 350x250 with 1 Axes>
time travelleryou can show black is white by argument said filby
<Figure size 350x250 with 1 Axes>
time travellery contenter mimensions of space ghic dimensions of
<Figure size 350x250 with 1 Axes>
perplexity 1.2, 9001.5 tokens/sec on cpu
time travellery contenter mimensions of space ghic dimensions of
travelleryou can showarde thing there was is alw ys canemer

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/3308.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

文件操作之文件系统

目录 一 磁盘 1 磁盘的物理结构 2 磁盘在物理结构上如何存储数据 CHS寻址 3 从物理结构到抽象结构 LBA寻址 4管理 二 块组 boot block super block inode table data blocks block bitmap GDT&#xff1a;Global Descriptor Table 块组描述符 三 文件名和目录之…

网课查题接口搭建

网课查题接口搭建 本平台优点&#xff1a; 多题库查题、独立后台、响应速度快、全网平台可查、功能最全&#xff01; 1.想要给自己的公众号获得查题接口&#xff0c;只需要两步&#xff01; 2.题库&#xff1a; 查题校园题库&#xff1a;查题校园题库后台&#xff08;点击跳…

字符串匹配算法(C/Java实现)

目录BF算法C语言实现Java实现KMP算法Java实现C语言实现next[]数组的优化BF算法 BF算法&#xff0c;即暴力(Brute Force)算法&#xff0c;是普通的模式匹配算法&#xff0c;BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配&#xff0c;若相等&#xff0c;…

R语言代做实现:混合正态分布EM最大期望估计法

全文链接&#xff1a;http://tecdat.cn/?p4815 原文出处&#xff1a;拓端数据部落公众号 因为近期在分析数据时用到了EM最大期望估计法这个算法&#xff0c;在参数估计中也用到的比较多。然而&#xff0c;发现国内在R软件上实现高斯混合分布的EM的实例并不多&#xff0c;大多…

Linux系统下实现开机自动加载驱动模块

在使用模块化加载驱动时&#xff0c;若系统内部存在同类别设备驱动&#xff0c;可能会出现无法加载我们添加的动态模块&#xff0c;比如Linux系统内置了CDC驱动&#xff0c;当我们使用兼容CDC和VCP驱动USB转串口芯片时&#xff0c;就会出现上电出现的是CDC串口&#xff0c;从而…

vue3 组件响应式v-model 失效,实践踩坑,一文搞懂组件响应式原理,对初学者友好

文章目录前情提要实战解析最后前情提要 vue3的v-model已经有了变化&#xff0c;假如你还不知道其中细节&#xff0c;看完这篇文章你就完全明白了&#xff0c;我以踩坑的场景来进行解析。起因是在我的项目中需要一个输入框组件&#xff0c;这个组件用来根据输入异步查询系统内已…

Python编程 基础数据类型

作者简介&#xff1a;一名在校计算机学生、每天分享Python的学习经验、和学习笔记。 座右铭&#xff1a;低头赶路&#xff0c;敬事如仪 个人主页&#xff1a;网络豆的主页​​​​​​ 目录 前言&#xff1a; 一.Python基础数据类型 1.为什么会有数据类型&#xff1f;&am…

公共云和私有云之间的区别

目前&#xff0c;越来越多的公司正在调整云服务来运行他们的应用程序。其实&#xff0c;有不同类型的云部署模型来满足客户的不同需求。云部署模型分为三种类型&#xff1a;公有云、私有云和混合云(公有云和私有云的混合)。在本文中&#xff0c;我们将对公共云和私有云之间的区…

【数据结构】单链表——单链表的定义及基本操作的实现(头插、尾插、头删、尾删、任意位置的插入与删除)

&#x1f9d1;‍&#x1f4bb;作者&#xff1a; 情话0.0 &#x1f4dd;专栏&#xff1a;《数据结构》 &#x1f466;个人简介&#xff1a;一名双非编程菜鸟&#xff0c;在这里分享自己的编程学习笔记&#xff0c;欢迎大家的指正与点赞&#xff0c;谢谢&#xff01; 单链表前言…

分享30个PHP源码,总有一款适合你

链接&#xff1a;https://pan.baidu.com/s/1dVbUn5YFMOze4J-K8sCAXQ?pwdeinu 提取码&#xff1a;einu 下面是文件的名字&#xff0c;我放了一些图片&#xff0c;文章里不是所有的图主要是放不下...&#xff0c;大家下载后可以看到。 Emlog for SAE 适合新浪sae使用的个人博客…

网关Gateway-快速上手

gateway网关官方文档: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/# 网关的概念 网关作为流量的入口&#xff0c;常用的功能包括路由转发&#xff0c;权限校验&#xff0c;限流等。 Spring Cloud Gateway 是Spring Cloud官方推出的第二代网关…

Java:修改Jar的源码,并上传Nexus私有仓库,替换jar版本

第一步&#xff1a;修改jar包源代码 建一个全类名一模一样的类&#xff0c;然后把要修改的类的代码复制过去&#xff0c;然后编译生成class。然后拿编译后的class覆盖到jar中对应的位置 第二步&#xff1a;上传nexus jar文件&#xff0c;pom文件&#xff1a;在本地仓库中可以…

Linux操作系统~进程有哪些状态?

目录 R状态 S/D状态 什么是D状态 T状态 X状态 Z状态 什么是等待队列&#xff0c;什么是运行队列&#xff0c;什么是挂起/阻塞&#xff0c;什么叫唤醒进程 对比宏观上操作系统的三种状态 从操作系统宏观的概念上讲&#xff0c;进程有三种状态&#xff0c;就绪态&#xff0…

自动化测试和测试自动化你分的清楚吗?

目录 前言 两种自动化测试 为什么测试自动化对连续测试至关重要 使测试自动化成为现实 拥抱连续测试 总结 重点&#xff1a;配套学习资料和视频教学 前言 当我们谈论持续测试&#xff0c;以及持续交付和DevOps时&#xff0c;“自动化”一词就泛滥了。从根本上讲&#xf…

ES6之对象解构

对象和数组字面量是JavaScript中两种最常用的数据结构&#xff0c;由于JSON数据格式的普及&#xff0c;二者已经成为语言中最重要的一部分。在代码中&#xff0c;我们经常定义很多对象和数组&#xff0c;然后从去提取相关的信息片段&#xff0c;ES6为简化这种任务引入了新特性&…

猿代码浅谈MPI与OpenMP并行程序设计

一、什么是OpenMP&#xff1f; OpenMP是一种用于共享内存并行系统的多线程程序设计的库(Compiler Directive),特别适合于多核CPU上的并行程序开发设计。它支持的语言包括&#xff1a;C语言、C、Fortran;不过&#xff0c;用以上这些语言进行程序开发时&#xff0c;并非需要特别…

一文读懂qt界面设计(分裂器,布局,拉伸,各种属性设置)

可以先看看我这个文章&#xff1a;qt关于界面设计中的一些知识总结_我是标同学的博客-CSDN博客_qt 水平伸展 现在我们来正式开始讲解。 布局种类 qt中能称为布局管理器的有如下6个&#xff1a; 水平布局&#xff08;QHBoxLayout&#xff09;垂直布局&#xff08;QVBoxLayout…

数字电路基础04(查找表LUT)

文章目录 LUT(Look Up Table)为什么要用LUT?示例(3输入LUT)LUT(Look Up Table) 在FPGA中,利用LUT来实现组合逻辑的功能,将组合逻辑的输入输出结果,存储为真值表的形式,来代替传统的由逻辑门组成的组合逻辑电路LUT就是将组合逻辑转换成真值表LUT实际上是将输入数据作…

怎么清理c盘的垃圾文件?有什么好的清理方法推荐?

在使用电脑办公或者娱乐的时候&#xff0c;我们的电脑会产生很多临时文件&#xff0c;如果这些临时文件不被清理掉的话&#xff0c;就会导致电脑的运行速度越来越慢&#xff0c;为了能够让电脑的速度越来越快&#xff0c;很多人都会想要清理C盘&#xff0c;但是在清理C盘的时候…

机器视觉(三):摄像机标定技术

目录&#xff1a; 机器视觉&#xff08;二&#xff09;&#xff1a;机器视觉硬件技术 机器视觉&#xff08;三&#xff09;&#xff1a;摄像机标定技术 &#x1f30f;&#x1f9d0;以下为正文&#x1f984;&#x1fa90; 摄像机标定的目的&#xff1a;三维重建 空间物体表面…