当前位置:网站首页>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
边栏推荐
- Minio fragment upload lifting fragment size limit - chunk size must be greater than 5242880
- Emoji表情符号用于文本情感分析-Improving sentiment analysis accuracy with emoji embedding
- 如果在线上遇到了OOM,该如何解决?
- GBASE 8C——SQL参考6 sql语法(10)
- MySQL快速比较数据库表数据
- Performance optimization of common ADB commands
- GBASE 8C——SQL参考6 sql语法(12)
- 数字图像处理——第三章 灰度变换与空间滤波
- GBASE 8C——SQL参考6 sql语法(6)
- 10.梯度、激活函数和loss
猜你喜欢

数字图像处理——第九章 形态学图像处理

19.上下采样与BatchNorm

NFT new paradigm, okaleido innovation NFT aggregation trading ecosystem

If you encounter oom online, how to solve it?

4.张量数据类型和创建Tensor

Day 9. Graduate survey: A love–hurt relationship

新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

Sealem Finance - a new decentralized financial platform based on Web3

The NFT market pattern has not changed. Can okaleido set off a new round of waves?
随机推荐
How to not overwrite the target source data when dBSwitch data migrates data increments
Performance optimization of common ADB commands
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
GBASE 8C——SQL参考4 字符集支持
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
「中高级试题」:MVCC实现原理是什么?
Emoji Emoji for text emotion analysis -improving sentimental analysis accuracy with Emoji embedding
GBASE 8C——SQL参考6 sql语法(9)
NFT new paradigm, okaleido innovation NFT aggregation trading ecosystem
Global evidence of expressed sentimental alterations during the covid-19 pandemics
How to realize master-slave synchronization in mysql5.7
数字图像处理——第六章 彩色图像处理
【MVC架构】MVC模型
18.卷积神经网络
4.张量数据类型和创建Tensor
vim编辑器全部删除文件内容
14.实例-多分类问题
舆情&传染病时空分析文献阅读笔记
MySQL索引失效与解决方法实践
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon