当前位置:网站首页>Pytorch learning 09: basic matrix operations
Pytorch learning 09: basic matrix operations
2022-06-22 00:58:00 【HMTT】
arithmetic
import torch
a = torch.tensor([
[1,2],
[3,4]
])
b = torch.tensor([
[10, 20]
])
# Add
print("torch.all(torch.eq(a+b, torch.add(a,b))):",
torch.all(torch.eq(a+b, torch.add(a,b))))
print("a+b:\n{}\n".format(a+b))
# reduce
print("torch.all(torch.eq(a-b, torch.sub(a,b))):",
torch.all(torch.eq(a-b, torch.sub(a,b))))
print("a*b:\n{}\n".format(a-b))
# ride ( It's point by point )
print("torch.all(torch.eq(a*b, torch.mul(a,b))):",
torch.all(torch.eq(a*b, torch.mul(a,b))))
print("a*b:\n{}\n".format(a*b))
# except
print("torch.all(torch.eq(a/b, torch.div(a,b))):",
torch.all(torch.eq(a/b, torch.div(a,b))))
print("a*b:\n{}\n".format(a/b))

matrix multiplication
import torch
a = torch.tensor([
[1],
[3]
])
b = torch.tensor([
[10, 20]
])
# mm Can only operate on at most two-dimensional matrices
print("torch.mm(a, b):\n{}\n".format(torch.mm(a, b)))
# matmul Computable higher dimensional matrices
print("torch.matmul(a, b):\n{}\n".format(torch.matmul(a, b)))
print("[email protected]:\n{}\n".format([email protected]))

Greater than 2 Matrix multiplication of dimensions
import torch
a1 = torch.rand(4, 3, 28, 64)
b1 = torch.rand(4, 3, 64, 32)
c1 = torch.matmul(a1, b1)
# Multiply the last two dimensions
# It can be understood as the parallel multiplication of multiple matrices
print("c1.shape: ", c1.shape)
a2 = torch.rand(4, 1, 28, 64)
b2 = torch.rand(4, 3, 64, 32)
c2 = torch.matmul(a2, b2)
# Here's the broadcast mechanism
print("c2.shape: ", c2.shape)

Power operation
import torch
a = torch.tensor([
[1, 2],
[3, 4]
])
print("a.pow(2):\n{}\n".format(a.pow(2)))
print("a**2:\n{}\n".format(a**2))
print("a.pow(0.5):\n{}\n".format(a.pow(0.5)))
print("a.sqrt():\n{}\n".format(a.sqrt()))
# The reciprocal of the square root
print("a.rsqrt():\n{}\n".format(a.rsqrt()))
print("a**0.5:\n{}\n".format(a**0.5))

exp log
import torch
a = torch.tensor([
[1, 2],
[3, 4]
])
a_exp = torch.exp(a)
# e^x
print("torch.exp(a):\n{}\n".format(a_exp))
# ln x
# With 2 Bottom :log2
# With 10 Bottom :log10
print("torch.log(a_exp):\n{}\n".format(torch.log(a_exp)))

Approximate value
import torch
a = torch.tensor(1.67)
# Rounding down
print("a.floor():", a.floor())
# Rounding up
print("a.ceil():", a.ceil())
# Take the whole part
print("a.trunc():", a.trunc())
# Take the decimal part
print("a.frac():", a.frac())
# rounding
print("a.round():", a.round())

Maximum 、 minimum value 、 Median
import torch
a = torch.rand(2,3)*20
print("a:\n{}\n".format(a))
# Maximum
print("a.max(): ", a.max())
# Median , Even numbers are not averaged , From small to large length/2 individual
print("a.median(): ", a.median())
# minimum value
print("a.min(): ", a.min())

Restricted interval
import torch
a = torch.rand(2,3)*20
print("a:\n{}\n".format(a))
# clamp(min), When there is a value less than min when , use min Replace
print("a.clamp(10):\n{}\n".format(a.clamp(10)))
# clamp(min, max), When there is a value less than min when , use min Replace
# When there is a value greater than max when , use max Replace
print("a.clamp(5, 10):\n{}\n".format(a.clamp(5, 10)))

边栏推荐
- Lecture 3 of Data Engineering Series: characteristic engineering of data centric AI
- It took 2 hours to build an Internet of things project, which is worth~
- Bit operation bit or
- Is xshell worse than SecureCRT?
- Use of MySQL performance analysis tools
- HarmonyOS应用开发第二次作业笔记
- DOM node
- 合理选择液压滑环密封间隙的重要性
- 导电滑环是如何工作的
- Go Technology Daily (June 20, 2022) -- go: simple optimization notes
猜你喜欢
随机推荐
Leetcode做题目录
导电滑环是如何工作的
pytorch学习05:索引和切片
鸿蒙OS学习(轮播图、列表、图标)
Pytorch learning 04: creation of tensor
MySQL 8.0 新特性梳理汇总
pytorch学习12:自动求导
SQL statement - permission management
过孔式导电滑环怎么用
调试中的虚变量和格式化字符
三种文件句柄之间的转换
对面积的曲面积分中dS与dxdy的转换
The tangled truth about NFT and copyright
【环境踩坑】用opencv打开图片时报错
Blazor data binding
四数之和[数组排序+双指针]
小小协议大威力,数字化转型为何缺不了NVMe全闪存?
Opérations de bits bits et
【环境踩坑】在自己电脑上搭建FastDFS
如何判断一个男人将来是穷还是富?









