当前位置:网站首页>Pytorch Basics (Introductory)
Pytorch Basics (Introductory)
2022-07-29 06:11:00 【Quinn-ntmy】
PyTorch Basics
- The core is tensor Tensor, A kind of Multidimensional Mathematical object of data .
- Use torch Package creation tensor , The concrete is very simple , Not much said .
(1) If you want to use a specific value fill tensor , have access to fill_() Method .【 Any underlined (_) Of PyTorch Methods all refer to in situ operation , That is, there is no need to create new objects Modify in place Content 】
(2) When using torch.Tensor Constructor , The default tensor type is torch.FloatTensor. You can perform type conversion when using or utilize torch.tensor() Medium dtype Parameters . - The operations of addition, subtraction, multiplication and division are similar to others .
- torch.transpose(): Transpose operation , It's simple , May refer to https://blog.csdn.net/qq_50001789/article/details/120451717
- General indexing and slicing : Basically the same as numpy.
- Complex index :
(1) Discontinuous index of tensor
def describe(x):
print("Type: {}".format(x.type()))
print("Shape/size: {}".format(x.shape))
print("Values: \n{}".format(x))
import torch
# Create a 2D tensor
x = torch.arange(6).view(2, 3)
describe(x)
# 1、 obtain 2D First order of tensor 2 Dimension and index number is 0 and 2 A quantum set of
indices = torch.LongTensor([0, 2])
describe(torch.index_select(x, dim=1, index=indices))
# 2、 obtain 2D First order of tensor 1 Dimension and index number is 0 and 0 A quantum set of
indices = torch.LongTensor([0, 0])
describe(torch.index_select(x, dim=0, index=indices))
# 3、row_indices The result is a tensor([0., 1.]), then col_indices yes tensor([1, 2])
row_indices = torch.arange(2).long()
col_indices = torch.LongTensor([1, 2])
describe(x[row_indices, col_indices])
Output results :
Type: torch.LongTensor
Shape/size: torch.Size([2, 3])
Values:
tensor([[0, 1, 2],
[3, 4, 5]])
Type: torch.LongTensor
Shape/size: torch.Size([2, 2])
Values:
tensor([[0, 2],
[3, 5]])
Type: torch.LongTensor
Shape/size: torch.Size([2, 3])
Values:
tensor([[0, 1, 2],
[0, 1, 2]])
Type: torch.LongTensor
Shape/size: torch.Size([2])
Values:
tensor([1, 5])
Process finished with exit code 0
Explain the last operation (3),
(2) Connection tensor
import torch
x = torch.arange(6).view(2, 3)
describe(x)
describe(torch.cat([x, x], dim=0))
describe(torch.cat([x, x], dim=1))
describe(torch.stack([x, x]))
result :
Type: torch.LongTensor
Shape/size: torch.Size([2, 3])
Values:
tensor([[0, 1, 2],
[3, 4, 5]])
Type: torch.LongTensor
Shape/size: torch.Size([4, 3])
Values:
tensor([[0, 1, 2],
[3, 4, 5],
[0, 1, 2],
[3, 4, 5]])
Type: torch.LongTensor
Shape/size: torch.Size([2, 6])
Values:
tensor([[0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5]])
Type: torch.LongTensor
Shape/size: torch.Size([2, 2, 3])
Values:
tensor([[[0, 1, 2],
[3, 4, 5]],
[[0, 1, 2],
[3, 4, 5]]])
Process finished with exit code 0
- Tensor requires_grad=True when , You can track gradient tensors and gradient functions . These two things need gradient based learning “ Supervised learning paradigm ”.
Create tensors and calculate gradients :
import torch
x = torch.ones(2, 2, requires_grad=True)
describe(x)
print(x.grad is None)
y = (x+2)*(x+5)+3
describe(y)
print(x.grad is None)
z = y.mean()
describe(z)
z.backward()
print(x.grad is None)
result :
Type: torch.FloatTensor
Shape/size: torch.Size([2, 2])
Values:
tensor([[1., 1.],
[1., 1.]], requires_grad=True)
True
Type: torch.FloatTensor
Shape/size: torch.Size([2, 2])
Values:
tensor([[21., 21.],
[21., 21.]], grad_fn=<AddBackward0>)
True
Type: torch.FloatTensor
Shape/size: torch.Size([])
Values:
21.0
False
Process finished with exit code 0
Gradient is a value ( The slope of function output to function input ).
- CUDA tensor
(1) establish cuda tensor
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
x = torch.rand(3, 3).to(device)
describe(x)
result :
cuda
Type: torch.cuda.FloatTensor
Shape/size: torch.Size([3, 3])
Values:
tensor([[0.2369, 0.9929, 0.6972],
[0.1366, 0.0594, 0.0726],
[0.4803, 0.1209, 0.6055]], device='cuda:0')
(2)CUDA Tensor and CPU Binding tensor mixing
# take CUDA Tensor sum CPU Binding tensor mixing (CUDA He Fei CUDA object ),y stay CPU On ,x stay GPU On . In this case, an error will be reported
y = torch.rand(3, 3)
x + y
The result is wrong :
Traceback (most recent call last):
File "C:/Users/27692/PycharmProjects/Quinn/test/test.py", line 60, in <module>
x + y
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
If you want to be right cuda He Fei cuda object To operate , It must be ensured that they are Same device On :
cpu_device = torch.device("cpu")
y = y.to(cpu_device)
x = x.to(cpu_device)
print(x + y)
result :
tensor([[0.9204, 0.8360, 0.6289],
[1.2421, 1.4353, 1.2174],
[0.9342, 1.0427, 1.1978]])
Process finished with exit code 0
【 Practice expanding 】
- unsqueeze() Functions and squeeze() function :
a = torch.rand(2, 3)
print(describe(a))
# In the 0 Dimension increase 1 Dimensions
b = a.unsqueeze(0)
print(describe(b))
# In the 1 Dimension increase 1 Dimensions
b = a.unsqueeze(1)
print(describe(b))
# In the last place 2 Dimensions increase 1 Dimensions
b = a.unsqueeze(-2)
print(describe(b))
Pay attention to the output :!!! Tensor shape (torch.size)
Type: torch.FloatTensor
Shape/size: torch.Size([2, 3])
Values:
tensor([[0.2643, 0.1925, 0.2562],
[0.7674, 0.9930, 0.2341]])
None
Type: torch.FloatTensor
Shape/size: torch.Size([1, 2, 3])
Values:
tensor([[[0.2643, 0.1925, 0.2562],
[0.7674, 0.9930, 0.2341]]])
None
Type: torch.FloatTensor
Shape/size: torch.Size([2, 1, 3])
Values:
tensor([[[0.2643, 0.1925, 0.2562]],
[[0.7674, 0.9930, 0.2341]]])
None
Type: torch.FloatTensor
Shape/size: torch.Size([2, 1, 3])
Values:
tensor([[[0.2643, 0.1925, 0.2562]],
[[0.7674, 0.9930, 0.2341]]])
None
Process finished with exit code 0
# ======squeeze()======
# Above is insert / increase , This is deleted / Get rid of
a = torch.rand(2, 1, 3)
print(describe(a))
# Will be the first 1 Dimension out of
b = a.squeeze(1) # a.squeeze(-2) It has the same effect second to last
print(describe(b))
# try -3
b = a.squeeze(-3) # This result remains unchanged , Because only the dimension is 1 Will be removed when
print(describe(b))
Output results :
Type: torch.FloatTensor
Shape/size: torch.Size([2, 1, 3])
Values:
tensor([[[0.4349, 0.0568, 0.0304]],
[[0.2712, 0.3612, 0.3238]]])
None
Type: torch.FloatTensor
Shape/size: torch.Size([2, 3])
Values:
tensor([[0.4349, 0.0568, 0.0304],
[0.2712, 0.3612, 0.3238]])
None
Type: torch.FloatTensor
Shape/size: torch.Size([2, 1, 3])
Values:
tensor([[[0.4349, 0.0568, 0.0304]],
[[0.2712, 0.3612, 0.3238]]])
None
Process finished with exit code 0
- Create a tensor with normal distribution :
a = torch.rand(3,3)
a.normal_()
边栏推荐
- 【Transformer】ACMix:On the Integration of Self-Attention and Convolution
- 京微齐力:基于HMEP060的心率血氧模块开发(1:FPGA发送多位指令)
- PyTorch基础知识(可入门)
- 【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
- [target detection] KL loss: bounding box progression with uncertainty for accurate object detection
- 逻辑回归-项目实战-信用卡检测任务(下)
- Wechat applet source code acquisition (download with tools)
- 第三周周报 ResNet+ResNext
- 2021-06-10
- 迁移学习—Geodesic Flow Kernel for Unsupervised Domain Adaptation
猜你喜欢
"Full flash measurement" database acceleration solution
【卷积核设计】Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs
京微齐力:基于HMEP060的心率血氧模块开发(1:FPGA发送多位指令)
电脑视频暂停再继续,声音突然变大
【Attention】Visual Attention Network
Is flutter being quietly abandoned? On the future of flutter
[convolution kernel design] scaling up your kernels to 31x31: revising large kernel design in CNN
GAN:生成对抗网络 Generative Adversarial Networks
Improve quality with intelligence financial imaging platform solution
基于STM32开源:磁流体蓝牙音箱(包含源码+PCB)
随机推荐
[network design] convnext:a convnet for the 2020s
Improve quality with intelligence financial imaging platform solution
Ml17 neural network practice
How to perform POC in depth with full flash distribution?
迁移学习笔记——Adapting Component Analysis
Analysis on the principle of flow
ML9自学笔记
Wechat applet source code acquisition (download with tools)
预训练语言模型的使用方法
2、 Multi concurrent interface pressure test
零基础学FPGA(五):时序逻辑电路设计之计数器(附有呼吸灯实验、简单组合逻辑设计介绍)
1、 Multiprocessing.pool.remotetraceback
【Transformer】AdaViT: Adaptive Vision Transformers for Efficient Image Recognition
GAN:生成对抗网络 Generative Adversarial Networks
2022春招——上海安路FPGA岗面经(以及乐鑫SOC面试)
3、 How to customize data sets?
备份谷歌或其他浏览器插件
Briefly talk about the difference between pendingintent and intent
1、 Pytorch Cookbook (common code Collection)
[semantic segmentation] full attention network for semantic segmentation