The Method of Creating and Using Pytorch Tensor

  • 2021-11-24 01:52:51
  • OfStack

Creation of catalog tensor and its basic types 1. Tensor (Tensor) function creation method
2. Types of tensors
3. Transformation of tensor types
Dimension and deformation of tensor 1. Create a high-dimensional tensor
2. Tensor deformation
2.1 flatten flattening: Transforming an arbitrary dimensional tensor into a 1-dimensional tensor 2.2 reshape method: Creation method of arbitrary deformation special tensor 1. Creation method of tensor with special value
2. Creates an array of specified shapes
Tensors and other types of transformation methods Deep copy of tensor

Creation of tensor and its basic types

1. The method of creating tensor (Tensor) function

The most basic method of creating tensor and the format of creating Array in Numpy are all the formats of creating functions (sequences): tensor creating functions: torch. tensor ()


import torch 

#  Create a tensor from a list 
t = torch.tensor([1,2])

#  Creating tensors from tuples 
t = torch.tensor((1,2))

import numpy as np

a = np.array((1,2))
#  Create a tensor from an array 
t1 = torch.tensor(a)
"""
 The output is  tensor([1,2],dtype=torch.int32)
Point:  Return the results by the above , We find that tensors also have dtype Type 
"""

2. Types of tensors

Tensors, like arrays, have the dtype method, which returns the tensor type. We found that integer arrays default to int32 (integer) type, while tensors default to int64 (long integer) type. In contrast, when creating floating-point arrays, the tensor defaults to float32 (single-precision floating-point) and Array defaults to float64 (double-precision floating-point). In addition to numerical tensors, the commonly used constant types are Boolean tensors, that is, tensors whose elements are Boolean.

3. Transformation of tensor types

Implicit Transformation of Tensor Types
Like Array in NumPy, when the tensor elements belong to different types, the system will automatically perform implicit conversion.


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

Transformation method of tensor type

Of course, we can also use. float (),. int () and other methods to transform tensor types.


t = torch.tensor([1,2])

#  Convert to default floating-point type (32 Bit )
t.float()

#  Convert to double-precision floating-point type 
t.double()

#  Convert to 16 Bit integer 
t.short()

Dimension and deformation of tensor

Tensor, as a structured representation of a group of numbers, also has the concept of dimension. Simply understood, vector is a 1-dimensional array, while matrix is a 2-dimensional array, and so on. In tensor, we can also define an array with higher dimensions. Of course, the high-dimensional array of tensors is similar in concept to the high-dimensional Array in Numpy.

1. Create a high-dimensional tensor

Creating 1-dimensional arrays with simple sequences
Sequences containing "simple" elements can create 1-dimensional arrays.


t1 = torch([1,2])
t1 

#  Use ndim Attribute to view the dimension of the tensor 
t1.ndim

#  Use shape View Shapes 
t1.shape

#  And size Same function 
t1.size()

Note: Unlike Numpy, the size method in PyTorch returns a result and the shape property returns a result 1.

In addition, note that there are two commonly used functions/methods for viewing the shape of tensors.


#  Returns how many (N-1) Dimensional element 
len(t1)

#  Returns how many numbers are there in total 
t1.numel()

Create a 2-D array with "sequence" of "sequence"
By analogy, we can also form a new sequence with the same shape, and then transform it into a two-dimensional tensor


#  Use list Adj. list Create 2 Dimensional array 
t2 = torch([[1,2],[3,4]])

Zero-dimensional tensor
In PyTorch, there is also a special kind of tensor, which is called zero-dimensional tensor. This type contains only one element, but it is not a single number.


t0 = torch.tensor([1])  #  This is still 1 Dimension tensor 
t0 = torch.tensor(1)    #  This is a zero-dimensional tensor 

Understanding zero-dimensional tensor:
At present, we can regard the zero-dimensional tensor as a single number with tensor attributes. (For example, a tensor can exist on GPU, but the native numerical object of Python can't, but a zero-dimensional tensor can, although it is zero-dimensional. ) In terms of academic name, the single number in Python is scalars (scalar), while the zero-dimensional tensor is tensor.

High dimensional tensor
Generally speaking, tensors with three dimensions and above are called high-dimensional tensors. Of course, among the high-dimensional tensors, the most common one is the three-dimensional tensor, which can be understood as a collection of two-dimensional arrays or matrices.


a1 = np.array([[1,2,2],[3,4,4]])
a2 = np.array([[5,6,6],[7,8,8]])
t3 = torch.tensor([a1,a2])
t3.shape   #  The result is torch.Size([2,2,3])    Contains two two lines 3 Matrix of columns 

Of course, for the creation method of N dimension tensor, we can first create an array of M dimensions of N-1, and then put it together into an N dimension tensor. As for the tensor of higher dimensions, we will explain it later when we meet it. In the process of tensor learning, three-dimensional tensor is enough.

2. Tensor deformation

Tensor is a structured set of numbers, and its structure is flexibly adjusted according to the demand.

2.1 flatten flattening: Transforming an arbitrary dimensional tensor into a 1-dimensional tensor

t2 = torch.tensor([[1,2]
                  ,[3,4]])
t2.flatten()   #  Flatten the tensors in rows 

2.2 reshape Method: Arbitrary Deformation

t1 = tensor([1.2])
#  Convert to two lines 1 Tensor of column 
t1.reshape(2,1)
"""
 The result is : tensor([[1],[2]])
 Attention : reshape Dimension changes in the process : reshape Parameters entered by this method for the converted dimension " Number " Decide 
"""

Creation method of special tensor

In many numerical scientific calculations, tensors with special values are created to simulate matrices with special values, such as all-zero matrices, diagonal matrices, etc. Therefore, there are many functions for creating special tensors in PyTorch.

1. Creation method of tensor with special value

All-zero tensor


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

0

Note: Since zeros already determines the values of tensor elements, the parameters passed in by this function actually determine the shape of the tensor

All 1 tensor


torch.ones([2,3])

Unity matrix


#  Return 5 Row 5 Unity matrix of columns , All diagonal elements are 1
torch.eyes(5)

Diagonal matrix
Slightly special, in PyTorch, it is necessary to create a focusing matrix by using 1-dimensional tensor fetching.


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

3

rand: Tensor obeying 0-1 uniform distribution


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

4

randn: Tensor obeying standard normal distribution


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

5

normal: A tensor that obeys a specified normal distribution


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

6

randint: Integer random sampling results


torch.randint(1,10,[2,4]) #  In 1-10 Randomly select integers between , Form two rows 4 Matrix of columns 

arrange/linsapce: Generating Sequences


torch.arrange(5)     #  And range Same 
"""
 The result is :
     tensor([0,1,2,3,4])
"""
torch.arraneg(1,5,0.5)    #  From 1 To 5( Left closed and right open ), Every interval 0.5 Value 1 Times 
torch.linspace(1,5,3)     #  From 1 To 5( Include both left and right ), Isometric fetching 3 Number 

empty: Generates a specified shape matrix for bit initialization


#  In order to ensure the accuracy of data, it is inclined to unify 1 Converted into data with higher accuracy 
#  Implicit Transformation of Floating-point Type and Integer Type 
torch.tensor([1.1,2])

#  Implicit transformation of Boolean type and numerical type 
torch.tensor([True,2.0])

9

full: Fill the specified value according to the specified shape


torch.full([2,4],2)

2. Creates an array of specified shapes

Of course, we can also populate numerically according to the shape of the specified object, just by adding _ like after the above function.


t1 = torch.tensor([1,2])
t2 = torch.tensor([[1,2],[3,4]])
torch.full_like(t1,2)    #  According to t1 Shape , Filling value 2
torch.randint_like(t2,1,10)
torch.zeros_like(t1)

Ponint: (1) For more _ like functions, see the help documentation

(2) One point that needs attention is that _ like type conversion needs to pay attention to the problem caused by data type 1 before and after conversion;


torch.rand_like(t1)     # t1 Is an integer, and will become a floating-point number after conversion , The code will report an error at this time  

Tensors and other types of transformation methods

Tensor, array and list are three similar types of objects. In the actual operation process, the mutual transformation of the three objects is often involved. Before that, in the process of creating tensors, we saw that torch. tensor function can directly transform arrays or lists into tensors, and we can also transform tensors into arrays or lists. In addition, the concept of zero-dimensional tensor was introduced in the previous article, and the transformation method between zero-dimensional tensor and numerical object will be given one step further here.

numpy Method: Tensor to Array


t1.numpy()
#  Of course, it can also be passed np.array Function is directly converted to the array
np.array(t1)

tolist method: Tensor to list


t1.tolist()

list function: Tensor to list


list(t1)

It should be noted that the converted list at this time is a list composed of zero-dimensional tensors, not a list converted from the values of tensors.

. item () method: Convert to numeric value

In many cases, we need to convert the final calculated result tensor into a separate value for output, which needs to be performed by using the. item method.


n = torch.tensor(1)
n.item()

Deep copy of tensor

Other object types in Python are like 1, and the equal sign assignment operation is actually a shallow copy. If you need to make a deep copy, you need to use clone method.


s = torch.tensor([1,2])
t = s.clone()


Related articles: