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
猜你喜欢
- 写在前面很长一段时间内,我都在研究在线地图的开发者文档,百度地图和高德地图的开发者中心提供了丰富的在线地图服务,虽然有一定的权限限制,但不得
- 本文实例讲述了python日期相关操作。分享给大家供大家参考,具体如下:用 Python 做项目时,经常会遇到与日期转换相关,日期计算相关的
- memoryview() 函数返回给定参数的内存查看对象(Momory view)。语法memoryview 语法:memoryview(o
- AddHeaderAddHeader 方法用指定的值添加 HTML 标题。该方法常常向响应添加新的 HTTP 标题。它并不替代现有的同名标题
- 开发背景是这样的:整个项目中使用很多台摩托罗拉的RFID读卡器,我要为这些读卡器写一个管理程序,判断是否有RFID标签进入或离开某个区域。用
- 本文实例为大家分享了React实现表格选取的具体代码,供大家参考,具体内容如下在工作中,遇到一个需求,在表格中实现类似于Excel选中一片区
- 一个ASP文件通常包含HTML标签,有时和一个HTML文件非常类似。然而,ASP文件(除了包含HTML标签外),还可以包括服务器的脚本程序,
- 代码和说明如下:<%Const ForReading = 1 &nbs
- 导读:最近使用Thinkphp5.1做开发,在使用LOG_PATH常量(日志路径)时发生报错,因为之前一直使用5.0的框架,换到5.1版本后
- 昨天又翻了下前段时间WD内部培训的幻灯片,发现了kejun推荐的一篇好文:Javascript Closures,看了之后受益匪浅。这篇文章
- asp自定义错误显示方法:<html><head><meta http-equiv="Co
- //TransmitFile实现下载protected void Button1_Click(object sender, EventArg
- Yoho, 大家好,又是我哟~ 首先抱歉让大家等了这么多时间。最近实在比较繁忙啦。不过我还是会尽量抽空出来给大家讲点有的没的,欢迎大家继续
- 如何 在Access中选择指定日期前的记录?我知道,在Access中不能使用DateDiff,那么我们怎样才能选择指定日期前的若干记录呢?虽
- 在使用Python做开发的时候,时不时会给自己编写了一些小工具辅助自己的工作,但是由于开发依赖环境问题,多数只能在自己电脑上运行,拿到其它电
- 作为语言模型和文本挖掘中的常用工具,Word2Vec也可以用来构建聊天机器人。在本文中,我们将使用Python和Gensim库从头开始构建一
- 为了庆祝jQuery的四周岁生日, jQuery的团队荣幸的发布了jQuery Javascript库的最新主要版本! 这个版本包含了大量的
- 本文实例解析Python SqlAlchemy的使用方法,分享给大家供大家参考,具体内容如下1.初始化连接from sqlalchemy i
- 这篇文章主要介绍了微信小程序wxml列表渲染原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 前言本文主要给大家介绍了关于golang分页算法的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧示例代码如下://