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

Pytorch 笔记

执行下面这段代码后,为什么返回的是 2 ?

vector = torch.tensor([7, 7])
vector.shape

为什么返回的是 torch.Size([2])

当你创建一个PyTorch张量时,它会记住张量中元素的数量每个维度的大小。在你的代码中,torch.tensor([7, 7])创建了一个一维张量,其中包含两个元素:7和7。因为这是一个一维张量,所以.shape属性返回一个只有一个元素的元组该元素表示张量的长度。在这种情况下,张量的长度为2,因此返回的形状是torch.Size([2])

tensor 的维度

TENSOR = torch.tensor([[[1, 2, 3],[3, 6, 9],[2, 4, 5]]])
TENSOR.ndim

返回的是 [1,3,3] , 如何判断?有三层 [ ] 括号,将每个 [ ] 括号视为列表,从最里层起,当前列表有几个并列的元素,TENSOR.ndim 返回的列表最右边的元素就是几,然后去掉最外面一层的 [ ] 括号,继续判断当前列表有几个并列的元素,TENSOR.ndim 返回的列表次右边的元素就是几,依次类推。

在这里插入图片描述

Scalar,Vector,Matrix,Tensor

在这里插入图片描述

torch.arange()

torch.arange() 返回的是 PyTorch 中的 tensor,而不是 NumPy 数组。

torch中对tensor的各种切片操作

好的,让我们使用一个三维张量来详细解释各种复杂的切片操作。我们首先创建一个形状为 2 × 3 × 4 2 \times 3 \times 4 2×3×4 的三维张量:

import torch# 创建一个形状为 2x3x4 的三维张量
tensor = torch.arange(24).reshape(2, 3, 4)
print("Original Tensor:")
print(tensor)

假设我们有一个如下所示的三维张量:

tensor([[[ 0,  1,  2,  3],[ 4,  5,  6,  7],[ 8,  9, 10, 11]],[[12, 13, 14, 15],[16, 17, 18, 19],[20, 21, 22, 23]]])

1. 选择特定的切片

选择第一个维度的第一个子张量
slice_1 = tensor[0, :, :]
print("Slice along the first dimension (index 0):")
print(slice_1)

输出:

tensor([[ 0,  1,  2,  3],[ 4,  5,  6,  7],[ 8,  9, 10, 11]])
选择第二个维度的第二个子张量
slice_2 = tensor[:, 1, :]
print("Slice along the second dimension (index 1):")
print(slice_2)

输出:

tensor([[ 4,  5,  6,  7],[16, 17, 18, 19]])
选择第三个维度的第三个子张量
slice_3 = tensor[:, :, 2]
print("Slice along the third dimension (index 2):")
print(slice_3)

输出:

tensor([[ 2,  6, 10],[14, 18, 22]])

2. 高级切片操作

选择第一个维度的第一个子张量中的第1到第2行(不包括第2行)
slice_4 = tensor[0, 0:1, :]
print("Slice along the first dimension (index 0) and rows 0 to 1:")
print(slice_4)

输出:

tensor([[0, 1, 2, 3]])
选择第二个维度的第0和第2行,并选择所有列
slice_5 = tensor[:, [0, 2], :]
print("Select rows 0 and 2 from the second dimension:")
print(slice_5)

输出:

tensor([[[ 0,  1,  2,  3],[ 8,  9, 10, 11]],[[12, 13, 14, 15],[20, 21, 22, 23]]])
选择第三个维度的第1和第3列
slice_6 = tensor[:, :, [1, 3]]
print("Select columns 1 and 3 from the third dimension:")
print(slice_6)

输出:

tensor([[[ 1,  3],[ 5,  7],[ 9, 11]],[[13, 15],[17, 19],[21, 23]]])

3. 使用布尔张量进行索引

选择大于10的元素
mask = tensor > 10
slice_7 = tensor[mask]
print("Elements greater than 10:")
print(slice_7)

输出:

tensor([11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])

4. 使用长整型张量进行索引

选择第1和第3列的数据
indices = torch.tensor([1, 3])
slice_8 = tensor[:, :, indices]
print("Select columns indexed by [1, 3]:")
print(slice_8)

输出:

tensor([[[ 1,  3],[ 5,  7],[ 9, 11]],[[13, 15],[17, 19],[21, 23]]])

5. 花式索引

使用多个索引数组
rows = torch.tensor([0, 1])
cols = torch.tensor([2, 3])
slice_9 = tensor[0, rows, cols]
print("Fancy indexing with rows and cols:")
print(slice_9)

输出:

tensor([2, 7])

通过这些示例,希望你对 PyTorch 中的张量索引和切片操作有了更深入的理解。这些操作在数据预处理、特征提取和神经网络模型的实现中非常重要。

torch 中 tensor 的各种乘法

在 PyTorch 中,有多种实现张量相乘的方式,每种方式在实现上有一些差异,有些是就地操作,有些不是。以下是几种主要的实现方式:

1. 元素级相乘 (Element-wise Multiplication)

要求两个 tensor 的 shape 一致

使用 * 操作符
import torcha = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])result = a * b
print(result)
使用 torch.mul()
result = torch.mul(a, b)
print(result)
就地操作

使用 mul_() 方法:

a.mul_(b)
print(a)

2. 矩阵乘法 (Matrix Multiplication)

使用 @ 操作符 (Python 3.5+)
result = a @ b.T  # 转置 b 以使其形状匹配矩阵乘法要求
print(result)
使用 torch.matmul()
result = torch.matmul(a, b.T)
print(result)
使用 torch.mm()(仅适用于二维张量)
result = torch.mm(a, b.T)
print(result)

3. 广义点积 (Dot Product for 1D tensors)

使用 torch.dot()
c = torch.tensor([1, 2, 3])
d = torch.tensor([4, 5, 6])result = torch.dot(c, d)
print(result)

4. 批量矩阵乘法 (Batch Matrix Multiplication)

使用 torch.bmm()
e = torch.randn(10, 3, 4)  # 形状为 (batch_size, m, n)
f = torch.randn(10, 4, 5)  # 形状为 (batch_size, n, p)result = torch.bmm(e, f)
print(result)

5. 广播相乘 (Broadcast Multiplication)

张量会自动广播到兼容的形状。

g = torch.tensor([1, 2, 3])
h = torch.tensor([[1], [2], [3]])result = g * h
print(result)

就地操作总结

就地操作会直接修改原始张量的值,通常以 _ 结尾:

  • a.mul_(b):就地进行元素级相乘

非就地操作会创建新的张量并返回结果,而不改变输入张量的值。

这些不同的乘法操作方式在不同的应用场景中有不同的用途,根据需要选择适合的乘法方式。

相关文章:

  • Linux入门攻坚——23、DNS和BIND基础入门2
  • 微信小程序开发(持续更新)
  • 实时合成 1 秒频订单簿快照:DolphinDB INSIGHT 行情插件与订单簿引擎应用
  • FaceChain-FACT:开源10秒写真生成,复用海量LoRa风格,基模友好型写真应用
  • 【链表】Leetcode 82. 删除排序链表中的重复元素 II【中等】
  • JavaScript日期与时间处理的艺术
  • webshell工具-冰蝎流量特征和加密方式
  • VUE3学习第一篇:启动ruoyi
  • 计算机网络-BGP基础概念
  • 算法之堆排序
  • 量子密钥分发系统基础器件(一):光纤干涉仪
  • C#算数运算符
  • HBase安装
  • 【深入浅出:正则化在防止深度学习过拟合中的应用】
  • AURIX TC3xx单片机介绍-启动过程介绍3
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 30天自制操作系统-2
  • C学习-枚举(九)
  • Django 博客开发教程 8 - 博客文章详情页
  • MySQL几个简单SQL的优化
  • SpiderData 2019年2月23日 DApp数据排行榜
  • 力扣(LeetCode)357
  • 如何设计一个比特币钱包服务
  • 使用 @font-face
  • 数据科学 第 3 章 11 字符串处理
  • 突破自己的技术思维
  • 为视图添加丝滑的水波纹
  • 我看到的前端
  • 学习JavaScript数据结构与算法 — 树
  • 一个完整Java Web项目背后的密码
  • 字符串匹配基础上
  • PostgreSQL之连接数修改
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • 容器镜像
  • ​马来语翻译中文去哪比较好?
  • # Maven错误Error executing Maven
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • (003)SlickEdit Unity的补全
  • (2015)JS ES6 必知的十个 特性
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (Git) gitignore基础使用
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)德国人的记事本
  • *** 2003
  • .NET 8.0 中有哪些新的变化?
  • .NET Core 中插件式开发实现
  • .net framework 4.0中如何 输出 form 的name属性。
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .net web项目 调用webService
  • ?.的用法
  • @Conditional注解详解
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——