Pytorch creates tensor operations

Pytorch creates tensor operations

Create an identity matrix

torch.eye(n, m=None, out=None)
#返回一个2维张量,对角线位置全1,其它位置全0的2维张量

parameter:

  • n(int) - number of rows
  • m(int, optional) - Number of columns, if None, defaults to n
  • out(Tensor,optional)- Output tensor

Convert a numpy.ndarray to a pytorch Tensor. The returned tensor shares the same memory space as the numpy ndarray. Modifying one will cause beam one to be modified as well. The returned tensor cannot be resized.

Example:
Insert image description here
Generate a 1D linear tensor
torch.linspace returns a 1D tensor containing evenly spaced start and end step points, the length of the output 1-dimensional tensor is steps.
torch.logspace returns a 1-dimensional tensor contained between 10start and 10end Step points evenly spaced on a logarithmic scale, and the length of the output 1-dimensional tensor is steps.

torch.linspace(start, end, steps=100, out=None) -> Tensor
torch.logspace(start, end, steos=100, out=None) -> Tensor

parameter:

  • start(float) - the starting point of the sequence
  • end(float) - the end point of the sequence
  • setps(int) - Number of samples generated between start and end
  • out(Tensor, optional) - the resulting tensor
    Example:
    Insert image description here
    Insert image description here
    Insert image description here
    generates a tensor of all ones
torch.ones(*size, out=None) -> Tensor

parameter:

  • size(int…) – sequence of integers that defines the output shape
  • out(Tensor, optional) - Result Tensor
    Example:
    Insert image description here
    Draw a random group from a uniform distribution in the interval [0, 1) number
torch.rand(*sizes, out=None) ->Tensor

parameter:

  • size(int…) - sequence of integers that defines the shape of the output
  • out(Tensor, optional) - Result Tensor
    Example:
    Insert image description here
    Insert image description here
    generates a standard normal distribution (mean is 0, variance is 1) a tensor
torch.randn(*sizes, out=None) ->Tensor

parameter:

  • size(int…) - sequence of integers that defines the shape of the output
  • out(Tensor, optional) - the result tensorInsert image description here
    generates a random integer permutation from 0 - (n-1)
torch.randperm(n, out=None) -> LongTensor

parameter:

  • n(int) - Upper bound (exclusive)
    Example:
    Insert image description here
    Generate a 1D tensor with specified stride:
    The length is floor((end-start)/step), including from start to end, with step as the step length
torch.arange(start, end, step=1, out=None)  -> Tensor

parameter:

  • start(float) - the starting point of the sequence
  • end(float) - the end point of the sequence
  • step(float) - the size of the interval between adjacent points
  • out(Tensor, optional) - the result tensor
    Example:
    Insert image description here
torch.range(start, end, step=1, out=None) -> Tensor

returns a 1-dimensional tensor with floor((end - start)/step) + 1 elements. A set of values ​​contained in the semi-open interval [start, end), starting from start, with step as the step size
Parameters:

  • start(float) - the starting point of the sequence
  • end(float) - the end point of the sequence
  • step(float) - the size of the interval between adjacent points
  • out(Tensor, optional) - the result tensor
    Example:
    Insert image description here
    Insert image description here
    generates a tensor matrix of all 0s
torch.zeros(*sizes, out=None) -> Tensor

parameter:

  • sizes(int…) - sequence of integers that defines the output shape
  • out(Tensor, optional) - the result tensor
    Example:
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43915090/article/details/134759277