当前位置:网站首页>Pytorch: tensor operation (I) contiguous
Pytorch: tensor operation (I) contiguous
2022-07-06 12:13:00 【harry_ tea】
Contents of this article
tensor Storage in memory
Information area and storage area
tensor The storage in memory contains Information area and Storage area
- Information area (Tensor) contain tensor The shape of the size, step stride, data type type etc.
- Storage area (Storage) Contains stored data
High dimensional arrays are stored in memory according to row priority , What is row priority ? Suppose we have a (3, 4) Of tensor, It is actually stored as a one-dimensional array , But in tensor The information area of recorded his size and stride As a result, the array actually displayed is two-dimensional ,size by (3, 4)

Two dimensional array

One dimensional form in memory
Now let's look at an example , Examples show that tensor The elements in are contiguous in memory , And it also proves that it is indeed row priority storage
tensor = torch.tensor([[[1 ,2, 3, 4], [5, 6, 7, 8], [9, 10,11,12]],
[[13,14,15,16],[17,18,19,20],[21,22,23,24]]])
print(tensor.is_contiguous())
for i in range(2):
for j in range(3):
for k in range(4):
print(tensor[i][j][k].data_ptr(), end=' ')
''' True 140430616343104 140430616343112 140430616343120 140430616343128 140430616343136 140430616343144 140430616343152 140430616343160 140430616343168 140430616343176 140430616343184 140430616343192 140430616343200 140430616343208 140430616343216 140430616343224 140430616343232 140430616343240 140430616343248 140430616343256 140430616343264 140430616343272 140430616343280 140430616343288 '''
shape && stride
Continuing with the above example, let's take a look at shape and stride attribute , about (2, 3, 4) Dimensional tensor His shape by (2, 3 ,4),stride by (12, 4, 1)
shape
shape It's easy to understand , Namely tensor Dimensions , The above example is (2, 3, 4) Of tensor, Dimension is (2, 3, 4)
stride
stride Represents the step size of multidimensional index , Each step represents an offset in memory +1, about (2, 3, 4) Dimensional tensor:stride+1 Represents the (dim2)+1,stride+4 For the rest dim unchanged ,(dim1)+1,stride+12 For the rest dim unchanged ,(dim0)+1, As shown in the figure below

Icon stride
stride computing method
s t r i d e i = s t r i d e i + 1 ∗ s i z e i + 1 i ∈ [ 0 , n − 2 ] stride_{i} = stride_{i+1} * size_{i+1}~~~~i\in[0, n-2] stridei=stridei+1∗sizei+1 i∈[0,n−2]
about shape(2, 3, 4) Of tensor, The calculation is as follows (stride3=1)
s t r i d e 2 = s t r i d e 3 ∗ s h a p e 3 = 1 ∗ 4 = 4 s t r i d e 1 = s t r i d e 2 ∗ s h a p e 2 = 4 ∗ 3 = 12 stride_{2} = stride_{3} * shape_{3}=1*4=4 \\ stride_{1} = stride_{2} * shape_{2}=4*3=12 stride2=stride3∗shape3=1∗4=4stride1=stride2∗shape2=4∗3=12
stride = [1] # Initialize the first element
# Iterate back and forward to generate stride
for i in range(len(tensor.size())-2, -1, -1):
stride.insert(0, stride[0] * tensor.shape[i+1])
print(stride) # [12, 4, 1]
print(tensor.stride()) # (12, 4, 1)
I understand tensor After storage in memory , Let's see contiguous
contiguous
contiguous
Returns a continuous memory tensor
Returns a contiguous in memory tensor containing the same data as self tensor. If self tensor is already in the specified memory format, this function returns the self tensor.
When to use contiguous Well ?
The simple understanding is tensor Use when the storage order in the memory address is inconsistent with the actual one-dimensional index order , As shown below , Right up there tensor One dimensional index , The result is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], Original tensor Application transpose To transpose , In the one-dimensional index , The result is [1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12], At this time, the index order changes , So we need to use contiguous
Be careful : No matter how it changes, the corresponding address of each element is unchanged , such as 11 The corresponding address is x11,transpose after 11 Still corresponding x11, So what has changed ? Remember tensor Is it divided into information area and storage area , The storage area does not change , What changes is the information area shape,stride Etc , Make an introduction when you have time ~

Code example
tensor = torch.tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(tensor)
print(tensor.is_contiguous())
tensor = tensor.transpose(1, 0)
print(tensor)
print(tensor.is_contiguous())
''' tensor([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) True tensor([[ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11], [ 4, 8, 12]]) False '''
Why use contiguous
Some people may have questions , Since the above index is different from before ( It's not continuous ), Why make him continuous ? because pytorch Some operations of require index and memory continuity , such as view
Code example ( Next, let's take the example above )
tensor = tensor.contiguous()
print(tensor.is_contiguous())
tensor = tensor.view(3, 4)
print(tensor)
''' True tensor([[ 1, 5, 9, 2], [ 6, 10, 3, 7], [11, 4, 8, 12]]) '''
If not contiguous The following errors will be reported
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
Why? contiguous It works ?
contiguous In a simple and crude way , Since your previous index and memory are not continuous , Then I'll reopen a piece of continuous memory and index it
Code example , From the following code stride Changes can be seen ,transpose After that tensor It really changes the information in the information area
tensor = torch.tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(tensor.is_contiguous()) # True
for i in range(3):
for j in range(4):
print(tensor[i][j], tensor[i][j].data_ptr(), end=' ')
print()
print(tensor.stride()) # (4, 1)
''' True tensor(1) 140430616321664 tensor(2) 140430616321672 tensor(3) 140430616321680 tensor(4) 140430616321688 tensor(5) 140430616321696 tensor(6) 140430616321704 tensor(7) 140430616321712 tensor(8) 140430616321720 tensor(9) 140430616321728 tensor(10) 140430616321736 tensor(11) 140430616321744 tensor(12) 140430616321752 (4, 1) '''
tensor = tensor.transpose(1, 0)
print(tensor.is_contiguous()) # False
for i in range(4):
for j in range(3):
print(tensor[i][j], tensor[i][j].data_ptr(), end=' ')
print()
print(tensor.stride()) # (1, 4) changed
''' False tensor(1) 140430616321664 tensor(5) 140430616321696 tensor(9) 140430616321728 tensor(2) 140430616321672 tensor(6) 140430616321704 tensor(10) 140430616321736 tensor(3) 140430616321680 tensor(7) 140430616321712 tensor(11) 140430616321744 tensor(4) 140430616321688 tensor(8) 140430616321720 tensor(12) 140430616321752 (1, 4) '''
tensor = tensor.contiguous()
print(tensor.is_contiguous()) # True
for i in range(4):
for j in range(3):
print(tensor[i][j], tensor[i][j].data_ptr(), end=' ')
print()
print(tensor.stride()) # (3, 1)
''' True tensor(1) 140431681244608 tensor(5) 140431681244616 tensor(9) 140431681244624 tensor(2) 140431681244632 tensor(6) 140431681244640 tensor(10) 140431681244648 tensor(3) 140431681244656 tensor(7) 140431681244664 tensor(11) 140431681244672 tensor(4) 140431681244680 tensor(8) 140431681244688 tensor(12) 140431681244696 (3, 1) '''
边栏推荐
- Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
- IOT system framework learning
- arduino JSON数据信息解析
- [Red Treasure Book Notes simplified version] Chapter 12 BOM
- I2C bus timing explanation
- gcc 编译选项
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- MySQL takes up too much memory solution
- inline详细讲解【C语言】
- Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
猜你喜欢
锂电池基础知识
JS variable types and common type conversions
[Red Treasure Book Notes simplified version] Chapter 12 BOM
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Analysis of charging architecture of glory magic 3pro
JS object and event learning notes
RT-Thread API参考手册
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Pat 1097 duplication on a linked list (25 points)
随机推荐
Understanding of AMBA, AHB, APB and Axi
vim命令行笔记
Programmers can make mistakes. Basic pointers and arrays of C language
Time slice polling scheduling of RT thread threads
GNN的第一个简单案例:Cora分类
Basic use of pytest
Kconfig Kbuild
共用体(union)详解【C语言】
AMBA、AHB、APB、AXI的理解
Rough analysis of map file
ES6 grammar summary -- Part I (basic)
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Apprentissage automatique - - régression linéaire (sklearn)
Générateur d'identification distribué basé sur redis
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
map文件粗略分析
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
arduino JSON数据信息解析
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Pytoch implements simple linear regression demo