没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Pytorch Tensor基本数学运算详解基本数学运算详解
今天小编就为大家分享一篇Pytorch Tensor基本数学运算详解,具有很好的参考价值,希望对大家有所帮助。一
起跟随小编过来看看吧
1. 加法运算加法运算
示例代码:
import torch
# 这两个Tensor加减乘除会对b自动进行Broadcasting
a = torch.rand(3, 4)
b = torch.rand(4)
c1 = a + b
c2 = torch.add(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
输出结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
2. 减法运算减法运算
示例代码:
a = torch.rand(3, 4)
b = torch.rand(4)
c1 = a - b
c2 = torch.sub(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
输出结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
3. 哈达玛积哈达玛积(element wise,对应元素相乘,对应元素相乘)
示例代码:
c1 = a * b
c2 = torch.mul(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
输出结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
4. 除法运算除法运算
示例代码:
c1 = a / b
c2 = torch.div(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
输出结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
5. 矩阵乘法矩阵乘法
(1)二维矩阵相乘
二维矩阵乘法运算操作包括torch.mm()、torch.matmul()、@,
资源评论
weixin_38725119
- 粉丝: 4
- 资源: 952
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功