pytorch tensor内所有元素相乘实例
作者:某C姓工程师傅 发布时间:2023-05-16 15:28:16
tensor内所有元素相乘
a = torch.Tensor([1,2,3])
print(torch.prod(a))
输出
tensor(6.)
tensor乘法运算汇总与解析
元素一一相乘
该操作又称作 “哈达玛积”, 简单来说就是 tensor 元素逐个相乘。这个操作,是通过 * 也就是常规的乘号操作符定义的操作结果。torch.mul 是等价的。
import torch
def element_by_element():
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
return x * y, torch.mul(x, y)
element_by_element()
(tensor([ 4, 10, 18]), tensor([ 4, 10, 18]))
这个操作是可以 broad cast 的。
def element_by_element_broadcast():
x = torch.tensor([1, 2, 3])
y = 2
return x * y
element_by_element_broadcast()
tensor([2, 4, 6])
向量点乘
torch.matmul: If both tensors are 1-dimensional, the dot product (scalar) is returned.
如果都是1维的,返回的就是 dot product 结果
def vec_dot_product():
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
return torch.matmul(x, y)
vec_dot_product()
tensor(32)
矩阵乘法
torch.matmul: If both arguments are 2-dimensional, the matrix-matrix product is returned.
如果都是2维,那么就是矩阵乘法的结果返回。与 torch.mm 是等价的,torch.mm 仅仅能处理的是矩阵乘法。
def matrix_multiple():
x = torch.tensor([
[1, 2, 3],
[4, 5, 6]
])
y = torch.tensor([
[7, 8],
[9, 10],
[11, 12]
])
return torch.matmul(x, y), torch.mm(x, y)
matrix_multiple()
(tensor([[ 58, 64],
[139, 154]]), tensor([[ 58, 64],
[139, 154]]))
vector 与 matrix 相乘
torch.matmul: If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.
如果第一个是 vector, 第二个是 matrix, 会在 vector 中增加一个维度。也就是 vector 变成了 与 matrix 相乘之后,变成 , 在结果中将 维 再去掉。
def vec_matrix():
x = torch.tensor([1, 2, 3])
y = torch.tensor([
[7, 8],
[9, 10],
[11, 12]
])
return torch.matmul(x, y)
vec_matrix()
tensor([58, 64])
matrix 与 vector 相乘
同样的道理, vector会被扩充一个维度。
def matrix_vec():
x = torch.tensor([
[1, 2, 3],
[4, 5, 6]
])
y = torch.tensor([
7, 8, 9
])
return torch.matmul(x, y)
matrix_vec()
tensor([ 50, 122])
带有batch_size 的 broad cast乘法
def batched_matrix_broadcasted_vector():
x = torch.tensor([
[
[1, 2], [3, 4]
],
[
[5, 6], [7, 8]
]
])
print(f"x shape: {x.size()} \n {x}")
y = torch.tensor([1, 3])
return torch.matmul(x, y)
batched_matrix_broadcasted_vector()
x shape: torch.Size([2, 2, 2])
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
tensor([[ 7, 15],
[23, 31]])
batched matrix x batched matrix
def batched_matrix_batched_matrix():
x = torch.tensor([
[
[1, 2, 1], [3, 4, 4]
],
[
[5, 6, 2], [7, 8, 0]
]
])
y = torch.tensor([
[
[1, 2],
[3, 4],
[5, 6]
],
[
[7, 8],
[9, 10],
[1, 2]
]
])
print(f"x shape: {x.size()} \n y shape: {y.size()}")
return torch.matmul(x, y)
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")
x shape: torch.Size([2, 2, 3])
y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2])
tensor([[[ 12, 16],
[ 35, 46]],
[[ 91, 104],
[121, 136]]])
上面的效果与 torch.bmm 是一样的。matmul 比 bmm 功能更加强大,但是 bmm 的语义非常明确, bmm 处理的只能是 3维的。
def batched_matrix_batched_matrix_bmm():
x = torch.tensor([
[
[1, 2, 1], [3, 4, 4]
],
[
[5, 6, 2], [7, 8, 0]
]
])
y = torch.tensor([
[
[1, 2],
[3, 4],
[5, 6]
],
[
[7, 8],
[9, 10],
[1, 2]
]
])
print(f"x shape: {x.size()} \n y shape: {y.size()}")
return torch.bmm(x, y)
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")
x shape: torch.Size([2, 2, 3])
y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2])
tensor([[[ 12, 16],
[ 35, 46]],
[[ 91, 104],
[121, 136]]])
tensordot
def tesnordot():
x = torch.tensor([
[1, 2, 1],
[3, 4, 4]])
y = torch.tensor([
[7, 8],
[9, 10],
[1, 2]])
print(f"x shape: {x.size()}, y shape: {y.size()}")
return torch.tensordot(x, y, dims=([0], [1]))
tesnordot()
x shape: torch.Size([2, 3]), y shape: torch.Size([3, 2])
tensor([[31, 39, 7],
[46, 58, 10],
[39, 49, 9]])
来源:https://blog.csdn.net/weixin_41185456/article/details/104328982


猜你喜欢
- 1.v-for直接上代码。示例一:<!DOCTYPE html><html><head> <met
- 效果如下:(动态效果可复制代码查看,案例中的图片可自行选择添加)代码如下:<!DOCTYPE html><html lan
- 原来看过MYSQL同步数据的实现,可是自己还没有动过手,今天没什么事就玩一玩,正好在旁边有另一台空电脑,都在同一个路由器下。哈哈,正好。 不
- 连接mysql#!/usr/bin/python#-*- coding:utf-8 -*-import timeimport pymysql
- 一、垃圾还是经典网页技术更新很快,一个网站的界面设计寿命仅仅2-3年而已。不管是垃圾还是精品,都没有所谓的经典。经典只存在于是哪个首次成功创
- 一 前言 问题的存在 从代码级别上,也就是应用层次上考虑代码安全的话(也就是不考虑底层的语言本身等问题的漏洞),脚本安全问题就是函数和变量的
- 本文实例讲述了Python数据预处理之数据规范化。分享给大家供大家参考,具体如下:数据规范化为了消除指标之间的量纲和取值范围差异的影响,需要
- 不能将 SQL Server 2000 日志传送配置升级到 SQL Server 2008。数据库维护计划向导是 SQL Server 20
- QSS介绍前言QSS即Qt样式表,是用来自定义控件外观的一种机制,QSS大量参考了Css的内容,但QSS的功能要比Css弱得多,体现在选择器
- 前沿对于iOS开发不要随便拆卸系统自带的Python,因为有很多 library 还是使用 Python2.7。1 安装Xcode1.1 A
- python中的集合什么是集合?集合是一个无序的不重复元素序列常用来对两个列表进行交并差的处理集合与列表一样,支持所有数据类型集合与列表的区
- 前言从前面已经知道, 一个 request 的到来和一个对应 response 的返回的流程, 数据处理和数据库离不开. 我们也经常在 vi
- python中注释在python中的注释一般分为单行注释、多行注释以及文档注释。注释描述在实际开发过程中,有效的代码注释不仅可以提升个人的工
- 本文实例讲述了Python实现从SQL型数据库读写dataframe型数据的方法。分享给大家供大家参考,具体如下:Python的pandas
- 本文将详细解释这些函数的使用方法。首先,我们介绍Python语言中类似于Windows系统的dir命令的列出文件功能,然后描述如何测试一个文
- 一、项目需求前言:BBS上每个id对应一个用户,他们注册时候会填写性别(男、女、保密三选一)。经过检查,BBS注册用户的id对应1-3000
- 一般情况下,网站的图片代码是这样的。<img src="./images/test.jpg"
- flask中的sqlalchemy 相比于sqlalchemy封装的更加彻底一些 , 在一些方法上更简单首先import类库:在CODE上查
- mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法。python操
- 目录:分析和设计组件编码实现和算法用 Ant 构建组件测试 JavaScript 组件话说上期我们讨论了队列管理组件的设计,并且给它取了个响