PyTorch basics (1) ----- Tensor (Tensor)

Preface

Many people will compare PyTorch with Google's Tensorflow. This is definitely not a problem, because they are the two most popular deep learning frameworks. But when it comes to PyTorch, you should actually talk about Torch first.

What is Torch?
Torch is a Tensor operation library similar to Numpy. The difference from Numpy is that Torch supports GPU very well. Lua is the upper packaging of Torch. Although Lua is very fast, Lua is too niche, so PyTorch turned out.

PyTorch is a Torch-based Python open source machine learning library for applications such as natural language processing. It is mainly developed by Facebook's artificial intelligence research group. Uber's "Pyro" also uses this library.

PyTorch is a Python package that provides two advanced functions:

  • Tensor calculations with powerful GPU acceleration (such as NumPy)
  • Deep neural network including automatic derivation system

Finally, there are already a lot of comparisons between Tensorflow and PyTorch on the Internet. I won't explain it in detail here. If you are interested, you can read this article https://zhuanlan.zhihu.com/p/28636490 .

1. What is a tensor (Tensor)

PyTorch is a library for processing tensors. A tensor is the basic operation unit in PyTorch. A tensor is a number, vector, matrix or any n-dimensional array . The tensor is the same as Numpy's ndarray, and both represent a multi-dimensional matrix. The biggest difference with ndarray is **: PyTorch's Tensor can run on the GPU, while numpy's ndarray can only run on the CPU. Running on the GPU greatly speeds up the calculation. **

  • Guide package
# 首先要导入相关的包,以保证程序可以正常运行
import numpy as np
import torch
# 我们可以通过打印PyTorch的版本来确定是否导入成功
torch.__version__
  • Create a simple Tensor
# 生成一个2行3列的矩阵
x = torch.rand(2,3)
x
  • Check the size of Tensor
# 可以使用与numpy相同的shape属性查看
print(x.shape)
# 也可以用使用size()函数
print(x.size())
  • Generate a multidimensional tensor
y = torch.rand(2,3,4,5)
print(y.shape) # 打印y张量的大小
y

In the sense of isomorphism, the 0th order tensor (r = 0) is a scalar (Scalar), the first order tensor (r = 1) is a vector (Vector), and the second order tensor (r = 2) is It is called a matrix, and the third order and above are collectively called a multi-dimensional tensor.

  • Among them, the specialism is scalar. Let's first generate a scalar:
scalar = torch.tensor(3.1415926)
scalar
# 对于标量, 我们可以直接使用.item()从中取出其对应的Python对象
scalar.item()
# 特别的,如果张量中只有一个元素,也可以用.item()取出对应的Python对象
tensor1 = torch.tensor([3.1415926])
tensor1.item()

2. How to create a tensor (Tensor)

  • Initialize the tensor randomly using [0,1] uniform distribution
rnd = torch.rand(5,3)
rnd
  • Use 1 padding
one = torch.ones(2,2)
one
  • Use 0 padding
zero = torch.zeros(2,2)
zero
  • Initialize an identity matrix, that is, the diagonal is 1, and the others are 0
eye = torch.eye(2,2)
eye

Three, commonly used methods

The tensor operation api in PyTorch is very similar to Numpy. If you are familiar with the related operations in Numpy, the two are basically the same

  • First, we create a new tensor Tensor
rnd = torch.randn(3,3)
rnd

Then we proceed to the following operations:

  • Take the maximum value along the line
max_value,max_idx = torch.max(x,dim = 1)
max_value,max_idx
  • Take the maximum value along the column
max_value,max_idx = torch.max(x,dim = 0)
max_value,max_idx
  • Find the sum of each row
sum_x = torch.sum(x,dim = 1)
sum_x
  • Find the sum of each column
sum_y = torch.sum(x,dim = 0)
sum_y
  • Find the sum of two tensors
# 首先我们需要另建一个张量y
y = torch.randn(3,3)
z = x+y
z
  • Any operation ending with "-" will replace the original variable with the result, for example: "x.copy_(y)", will change x
x = torch.randn(3,3)
print('修改前', x)
y = torch.randn(3,3)
x.add_(y)
print('修改后', x)

Fourth, the conversion between Tensor and Numpy objects

Tensor and numpy objects share memory, so the conversion between them is fast and consumes almost no resources. But this also means that if one of them changes, the other will also change.

  • Tensor to Numpy object
a = torch.randn((3,2))
# 将tensor转为numpy
numpy_a = a.numpy()
print(numpy_a)
  • Convert Numpy object to Tensor
torch_a = torch.from_numpy(numpy_a)
print(torch_a)

V. References

https://github.com/zergtant/pytorch-handbook/blob/master/chapter2/2.1.1.pytorch-basics-tensor.ipynb

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/113781001