当前位置: 首页 > ops >正文

循环神经网络及其变体

RNN

import torch
import torch.nn as nnrnn = nn.RNN(5, 6, 1)
# 维度为5,每个隐藏层6个神经元,1个隐藏层
input = torch.randn(1, 3, 5)
# 批次样本数量为1,序列长度为3,维度为5
h0 = torch.randn(1, 3, 6)
# 1个隐藏层,序列长度为3,每个隐藏层6个神经元
output, hn = rnn(input, h0)
# 输出部分 = rnn(输入部分)print(output)
print(hn)
print(output.shape)
print(hn.shape)"""
tensor([[[-0.1758,  0.3392,  0.7851,  0.0583,  0.9421, -0.6396],[ 0.7938,  0.3311, -0.6934, -0.9418,  0.9247, -0.3842],[-0.4600,  0.7236, -0.5175,  0.2813, -0.5300,  0.3985]]],grad_fn=<StackBackward0>)
tensor([[[-0.1758,  0.3392,  0.7851,  0.0583,  0.9421, -0.6396],[ 0.7938,  0.3311, -0.6934, -0.9418,  0.9247, -0.3842],[-0.4600,  0.7236, -0.5175,  0.2813, -0.5300,  0.3985]]],grad_fn=<StackBackward0>)
torch.Size([1, 3, 6])
torch.Size([1, 3, 6])
"""

LSTM

import torch.nn as nn
import torchlstm = nn.LSTM(5, 6, 2)
# 维度为5,每个隐藏层6个神经元,2个隐藏层
input = torch.randn(1, 3, 5)
# 批次样本数量为1,序列长度为3,维度为5
h0 = torch.randn(2, 3, 6)# 初始化 LSTM 的初始隐藏状态
# 1个隐藏层,序列长度为3,每个隐藏层6个神经元
c0 = torch.randn(2, 3, 6)# 初始化 LSTM 的初始细胞状态
# 2个隐藏层,序列长度为3,每个隐藏层6个神经元
output, (hn, cn) = lstm(input, (h0, c0))
# 输出部分 = lstm(输入部分)print(output)
print(hn)
print(cn)
print(output.shape)
print(hn.shape)
print(cn.shape)"""
tensor([[[ 0.2465,  0.4144,  0.2912, -0.0157, -0.0693,  0.1067],[-0.0261, -0.5055, -0.4013,  0.4995, -0.5971, -0.0331],[-0.2773,  0.0694,  0.1586, -0.1511, -0.0051, -0.4707]]],grad_fn=<StackBackward0>)
tensor([[[-0.1957,  0.0412, -0.5626,  0.2152,  0.3757,  0.2379],[-0.0030,  0.0779,  0.1966,  0.4887,  0.2604,  0.3688],[ 0.0533, -0.1841, -0.0854,  0.3123,  0.2481,  0.3225]],[[ 0.2465,  0.4144,  0.2912, -0.0157, -0.0693,  0.1067],[-0.0261, -0.5055, -0.4013,  0.4995, -0.5971, -0.0331],[-0.2773,  0.0694,  0.1586, -0.1511, -0.0051, -0.4707]]],grad_fn=<StackBackward0>)
tensor([[[-0.5586,  0.1939, -1.4773,  0.4838,  0.5345,  0.4465],[-0.0109,  0.1245,  0.2840,  0.6625,  1.6524,  0.5431],[ 0.1326, -0.4249, -0.1355,  1.0727,  0.4292,  0.5769]],[[ 0.4582,  0.8678,  0.6479, -0.0381, -0.1044,  0.2260],[-0.0364, -0.7348, -0.7428,  1.0751, -1.3216, -0.0771],[-0.4357,  0.1676,  0.5697, -0.5810, -0.0135, -0.7366]]],grad_fn=<StackBackward0>)
torch.Size([1, 3, 6])
torch.Size([2, 3, 6])
torch.Size([2, 3, 6])
"""

 GRU

import torch
import torch.nn as nngru=nn.GRU(5,6,2)
# 维度为5,每个隐藏层6个神经元,2个隐藏层
input1=torch.randn(1,3,5)
# 批次样本数量为1,序列长度为3,维度为5
h0=torch.randn(2,3,6)
# 2个隐藏层,序列长度为3,每个隐藏层6个神经元
output,hn = gru(input1,h0)
# 输出部分 = gru(输入部分)print(output)
print(hn)
print(output.shape)
print(hn.shape)"""
tensor([[[ 0.9017, -0.8316, -0.7745,  0.3363, -0.4152, -0.2663],[-0.1789,  0.3301,  0.5574,  0.1021, -0.1050, -0.8172],[-0.3017,  0.4322,  0.1236,  0.5446, -0.2009, -0.3321]]],grad_fn=<StackBackward0>)
tensor([[[-0.5085,  0.8401, -0.1227, -1.2665,  0.2676, -0.7743],[-0.0389, -0.3141, -0.4438,  0.7459, -0.2061,  0.5883],[ 0.1583, -0.3947,  0.2210,  0.8555,  0.6499, -0.5270]],[[ 0.9017, -0.8316, -0.7745,  0.3363, -0.4152, -0.2663],[-0.1789,  0.3301,  0.5574,  0.1021, -0.1050, -0.8172],[-0.3017,  0.4322,  0.1236,  0.5446, -0.2009, -0.3321]]],grad_fn=<StackBackward0>)
torch.Size([1, 3, 6])
torch.Size([2, 3, 6])
"""

http://www.xdnf.cn/news/14383.html

相关文章:

  • 数据库核心技术深度剖析:事务、索引、锁与SQL优化实战指南(第六节)-----InnoDB引擎
  • 软件设计模式入门
  • 力扣Hot100每日N题(17~18)
  • Vue学习001-创建 Vue 应用
  • anaconda安装教程
  • 板凳-------Mysql cookbook学习 (十--7)
  • 使用pinia代替vuex处理登录流程
  • 什么是扩展运算符?有什么使用场景?
  • 强化学习怎么入门?
  • Vue3 跨多个组件方法调用:简洁实用的解决方案
  • 人工智能基础知识笔记十:降维技术
  • cache的学习
  • 扣子开发平台 Agent 开发教程(一)
  • Adoquery 转AdoDataSet
  • LeetCode 1385.两个数组间的距离值
  • Kafka 可靠性保障:消息确认与事务机制(一)
  • vue3 +spring boot文件上传
  • 【Go语言-Day 1】扬帆起航:从零到一,精通 Go 语言环境搭建与首个程序
  • 操作系统核心名词解释--期末简答题快速复习
  • cuda编程笔记(2.5)--简易的应用代码
  • 利用 Python 爬虫获取 Amazon 商品详情:实战指南
  • HarmonyOS 探秘手记:我在 “鸿蒙星球” 的第一天
  • linux 常用工具的静态编译之二
  • 数字孪生赋能智慧城市大脑建设方案PPT(65页)
  • vscode通过ssh连接
  • 理解ES6中的Promise
  • SAP-增删改查
  • 中介者模式Mediator Pattern
  • 鸿蒙智行5月全系交付新车破4.4万辆,销量再创新高
  • FTP 并不适合用在两个计算机之间共享读写文件 为什么