当前位置:网站首页>Pytorch summary learning series - operation
Pytorch summary learning series - operation
2022-06-29 09:21:00 【TJMtaotao】
Arithmetic operations
stay PyTorch in , The same operation can take many forms , Next ⾯ Use addition as 例⼦.
Additive form ⼀
x = torch.tensor([5.5, 3])
y = torch.rand(5, 3)
print(x + y)
Additive form ⼆
print(torch.add(x, y))
You can also specify the output :
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Addition form 3 、inplace
# adds x to y
y.add_(x)
print(y)
Indexes
We can also make ⽤ Use similar NumPy To access Tensor Of ⼀ part , It should be noted that : The indexed results are similar to
Original data shared memory , That is, modify a , The other one will be modified .
y = x[0, :]
y += 1
print(y)
print(x[0, :]) # Source tensor Also changed 了
Except often ⽤ Index of the selected data ,PyTorch Some advanced selection functions are also provided :

Change shape
⽤ use view() To change Tensor The shape of the :
y = x.view(15)
z = x.view(-1, 5) # -1 The dimension can be derived from the values of other dimensions
print(x.size(), y.size(), z.size())
Output
torch.Size([5, 3]) torch.Size([15]) torch.Size([3, 5])
Be careful view() Back to the new tensor With the source tensor Shared memory ( In fact, it is the same as ⼀ One tensor), That is to say 更 Change one of them ⼀ One , another
Outside ⼀ One will change with it .( seeing the name of a thing one thinks of its function ,view It just changed 了 For this tensor 量 The observation of ⻆ angle )
x += 1
print(x)
print(y) # Also add 了1
So if we Want to go back ⼀ A really new copy ( namely 不 Shared memory ) What to do ?Pytorch It also provides 了⼀ One
individual reshape() You can change the shape , But this function does not 不 What can guarantee to return is its copy ⻉ shellfish , So don't 不 Recommend to make ⽤ use . Recommend first
use clone Create a copy and then make ⽤ view
x_cp = x.clone().view(15)
x -= 1
print(x)
print(x_cp)
send ⽤ use clone also ⼀ One advantage is that it will be recorded in the calculation diagram , That is, when the gradient is returned to the replica, it will also be transmitted to the source Tensor .
in addition ⼀ A regular ⽤ The function used is item() , It can be ⼀ A target 量 Tensor convert to ⼀ One Python number:
x = torch.randn(1)
print(x)
print(x.item())
Output
tensor([2.3466])
2.3466382026672363
linear algebra
in addition ,PyTorch also ⽀ Some linear functions are supported , There is no need to ⼰ Making a wheel ⼦, For specific usage, please refer to the official ⽅
file . As shown in the following table :

边栏推荐
- [target detection] | indicator a probabilistic challenge for object detection
- Verilog reduction operator
- 来个小总结吧
- Let's make a summary
- ServerApp.iopub
- pytorch总结—TENSOR ON GPU
- (transfer) mysql: error 1071 (42000): specified key was too long; max key length is 767 bytes
- Wechat applet project: tab navigation bar
- H5 soft keyboard problem
- Mongodb persistence
猜你喜欢

keras转tf.keras中VGG19 input_shape
![[target detection] | indicator a probabilistic challenge for object detection](/img/82/7b830d44bed7b1509cb0cdfed88e3e.png)
[target detection] | indicator a probabilistic challenge for object detection

Debug H5 page -vconsole

Detecting and counting tiny faces

Wechat applet sharing page, sharing to the circle of friends

H5 soft keyboard problem

Let's make a summary

MT yolov6 training and testing

Written test question "arrange version numbers from large to small"

Augfpn: improved multiscale feature learning for target detection
随机推荐
网络安全问题
PAT (Basic Level) Practice (中文)1003 我要通过! (20分) C语言实现
SSD improvements cfenet
Verilog data type
ActiveMQ message component publish subscribe redelivery message redelivery
Pointnet/pointnet++ training and testing
MySQL virtual column
深卷积神经网络时代的目标检测研究进展
ServerApp. iopub
Wechat applet custom multi selector
微信小程序项目:tab导航栏
UE4 去掉材质中Mask透明白边
NPM common commands
What is the difference between hyperconverged architecture and traditional architecture?
Highlight in the middle of the navigation bar at the bottom of wechat applet
HB5470民用飞机机舱内部非金属材料燃烧测试
Pat (basic level) practice (Chinese) 1003 I want to pass! (20 points) C language implementation
实例报错IOPub data rate exceeded
Abstract classes and interfaces
在 golang 中是如何对 epoll 进行封装的?