当前位置:网站首页>Pytorch for former Torch users - Tensors
Pytorch for former Torch users - Tensors
2022-06-30 09:14:00 【The man of Jike will never admit defeat】
Pytorch for former Torch users - Tensors
Tensors
Pytorch Medium Tensor and Torch Medium Tensor Have almost the same usage
Create a (5*7) Uninitialized... Of size tensor
import torch
a = torch.empty(5, 7, dtype=torch.floar)
Initializes a double precision floating-point number randomly obtained from the standard normal distribution tensor
b = torch.randn(5, 7, dtype=torch.double)
print(b.size())
# torch.Size([5, 7])
Note: torch.Size It's actually a tuple (tuple)
Inplace / Out-of-place
tensor All local operations on the are suffixed _
for example ,add It's an ex situ version , add_ It is an in place version
Some operations , for example narrow There is no in place version , And like fill_ There is no easy version for the operation of
Zero Indexing
Pytorch in tensor The index is from 0 At the beginning a = b[0, 3] # from b Select the first 1 That's ok , The first 4 Column
Tensors You can also use Python Slice index for b = a[:, 3:5] # Select all lines , The first 4 And the 5 Column
No Camel Casing Humpless spelling
All function names are no longer spelled hump . for example indexAdd Now rewrite to index_add_
x = torch.ones(5, 5);
# tensor([[ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.]])
z = torch.empty(5, 2)
z[:, 0] = 10
z[:, 1] = 100
# tensor([[ 10., 100.],
# [ 10., 100.],
# [ 10., 100.],
# [ 10., 100.],
# [ 10., 100.]])
x.index_add_(1, torch.tensor([4, 0]), z)
# stay x The first dimensional direction of ( horizontal direction ), x The fifth column of ( The index for 4) Add z The first column ,x The first column ( The index for 0) Add z The second column of
# tensor([[ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.]])
Numpy Bridge
Numpy array and torch Tensor The transition between them is very simple , Memory addresses are shared between the two , That is, changing one will change the other .
hold torch Tensor convert to numpy Array
a = torch.ones(5)
# tensor([ 1., 1., 1., 1., 1.])
b = a.numpy()
# [1. 1. 1. 1. 1.]
a.add_(1)
# tensor([ 2., 2., 2., 2., 2.])
# [2. 2. 2. 2. 2.]
hold numpy Array convert to torch Tensor
a = np.ones(5)
b = torch.from_numpy(a)
# [1. 1. 1. 1. 1.]
# tensor([ 1., 1., 1., 1., 1.], dtype=torch.float64)
CUDA Tensors
stay Pytorch in CUDA Tensors Simple to use , from CPU Change one CUDA Tensors To GPU Will save its original type
# Let us run this cell only cuda is available
if torch.cuda.is_available():
# create a LongTensor and transfer it
# to GPU as torch.cuda.LongTensor
a = torch.full((10, ), 2, device=torch.device("cuda"))
print(a)
b = a.to(torch.device("cpu"))
# transfer it to CPU, back to
# being a torch.LongTensor
边栏推荐
- What are the SQL add / delete / modify queries?
- Use Huawei performance management service to configure the sampling rate on demand
- Summary of common pytoch APIs
- What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
- 将线程绑定在某个具体的CPU逻辑内核上运行
- Common query and aggregation of ES
- Talking about the difference between kotlin collaboration and thread
- Application of hongruan face recognition
- 14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
- [untitled]
猜你喜欢
[wechat applet] realize applet pull-down refresh and pull-up loading
Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
Based on svelte3 X desktop UI component library svelte UI
Flutter 0001, environment configuration
Rew acoustic test (VI): signal and measurement
Detectron2 source code reading 4-- registrar construction model
Introduction to the runner of mmcv
Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"
Small program learning path 1 - getting to know small programs
随机推荐
Use V-IF with V-for
Detailed explanation of pipline of mmdetection
Wechat development tool (applet)
ES6 learning path (III) deconstruction assignment
Mmdet line by line code interpretation of positive and negative sample sampler
Set, map and modularity
Comparison of two ways for C to access SQL Server database (SqlDataReader vs SqlDataAdapter)
About MySQL Boolean and tinyint (1)
mysql基础入门 day4 动力节点[老杜]课堂笔记
ES6 learning road 5 symbol
Harmonyos actual combat - ten thousand words long article understanding service card development process
Tutorial for beginners of small programs day01
Pytorch BERT
Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
[JPEG] how to compile JPEG turbo library files on different platforms
[wechat applet] realize applet pull-down refresh and pull-up loading
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
Agp7.0|kts makes a reinforced plug-in
List set export excel table
使用华为性能管理服务,按需配置采样率