当前位置:网站首页>8. Mathematical operation and attribute statistics
8. Mathematical operation and attribute statistics
2022-07-27 05:59:00 【Pie star's favorite spongebob】
List of articles
Mathematical operations
One .torch.add()/minus()/multply()/divide()
It is recommended to use the operator directly
add
a=torch.rand(3,4)
b=torch.rand(4)
print(a)
print(b)
print(torch.add(a,b))

minus
multply
divide
Two .matmul
matrix multiplication A[a,b], ride B[b,c], Finally, the matrix C[a,c],C The value of the first row and the first column of is equal to A The first line of is multiplied by B The sum of the values in the first column of .
1.torch.mm
Only suitable for 2D
2.torch.matmul recommend
a=torch.ones(2,2)
b=torch.full([2,2],6,dtype=torch.float32)
c=torch.matmul(a,b)
print(a)
print(b)
print(c)

[4,3,28,64] multiply [4,3,64,32], The first two dimensions remain unchanged , The latter two dimensions follow 2D The operation rules of [4,3,28,32]
[4,3,28,64] multiply [4,1,64,32], The second satisfaction broadcast, become [4,3,64,32], And then multiply it .
a=torch.rand(4,3,28,64)
b=torch.rand(4,3,64,32)
c=torch.rand(4,1,64,32)
d=torch.matmul(a,b)
e=torch.matmul(a,c)
print(d.shape)
print(e.shape)

[email protected]
A more concise method , The same principle as the previous one
a=torch.rand(4,3,28,64)
b=torch.rand(4,3,64,32)
c=[email protected]
print(c.shape)

4.example
x=torch.rand(4,784)
w=torch.rand(512,784)
print(([email protected].t()).shape)
When we reduce the dimension , take [4,784] Multiplication matrix [784,512] available [4,512],w Not written [784,512], Because pytorch Fixed writing , First channe-out, Again channel-in.

3、 ... and .pow/exp/log
.pow()
a=torch.full([2,2],3)
print(a.pow(2))

torch.exp()
exp(a), seek e Of a Power .
a=torch.ones([2,2])
a=torch.exp(a)
print(a)

torch.log()
The log In order to e Base number
a=torch.ones([2,2])
a=torch.exp(a)
print(torch.log(a))

Four .sqrt/rsqrt
.sqrt()
square root
a=torch.full([2,2],3)
aa=a.pow(2)
print(aa.sqrt())

.rsqrt()
The reciprocal of the square root
a=torch.full([2,2],3)
aa=a.pow(2)
print(aa.rsqrt())

5、 ... and .approximation
1…floor()/.ceil()
.floor() Rounding down ,.ceil() Rounding up
a=torch.tensor(3.14)
print(a.floor(),a.ceil())

2…trunc()/.frac()
.trunc() Crop integer parts ,.frac() Cut the decimal part
a=torch.tensor(3.14)
print(a.trunc())
print(a.frac())

3…round()
.round() rounding
a=torch.tensor(3.54)
print(a.round())

6、 ... and …clamp() Use more
gradient clipping Gradient cut
a=torch.rand(2,3)*15
print(a)
print(a.clamp(8))
print(a.clamp(5,8))
a.clamp(8), Less than 8 To become 8
a.clamp(5,8) Count in 5 To 8 Between , Greater than 8 To become 8, Less than 5 To become 5.

Attribute Statistics
.norm()
1-norm:
P-norm:
.mean()/.sum()/.min()/.max()/.prod()
product, Multiplicative multiplication
a=torch.arange(8).view(2,4).float()
print(a)
print(a.mean())
print(a.sum())
print(a.min())
print(a.max())
print(a.prod())

dim/keepdim
a[4,10]
a.max(dim=1)z
tensor(0.8,),tensor(3,)
first tensor yes 4 Probability values , the second tensor It shows the location of each probability value ( Indexes ), The first one is 3 Is the probability that 0.8
To which dim Conduct max, Take one dim Equivalent to being eliminated .keepdim yes dim Be consistent with the original , return [4,1]
.argmax()/.argmin()
argmax() Returns the index of the maximum value
a[4,10]
a.argmax(dim=1), Then return to 4 One is the location of each probability value ( Indexes )
.argmin() Returns the index of the minimum value , Will be able to tensor Strike a level , Then look at the index , If you don't want to draw , Look directly at a dimension , The parameter (dim=)
a=torch.arange(8).view(2,4).float()
print(a)
print(a.argmax())
print(a.argmin())

.kthvalue()/.topk()
kthvalue() Return to the smallest k Number , amount to [4,10] obtain [4,k]
topk() Return to the biggest k Number , amount to [4,10] obtain [4,k]
compare
>,>= No true and false, It is 0 and 1.
torch.eq(a,b) Judge a and b Whether each position is equal
torch.equal(a,b) return true
边栏推荐
- golang控制goroutine数量以及获取处理结果
- 如果面试官问你 JVM,额外回答“逃逸分析”技术会让你加分
- GBASE 8C——SQL参考 5 全文检索
- Cap principle
- Gbase 8C - SQL reference 6 SQL syntax (1)
- Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
- Inno setup package jar + H5 + MySQL + redis into exe
- 数字图像处理 第二章 数字图像基础
- 万字解析MySQL索引原理——InnoDB索引结构与读取
- Deploy redis with docker for high availability master-slave replication
猜你喜欢
随机推荐
16.过拟合欠拟合
How to realize master-slave synchronization in mysql5.7
15.GPU加速、minist测试实战和visdom可视化
GBase 8c产品简介
Gbase 8C - SQL reference 6 SQL syntax (1)
2021中大厂php+go面试题(1)
GBASE 8C——SQL参考 5 全文检索
Digital image processing Chapter 4 - frequency domain filtering
Docker deploys the stand-alone version of redis - modify the redis password and persistence method
Minio8.x version setting policy bucket policy
golang控制goroutine数量以及获取处理结果
GBASE 8C——SQL参考6 sql语法(1)
Digital image processing Chapter 5 - image restoration and reconstruction
新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic
常用adb命令汇总 性能优化
Andorid detects GPU rendering speed and over rendering
Seven enabling schemes of m-dao help Dao ecology move towards mode and standardization
2021中大厂php+go面试题(2)
Fortex Fangda releases the electronic trading ecosystem to share and win-win with customers
Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例









