在 PyTorch 中展开
来源:dev.to
时间:2024-11-18 21:27:40 117浏览 收藏
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《在 PyTorch 中展开》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
请我喝杯咖啡☕
*备忘录:
- 我的帖子解释了 unflatten()。
- 我的帖子解释了 flatten() 和 ravel()。
- 我的帖子解释了 flatten()。
unflatten() 可以向零个或多个元素的一维或多个 d 张量添加零个或多个维度,得到零个或多个元素的一维或多个 d 张量,如下所示:
*备忘录:
- 初始化的第一个参数是dim(required-type:int)。
- 初始化的第二个参数是 unflattened_size(必需类型:元组或 int 列表)。
- 第一个参数是输入(必需类型:int、float、complex 或 bool 的张量)。 *-1 推断并调整大小。
- unflatten() 和 unflatten() 的区别是:
- unflatten() 具有 unflattened_size 参数,该参数与 unflatten() 的 size 参数相同。
- 基本上,unflatten() 用于定义模型,而 unflatten() 不用于定义模型。
import torch from torch import nn unflatten = nn.Unflatten() unflatten # Unflatten(dim=0, unflattened_size=(6,)) unflatten.dim # 0 unflatten.unflattened_size # (6,) my_tensor = torch.tensor([7, 1, -8, 3, -6, 0]) unflatten = nn.Unflatten(dim=0, unflattened_size=(6,)) unflatten = nn.Unflatten(dim=0, unflattened_size=(-1,)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(6,)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1,)) unflatten(input=my_tensor) # tensor([7, 1, -8, 3, -6, 0]) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 6)) unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 6)) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 6)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 6)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1)) unflatten(input=my_tensor) # tensor([[7, 1, -8, 3, -6, 0]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(2, 3)) unflatten = nn.Unflatten(dim=0, unflattened_size=(2, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(2, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(2, -1)) unflatten(input=my_tensor) # tensor([[7, 1, -8], [3, -6, 0]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(3, 2)) unflatten = nn.Unflatten(dim=0, unflattened_size=(3, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, 2)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, -1)) unflatten(input=my_tensor) # tensor([[7, 1], [-8, 3], [-6, 0]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(6, 1)) unflatten = nn.Unflatten(dim=0, unflattened_size=(6, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(6, 1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(6, -1)) unflatten(input=my_tensor) # tensor([[7], [1], [-8], [3], [-6], [0]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2, 3)) unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 2, 3)) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1, 3)) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 2, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 2, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 2, -1)) unflatten(input=my_tensor) # tensor([[[7, 1, -8], [3, -6, 0]]]) etc my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(2,)) unflatten = nn.Unflatten(dim=0, unflattened_size=(-1,)) unflatten = nn.Unflatten(dim=1, unflattened_size=(3,)) unflatten = nn.Unflatten(dim=1, unflattened_size=(-1,)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(3,)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1,)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(2,)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(-1,)) unflatten(input=my_tensor) # tensor([[7, 1, -8], [3, -6, 0]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2)) unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 2)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 2)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(-1, 2)) unflatten(input=my_tensor) # tensor([[[7, 1, -8], [3, -6, 0]]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(2, 1)) unflatten = nn.Unflatten(dim=0, unflattened_size=(2, -1)) unflatten = nn.Unflatten(dim=1, unflattened_size=(1, 3)) unflatten = nn.Unflatten(dim=1, unflattened_size=(-1, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 3)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(2, 1)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(2, -1)) unflatten(input=my_tensor) # tensor([[[7, 1, -8]], [[3, -6, 0]]]) unflatten = nn.Unflatten(dim=1, unflattened_size=(3, 1)) unflatten = nn.Unflatten(dim=1, unflattened_size=(3, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, 1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, -1)) unflatten(input=my_tensor) # tensor([[[7], [1], [-8]], [[3], [-6], [0]]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 1, 2)) unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 1, 2)) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1, 2)) unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 1, -1)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 1, 2)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(-1, 1, 2)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, -1, 2)) unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 1, -1)) unflatten(input=my_tensor) # tensor([[[[7, 1, -8], [3, -6, 0]]]]) unflatten = nn.Unflatten(dim=1, unflattened_size=(1, 1, 3)) unflatten = nn.Unflatten(dim=1, unflattened_size=(-1, 1, 3)) unflatten = nn.Unflatten(dim=1, unflattened_size=(1, -1, 3)) unflatten = nn.Unflatten(dim=1, unflattened_size=(1, 1, -1)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 1, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 1, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1, 3)) unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 1, -1)) unflatten(input=my_tensor) # tensor([[[[7, 1, -8]]], [[[3, -6, 0]]]]) my_tensor = torch.tensor([[7., 1., -8.], [3., -6., 0.]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(2,)) unflatten(input=my_tensor) # tensor([[7., 1., -8.], [3., -6., 0.]]) my_tensor = torch.tensor([[7.+0.j, 1.+0.j, -8.+0.j], [3.+0.j, -6.+0.j, 0.+0.j]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(2,)) unflatten(input=my_tensor) # tensor([[7.+0.j, 1.+0.j, -8.+0.j], # [3.+0.j, -6.+0.j, 0.+0.j]]) my_tensor = torch.tensor([[True, False, True], [False, True, False]]) unflatten = nn.Unflatten(dim=0, unflattened_size=(2,)) unflatten(input=my_tensor) # tensor([[True, False, True], [False, True, False]])
理论要掌握,实操不能落!以上关于《在 PyTorch 中展开》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
345 收藏
-
463 收藏
-
124 收藏
-
393 收藏
-
424 收藏
-
123 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习