当前位置:网站首页>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
边栏推荐
- Inno setup package jar + H5 + MySQL + redis into exe
- ES对比两个索引的数据差
- 如果面试官问你 JVM,额外回答“逃逸分析”技术会让你加分
- GBASE 8C——SQL参考6 sql语法(3)
- GBASE 8C——SQL参考6 sql语法(13)
- Docker deploys the stand-alone version of redis - modify the redis password and persistence method
- 2021中大厂php+go面试题(2)
- 关于pytorch反向传播的思考
- How MySQL and redis ensure data consistency
- Gbase 8C - SQL reference 6 SQL syntax (5)
猜你喜欢

7.合并与分割

Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease

西瓜书学习第五章---神经网络

Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例

Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis

9.高阶操作

The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market

Move protocol launched a beta version, and you can "0" participate in p2e

Do you really know session and cookies?

根据文本自动生成UML时序图(draw.io格式)
随机推荐
GBase 8c核心技术
数字图像处理 第二章 数字图像基础
Inno setup package jar + H5 + MySQL + redis into exe
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter
Minio8.x version setting policy bucket policy
How to not overwrite the target source data when dBSwitch data migrates data increments
关于pytorch反向传播的思考
Docker deploys the stand-alone version of redis - modify the redis password and persistence method
Cap principle
php 定义数组使用逗号,
一张照片攻破人脸识别系统:能点头摇头张嘴,网友
GBASE 8C——SQL参考6 sql语法(3)
Graph node deployment
GBASE 8C——SQL参考6 sql语法(2)
GBASE 8C——SQL参考6 sql语法(10)
GBASE 8C——SQL参考6 sql语法(11)
Gbase 8C - SQL reference 5 full text search
Day10. Work organization and mental health problems in PhD students
Brief analysis of application process creation process of activity