当前位置:网站首页>[PyTroch系列-12]:PyTorch基础 - 张量Tensor线性运算(点乘、叉乘)
[PyTroch系列-12]:PyTorch基础 - 张量Tensor线性运算(点乘、叉乘)
2021-08-08 15:40:37 【文火冰糖的硅基工坊】
第1章 Tensor运算概述
1.1 概述
PyTorch提供了大量的张量运算,基本上可以对标Numpy多维数组的运算,以支持对张量的各种复杂的运算。
这些操作运算中大多是对数组中每个元素执行相同的函数运算,并获得每个元素函数运算的结果序列,这些序列生成一个新的同维度的数组。
https://www.runoob.com/numpy/numpy-linear-algebra.html
1.2 运算分类
(1)算术运算:加、减、系数乘、系数除
(2)函数运算:sin,cos
(3)取整运算:上取整、下取整
(4)统计运算:最大值、最小值、均值
(5)比较运算:大于,等于,小于、排序
(6)线性代数运算:矩阵、点乘、叉乘
1.3 “in place“运算
“in place“运算不是某个特定的函数运算,而是每个函数都有自己的“in place“运算的版本。
xxx_():执行完该操作,直接修改tensor自身的值。
基本上,每个函数都有自己的in place版本。
如
torch.cos() =》torch.cos_()
torch.floor() =》torch.floor_()
1.4 Tensor的广播机制: 不同维度的张量运算
1.5 环境准备
import numpy as np import torch print("Hello World") print(torch.__version__) print(torch.cuda.is_available())
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
1.6 张量的线性代数运算
(1)点乘:dot(a,b)
(2)内积: inner(a,b)
(3)叉乘:matmul(a,b)
备注:
点乘与内积的异同:
- 相同点:点乘与内积的基本操作相同:每个元素相乘后再相加。
- 不同点:点乘只支持两个一维张量点乘,而内积支持多维张量的内积
点乘与叉乘:
- 相同点:点乘是基础,即对应元素相乘后相加。
- 不同点:点乘只支持两个一维张量点乘,而叉乘支持多维张量,每一个维度上的数据都是一次点乘。
第2章 向量的点乘(是基础):dot()
2.1 定义
概括地说,向量的内积(点乘/数量积)。
对两个向量执行点乘运算,就是对这两个向量对应位一一相乘之后求和的操作,如下所示,对于向量a和向量b:
注意:
- 这里要求一维向量a和向量b的行列数相同。
- 点乘的结果是一个标量(数量而不是向量)
2.2 向量内积的几何意义
(1)可用于计算计算两个向量之间的夹角.
θ=arccos(a∙b/|a||b|)
(2)b向量在a向量方向上的投影与a相乘
|a| = 所有元素的平方和开根号,实际上就是向量a的长度。
|b| = 所有元素的平方和开根号,实际上就是向量b的长度。
a.b = a1*b1 + a2*b2 ..... an*bn
(3)是否正交指示:
如果点乘的结果为0,则表示a在b上的投影为0,表示a和b是正交的。
如果正交,表示这两个向量不相干。
2.3 代码示例
#向量的点乘(点积)运算 a = torch.Tensor([1,2,3]) b = torch.Tensor([1,1,1]) print(a) print(b) print(torch.dot(a,b)) # 等价于 1*0+2*1+3*0
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
输出: tensor([1., 2., 3.]) tensor([1., 1., 1.]) tensor(6.)
- 1.
- 2.
- 3.
- 4.
- 5.
#向量的点乘(点积)运算 a = torch.Tensor([1,2,3]) b = torch.Tensor([1,1,1]) print(a) print(b) print(torch.vdot(a,b)) # 等价于 1*0+2*1+3*0
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
输出: tensor([1., 2., 3.]) tensor([1., 1., 1.]) tensor(6.)
- 1.
- 2.
- 3.
- 4.
- 5.
第3章 向量的叉乘
3.1 定义
两个向量的外积,又叫叉乘、叉积向量积,其运算结果是一个向量而不是一个标量。
并且两个向量的外积与这两个向量组成的坐标平面垂直。
定义:向量a与b的外积a×b是一个向量,其长度等于|a×b| = |a||b|sin∠(a,b),其方向正交于a与b。并且,(a,b,a×b)构成右手系。
特别地,0×a = a×0 = 0.此外,对任意向量a,自身相乘a×a=0。
对于向量a和向量b:
a和b的外积公式为(得到的是原先维度的向量):
3.2 几何意义
在三维几何中,向量a和向量b的外积结果是一个向量,有个更通俗易懂的叫法是法向量,该向量垂直于a和b向量构成的平面。
在3D图像学中,外积的概念非常有用,可以通过两个向量的外积,生成第三个垂直于a,b的法向量,从而构建X、Y、Z坐标系。如下图所示:
3.3 代码示例
# 向量的叉乘(乘积)运算 a = torch.Tensor([1,2,3]) b = torch.Tensor([1,1,1]) print(a) print(b) print(torch.multiply(a,b))
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
输出: tensor([1., 2., 3.]) tensor([1., 1., 1.]) tensor([1., 2., 3.])
- 1.
- 2.
- 3.
- 4.
- 5.
第4章 矩阵的内积运算(对应):inner()
4.1 矩阵内积的定义
两个相同维度的矩阵a和b,a和b矩阵的内积时相同位置的向量的内积。
(1)向量向量内积
(2)向量矩阵的内积:
4.2 代码示例
# 矩阵的内积运算 a = torch.Tensor([1,2,3]) b = torch.Tensor([0,1,0]) print(a) print(b) print(torch.inner(a,b)) # 等价于 1*0+2*1+3*0 print("") a = torch.Tensor([[0,1,0], [0,2,0]]) b = torch.Tensor([[0,3,0], [0,4,0]]) print(a) print(b) print(torch.inner(a,b)) # 等效于每个向量两两内积
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
输出: tensor([1., 2., 3.]) tensor([0., 1., 0.]) tensor(2.) tensor([[0., 1., 0.], [0., 2., 0.]]) tensor([[0., 3., 0.], [0., 4., 0.]]) tensor([[3., 4.], [6., 8.]])
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
第5章 矩阵的外积运算: matmul()
5.1 矩阵外积(矩阵乘积)的定义 (矩阵相乘)
矩阵相乘最重要的方法是一般矩阵乘积。它只有在第一个矩阵的列数(column)和第二个矩阵的行数(row)相同时才有意义。
(1)向量的乘积
(2)矩阵的乘积
5.2代码示例
# 外积 a = torch.Tensor([1,2,3]) # 相当于1* N b = torch.Tensor([0,1,0]) # 相当于N * 1 print(a) print(b) print(torch.matmul(a,b)) # 等价于 1*0+2*1+3*0 print("") a = torch.Tensor([[1,2,3], [4,5,6]]) b = torch.Tensor([[0,1], [1,1], [1,1]]) print(a) print(b) print(torch.matmul(a,b)) # X * N VS N * Y => X * Y
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
输出: tensor([1., 2., 3.]) tensor([0., 1., 0.]) tensor(2.) tensor([[1., 2., 3.], [4., 5., 6.]]) tensor([[0., 1.], [1., 1.], [1., 1.]]) tensor([[ 5., 6.], [11., 15.]])
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
版权声明
本文为[文火冰糖的硅基工坊]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_11299290/3312822
边栏推荐
- Fourth in the world! Wang Sicong installed a server "readily". Netizen: trench is inhuman
- [Tencent classroom] creator zero foundation immortal practice is online!
- 跟着华为,学数字化转型(3):模式创新
- 记一次接口慢查排查
- Follow Huawei and learn digital transformation (3): mode innovation
- Record an interface slow check and troubleshooting
- @Autowired的这些骚操作,你都知道吗?
- ss -h命令
- @Do you know all these operations of Autowired?
- 使用Yolo v5进行目标检测
猜你喜欢
-
Yazid的新生舞会(线段树)
-
当creator遇上protobufjs|孕育
-
Identify and stop the process that‘s listening on port 8080 or configure this application to listen
-
为什么要推荐大家学习字节码?
-
揭秘!价值百万的像素填色解决方案,想开发绘本应用的有福了!
-
[PyTroch系列-11]:PyTorch基础 - 张量Tensor元素的排序
-
[PyTroch系列-12]:PyTorch基础 - 张量Tensor线性运算(点乘、叉乘)
-
【环境篇】第 3 节 • Navicat 环境安装
-
预训练语言模型的前世今生 - 从Word Embedding到BERT
-
讲道理,只要你是一个爱折腾的程序员,毕业找工作真的不需要再花钱培训!
随机推荐
- 华南理工 | 基于生成式的低比特无数据量化
- 微信小程序授权位置和用户信息权限(防止用户禁止后无法使用位置信息)
- 一行代码快速实现今日头条 网易新闻焦点图自动循环轮播效果
- 因果涌现:数学理论揭示整体怎样大于部分之和
- 年收入百万美元AI科学家的烦恼
- API《为什么奥运会以五色环为标志?》数据源接口
- 用一张草图创建GAN模型,新手也能玩转,朱俊彦团队新研究入选ICCV 2021
- UIUC | 用于语言模型的课程学习
- SS - H command
- Target detection using Yolo V5
- Yazid's freshman ball (thread tree)
- When creator meets protobufjs 𞓜
- 我敢肯定!你还没用过一款代码神器,只属于Creator的用户!
- 小程序页面跳转&&文章详情页的实现&&markdown格式转化为wxml显示在小程序页面里
- 49个项目管理过程ITTO整理(详细)
- 49个项目管理过程ITTO整理(详细-文字版)
- 只是想虐下春丽,一不小心撸了台游戏机...
- Cocos论坛九问九答
- Identify and stop the process that‘s listening on port 8080 or configure this application to listen
- 超详细的I/O多路复用概念、常用I/O模型、系统调用等介绍
- Why recommend learning bytecode?
- SAP Commerce Cloud UI 的用户会话管理
- 以太坊 交易 data字段 内容是什么
- SAP CRM Fiori 应用 My Note 里创建 Note 失败的一个原因分析
- 当creator遇上protobufjs|pbkiller填坑历险记
- Uncover the secret! Millions of pixel color filling solutions. Blessed are those who want to develop picture book applications!
- [pytroch series - 11]: pytorch basis - ordering of tensor tensor elements
- [pytroch series - 12]: pytorch basis tensor tensor linear operation (point multiplication, cross multiplication)
- [environment] section 3 • Navicat environment installation
- The past and present life of pre training language model - from word embedding to Bert
- Make sense, as long as you are a tossing programmer, you really don't need to spend money on training to find a job after graduation!
- South China Technology | low bit no data quantization based on generative
- Wechat applet authorizes location and user information permissions (to prevent users from being unable to use location information after prohibition)
- One line of code can quickly realize the automatic circular rotation effect of today's headlines and Netease News focus map
- Causal emergence: mathematical theory reveals how the whole is greater than the sum of parts
- The troubles of AI scientists with an annual income of millions of dollars
- API "why is the Olympic Games marked by five color rings?" Data source interface
- Create a GaN model with a sketch, which can be played by novices. The new research of Zhu Junyan's team was selected into iccv 2021
- UIUC | course learning for language model
- I'm sure! You haven't used a code artifact yet. It only belongs to creator users!