当前位置:网站首页>Pytorch learning notes 2 - about tensor
Pytorch learning notes 2 - about tensor
2022-07-28 06:27:00 【I have two candies】
List of articles
Tensor yes PyTorch A special matrix storage structure in , And numpy similar , It's just tensors have access to GPU Speed up the operation
1. Tensor Initialization
You can initialize in the following ways tensor:
1. From data
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
2. From numpy array
np_array = np.array(data)
x_data = torch.from_numpy(np_array)
3. From another tensor
x_ones = torch.rand_like(x_data, dtype=torch.float)
print(x_data)
4. Rand or const tensor
rand_tensor = torch.rand(size=(2, 3))
ones_tensor = torch.ones(size=(2, 3))
zeros_tensor = torch.zeros(size=(2, 3))
print(rand_tensor)
print(ones_tensor)
print(zeros_tensor)
tensor & numpy
CPU Upper tensors And Numpy The matrix physically shares storage units , And through tensor.numpy() and torch.from_numpy() Interconversion :
# convert torch to numpy
t = torch.ones(5)
n = t.numpy()
print(t) # tensor([1., 1., 1., 1., 1.])
print(n) # [1. 1. 1. 1. 1.]
# torch and numpy array share the same underlying memory
t.add_(3)
print(t) # tensor([4., 4., 4., 4., 4.])
print(n) # [4. 4. 4. 4. 4.]
# convert numpy to torch
n = np.array([1, 1, 1])
t = torch.from_numpy(n)
print(t) # tensor([1, 1, 1], dtype=torch.int32)
print(n) # [1. 1. 1.]
2. Tensor Attributes
Tensor Attributes Can describe tensor Of shape,datatype And the stored device:
tensor = torch.randn(size=(2, 3), requires_grad=True, device='cuda')
print(f'Shape: {
tensor.shape}')
print(f'Datatype: {
tensor.dtype}')
print(f'Device tensor stored on: {
tensor.device}')
# Shape: torch.Size([2, 3])
# Datatype: torch.float32
# Device tensor stored on: cuda:0
3. Tensor Operations
Over 100 tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random sampling, and more are comprehensively described here.
Each of them can be run on the GPU (at typically higher speeds than on a CPU). If you’re using Colab, allocate a GPU by going to Edit > Notebook Settings.
Use GPU operation tensors:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
tensor1 = torch.rand(size=(2, 3))
tensor2 = tensor1.to(device)
print(f'Device tensor stored on: {
tensor2.device}')
# Device tensor stored on: cuda:0
1. Standard numpy-like indexing and slicing
take index=1 The column of is set to zero :
tensor = torch.ones(3, 3)
tensor[:, 1] = 0
print(tensor)
2. Joining tensors
adopt torch.cat([t1, t2, t3], dim=1) By column (dim=1 Specify to merge by the second dimension column ,dim=0 To merge by the first dimension row ) Merge multiple tensors
tensor = torch.ones(3, 3)
t1 = torch.cat([tensor, tensor], dim=0)
print(t1.shape) # torch.Size([6, 3])
print(t1)
t2 = torch.cat([tensor, tensor, tensor], dim=1)
print(t2.shape) # torch.Size([3, 9])
3. Multiplying tensors
Multiplication includes element multiplication and matrix multiplication :
- Element multiplication :
*,t1.mul(t2) - Matrix multiplication :
t1.matmul(t2),t1 @ t2
# element-wise product
t1 = torch.ones(2, 2)
print(t1.mul(t1))
print(t1 * t1)
# matrix multiplication
t1 = torch.ones(size=(2, 3))
print(t1.matmul(t1.T))
print(t1 @ t1.T)
among ,T Transposition of representative matrix
Be careful :numpy in , Multiply the element by *, Matrix multiply by np.dot(A, B),A.dot(B) and np.matmul(A, B)
4. In-place operations
with _ The suffix operator is in-place Operator :
tensor = torch.rand(2, 2)
tensor.add_(1) # add
print(tensor)
tensor.copy_(torch.rand(2, 2)) # copy
print(tensor)
That's all ~
More reference PyTorch Learning notes
REFERENCE: Tensors
边栏推荐
- 简述EMD分解、希尔伯特变换、谱方法
- VB OCX applied to Web
- In asp Usage of cookies in. Net
- 当mysql表从压缩表变成普通表会发生什么
- Bert bidirectional encoder based on transformer
- How to use the bit error meter?
- Selection of PLC
- Fluke fluke aircheck WiFi tester cannot configure file--- Ultimate solution experience
- IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha
- ConNeXt
猜你喜欢

Efficient Net_ V2

VAN(DWConv+DWDilationConv+PWConv)

Fluke fluke aircheck WiFi tester cannot configure file--- Ultimate solution experience

Pycharm2019 set editor theme and default code

雷达成像 Matlab 仿真 4 —— 距离分辨率分析

Shuffle Net_v1-shuffle_v2

EMC experiment practical case ESD electrostatic experiment

Word邮件合并功能详解:合并后生成多个word文档,删除空白页

ICC2(四)Routing and Postroute Optimization

set_ false_ path
随机推荐
基于 YOLOV5 的 SAR 图像舰船检测
PyTorch 学习笔记
ICC2(一)Preparing the Design
(PHP graduation project) obtained based on PHP novel website management system
福禄克DSX2-5000 网络线缆测试仪为什么每年都要校准一次?
How to view the transfer function of the module directly built by Simulink
Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
What are the common English questions in the postgraduate interview?
Electric fast burst (EFT) design - EMC series hardware design notes 4
【服务器使用记录】通过跳板机登录远程服务器并进行文件传输
PT physical aware based on multi voltage
Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
How to calibrate dsx2-8000? Calibration process?
Random life-1
听说你也在实习|当我采访了几个大三实习生之后。
Fluke fluke aircheck WiFi tester cannot configure file--- Ultimate solution experience
机器学习笔记 5 —— Logistic Regression
Detailed explanation of creepage distance and electrical clearance
Overall understanding of PLC
vi和vim命令