PyTorch Quick Start Series-01

Introduction to PyTorch

The most commonly used frameworks in deep learning are Tensorflow and PyTorch. When the editor himself taught himself deep learning, Tensorflow2.0 just came out. Since there are not many PyTorch materials on the market, and Tensorflow is easy to get started and faster, so I chose Tensorflow. But as the scientific research deepened, I found that most of the papers and projects used the PyTorch framework, and I was stunned! ! ! It should be that I am too good to use Tensorflow flexibly, and everyone around me has started to use PyTorch, so I finally turned to learn the PyTorch framework. (With the widespread use of PyTorch in various industries in recent years, and its high flexibility, it is recommended to start deep learning from PyTorch.)
PyTorch advantages:

  1. The code is concise and flexible: because it is based on the dynamic graph mechanism, it is more flexible to use than Tensorflow.
  2. DeBug is convenient: debugging PyTorch code is as simple and clear as C/C++/Python.
  3. Development documentation: https://pytorch.org/docs/stable/index.html
  4. Most of the algorithms in the academic and industrial circles are developed by PyTorch.
  5. Getting started is simple and fast. (Convenient for artificial intelligence beginners to learn quickly)

Installation method: A complete installation article will be published later, you can try to install it yourself. (The general collocation environment is to install Anaconda, PyCharm, PyTorch).
PyTorch officially released a tutorial , you can take a look if you are interested.

Tensor: Tensor

official document

torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor

官方说明:
Parameters:
data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.

Keyword Arguments:
dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from data.

device (torch.device, optional) – the device of the constructed tensor. If None and data is a tensor then the device of data is used. If None and data is not a tensor then the result tensor is constructed on the CPU.

requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

explain:

  • data: data, which can be a list, tuple, array or other types.
  • dtype: data type, which is consistent with data by default.
  • device: data location: CPU/GPU.
  • requires_grad: Whether to require a derivative gradient.
  • pin_memory: Whether the returned Tensor is allocated in pinned memory.

Doesn't it look troublesome! Next, I will not introduce it according to the official document. It will be simpler. If you want to know more details, just read the official website document!

Tensors Tensor
  • torch.is_tensor(obj) returns True
    if obj is a pytorch tensor

  • torch.is_storage(obj) returns True
    if obj is a pytorch storage object

  • torch.__set_default_tensor_type(t)

  • torch.numel(input) -> int
    returns the number of elements in the input tensor
    Parameters: input(Tensor)—input tensor
    Case:

a = torch.randn(1,2,3,4,5) 
torch.numel(a)
#a = 5

b =  torch.ones((4,4))
torch.numel(b)
#b = 16
  • torch.set_printoptions(precision=None,threshold=None,edgeitems=None,linewidth=None,profile=None)
    sets printing options, which are used less.
Creation Ops
  • torch.eye(n,m=None,out=None)
    returns a 2-dimensional tensor, the diagonal position is all 1, and the other positions are all 0.
    Parameters: n(int) — the number of rows, m(int, optional ) — the number of columns. If None, defaults to n. out(Tensor.optional) — Output tensor The above parameters are only common parameters.
    Return value: a two-dimensional tensor whose diagonal positions are all 1 and other positions are all 0.
    Return value type: Tensor
    Case:
torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
  • torch.from_numpy(ndarray) -> Tensor
    Numpy bridge, convert numpy.ndarray to pytorch's Tensor. The returned tensor tensor and numpy ndarray share the same memory space. Modifying one will cause the other to be modified as well. The returned tensor cannot change size.
    case:
a = np.array([1,2,3])
torch.from_numpy(a)
tensor([1, 2, 3], dtype=torch.int32)
  • torch.linspace(start,end,steps=None,out=None) -> Tensor
    returns a one-dimensional tensor containing steps points evenly spaced on the interval start and end. Output a 1D tensor of length steps.
    Parameters: start(float) — the starting point of the sequence end(float) — the final value of the sequence steps(int) — the number of samples generated between start and end out(Tensor, optional) — tensor Case
    :
torch.linspace(-10,10,steps=10)
tensor([-10.0000,  -7.7778,  -5.5556,  -3.3333,  -1.1111,   1.1111,   3.3333,
          5.5556,   7.7778,  10.0000])
  • torch.logspace(start,end,steps=None,out=None) -> Tensor
    returns a one-dimensional tensor, contained between the start and end of the interval with base 10, evenly spaced by steps points.
  • torch.ones(sizes,out=None) -> Tensor
    returns a tensor of all ones, whose shape is defined by sizes.
    case:
torch.ones((2,3))
tensor([[1., 1., 1.],
        [1., 1., 1.]])
  • torch.rand(sizes,out=None) -> Tensor
    returns a tensor containing a set of random numbers drawn from a uniform distribution in the interval [0,1), whose shape is defined by sizes.
  • torch.randn(sizes,out=None) -> Tensor
    returns a tensor containing a set of random numbers drawn from a standard normal distribution (mean 0, variance 1, Gaussian white noise), whose shape is defined by sizes .
  • torch.randperm(n,out=None)
    Given parameter n, returns a random number permutation from 0 to n-1.
    Parameters: n (int) – upper bound (exclusive)
  • torch.arange(start,end,step=1,out=None) -> Tensor
    returns a one-dimensional tensor with length floor( (end-start) / step ). Contains a group from start to end with step as the step size. Sequence values ​​default to a step size of 1.
    case:
torch.arange(1,4)
tensor([1,2,3])
  • torch.range(start,end,step=1,out=None) -> Tensor
    returns a one-dimensional tensor with floor((end-start)/step+1) elements. Included in the half-open interval [start, end]. A set of values ​​in steps, starting from start.
    Rarely used, it is recommended to use torch.arange()!
  • torch.zeros(sizes,out=None) -> Tensor
    returns a Tensor of all scalar 0s with shape defined by sizes.
Indexing, Slicing, Joining, Transposition (Indexing, Slicing, Joining, Mutating Ops)
  • torch.cat(inputs,dimension=0) -> Tensor
    connects and stitches the input tensor sequence sep on a given dimension.
    torch.cat() can be seen as the inverse operation of torch.split() and torch.chunk().
    Parameters: input(sequence of Tensors) — can be any python sequence of the same Tensor type dimensions(int, optional) — concatenate tensor sequences along this dimension
    Case:
x = torch.randn((2,3))
x
tensor([[ 0.2392,  1.6352,  0.3733],
        [ 0.2606, -0.3474, -1.9230]])
torch.cat((x,x),0)
tensor([[ 0.2392,  1.6352,  0.3733],
        [ 0.2606, -0.3474, -1.9230],
        [ 0.2392,  1.6352,  0.3733],
        [ 0.2606, -0.3474, -1.9230]])
torch.cat((x,x),1)
tensor([[ 0.2392,  1.6352,  0.3733,  0.2392,  1.6352,  0.3733],
        [ 0.2606, -0.3474, -1.9230,  0.2606, -0.3474, -1.9230]])
  • torch.chunk(tensor,chunks,dim=0)
    Chunks the input tensor along the given dimension (axis).
    Parameters: tensor(Tensor) — the input tensor to be chunked, chunks(int) — the number of chunks, dim(int) — along which dimension to chunk
  • torch.split(tensor, split_size, dim=0)
    splits the input tensor into chunks of equal shape (if divisible). If the tensor shape size along the specified dimension is not divisible by split_size, the last split will be smaller than the others.
    Parameters: tensor(Tensor) — the tensor to be split, split_size(int) — the shape and size of a single block, dim(int) — splitting along the specified dimension.
  • torch.squeeze(input, dim=None, out=None)
    removes 1 from the input tensor shape and returns it. If the input is of shape (A 1 B 1 C 1 D), then the output shape is: (A B C D).
    When dim is given, the squeeze operation is only in the given dimension. For example, if the input shape is: (A
    1 B), squeeze(input,0) will keep the tensor unchanged, only with squeeze(input,1), the shape will become (A B).
    Note: The returned tensor shares memory with the input tensor, so changing the contents of one will change the other.
    Parameters: input(Tensor) — input tensor, dim(int, optional) — if given, input will only squeeze in the given dimension, out(Tensor, optional) — output tensor.
    case:
x = torch.zeros((3,1,3,1,3))
x.size()
#torch.Size([3, 1, 3, 1, 3])

torch.squeeze(x,0).size()
#torch.Size([3, 1, 3, 1, 3])

torch.squeeze(x,1).size()
#torch.Size([3, 3, 1, 3])
  • torch.t(input,out=None) -> Tensor
    input a matrix (2-dimensional tensor), and transpose 0, 1 dimension. Can be seen as shorthand for transpose(input,0,1).
    Parameters: input(Tensor)—input tensor, out(Tensor,optional)—result tensor
x = torch.randn((2,3))
tensor([[ 1.7073, -0.1557,  1.4400],
        [ 0.3452,  0.0751,  0.1627]])

torch.t(x)
tensor([[ 1.7073,  0.3452],
        [-0.1557,  0.0751],
        [ 1.4400,  0.1627]])

torch.t(x).size()
torch.Size([3, 2])
  • torch.transpose(input,dim0,dim1,out=None)->Tensor
    returns the transpose of the input matrix input. Swap dimensions dim0 and dim1. The output tensor shares memory with the input tensor. So changing one of them will cause the other to be modified as well.
    Parameters: input(Tensor)—input tensor, dim0(int)—first dimension of transposition, dim1(int)—second dimension of transposition
    Case:
x = torch.randn(2,3)
x
tensor([[ 1.6700,  2.3639, -1.5683],
        [ 0.0986,  1.5579,  0.9787]])

torch.transpose(x,0,1)
tensor([[ 1.6700,  0.0986],
        [ 2.3639,  1.5579],
        [-1.5683,  0.9787]])
  • torch.unsqueeze(input,dim,out=None)
    returns a new tensor with dimension 1 inserted at the specified position of the input
    Note: the returned tensor shares memory with the input tensor, so changing the content of one will change the other.
    If dim is negative, it will be converted dim+input.dim()+1
    Parameters: tensor(Tensor)—input tensor, dim(int)—index of inserted dimension, out(Tensor,optional)—result tensor
    case:
x = torch.Tensor([1,2,3,4])
torch.unsqueeze(x,0)
#tensor([[1., 2., 3., 4.]])
x.size()
#torch.Size([4])
torch.unsqueeze(x,0).size()
#torch.Size([1, 4])

Guess you like

Origin blog.csdn.net/weixin_50918736/article/details/130428393