pytroch 函数学习

1.  tensor=torch.from_numpy(ndarray).

ndarray转化为tensor

2 .     ndarray =tensor.numpy()

将tensor转化为ndarray

3.    x = torch.zeros(*sizes, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)  

>>> torch.zeros(2, 3)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

>>> torch.zeros(5)
tensor([ 0.,  0.,  0.,  0.,  0.]

初始化一个全零数组。

4.  y=torch.zeros_like(x)

构造一个形状和类型和x一致的全零数组

5. torch.ones(*sizes, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

 torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor

和上面的例子一样,只是这次构造的是全一数组

6. torch.arange(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

torch.range(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

构造一个顺序数组

>>> torch.arange(5)
tensor([ 0,  1,  2,  3,  4])
>>> torch.arange(1, 4)
tensor([ 1,  2,  3])
>>> torch.arange(1, 2.5, 0.5)
tensor([ 1.0000,  1.5000,  2.0000])

arange()和range()的区别是range()结果多了一位,并且range()需要起始点。

>>>a=torch.range(0,5)
>>>print a
tensor([ 0.,  1.,  2.,  3.,  4.,  5.])

7. torch.masked_select(input, mask, out=None) → Tensor

可以根据掩模选择为一的数值重新组成一个数组

>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297,  0.3477],
        [-1.2035,  1.2252,  0.5002,  0.6248],
        [ 0.1307, -2.0608,  0.1244,  2.0139]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[ 0,  0,  0,  0],
        [ 0,  1,  1,  1],
        [ 0,  0,  0,  1]], dtype=torch.uint8)
>>> torch.masked_select(x, mask)
tensor([ 1.2252,  0.5002,  0.6248,  2.0139])

8. torch.reshpe(input,shape)

改变数组形状,但总体大小必须一致,不然会报错

>>> a = torch.arange(4.)
>>> torch.reshape(a, (2, 2))
tensor([[ 0.,  1.],
        [ 2.,  3.]])
>>> b = torch.tensor([[0, 1], [2, 3]])
>>> torch.reshape(b, (-1,))
tensor([ 0,  1,  2,  3])

9. torch.squeeze(input, dim=None, out=None) → Tensor

将多余的一维去除

>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])

10. torch.stack(seq, dim=0, out=None) → Tensor

Concatenates sequence of tensors along a new dimension.

All tensors need to be of the same size.

Parameters:
  • seq (sequence of Tensors) – sequence of tensors to concatenate
  • dim (int) – dimension to insert. Has to be between 0 and the number of dimensions of concatenated tensors (inclusive)
  • out (Tensor, optional) – the output tensor

按维度拼接数组

11.torch.take(input, indices) → Tensor

>>> src = torch.tensor([[4, 3, 5],
                        [6, 7, 8]])
>>> torch.take(src, torch.tensor([0, 2, 5]))
tensor([ 4,  5,  8])

效果类似于7,但是7是寻找掩模为1的,而这个是寻找index上数组的值。

12.torch.transpose(input,dim0,dim1)

将输入数组转置,dim0为第一维,dim1为第二维

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 1.0028, -0.9893,  0.5809],
        [-0.1669,  0.7299,  0.4942]])
>>> torch.transpose(x, 0, 1)
tensor([[ 1.0028, -0.1669],
        [-0.9893,  0.7299],
        [ 0.5809,  0.4942]])

13.torch.where(condition,x,y)

当满足条件时,赋值上x的值,当不满足条件时,赋值上y的值。x,y的维度必须一致。

>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)
>>> x
tensor([[-0.4620,  0.3139],
        [ 0.3898, -0.7197],
        [ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000,  0.3139],
        [ 0.3898,  1.0000],
        [ 0.0478,  1.0000]])

14.torch.normal(mean, std, out=None) → Tensor

Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.

The mean is a tensor with the mean of each output element’s normal distribution

The std is a tensor with the standard deviation of each output element’s normal distribution

The shapes of mean and std don’t need to match, but the total number of elements in each tensor need to be the same.

Note

When the shapes do not match, the shape of mean is used as the shape for the returned output tensor

Parameters:
  • mean (Tensor) – the tensor of per-element means
  • std (Tensor) – the tensor of per-element standard deviations
  • out (Tensor, optional) – the output tensor

Example:

>>> torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
tensor([  1.0425,   3.5672,   2.7969,   4.2925,   4.7229,   6.2134,
          8.0505,   8.1408,   9.0563,  10.0566])

15.torch.clamp(input, min, max, out=None) → Tensor

小于min的值取min,大于max的值取max。

Clamp all elements in input into the range [ min, max ] and return a resulting tensor:

If input is of type FloatTensor or DoubleTensor, args min and max must be real numbers, otherwise they should be integers.

Parameters:
  • input (Tensor) – the input tensor
  • min (Number) – lower-bound of the range to be clamped to
  • max (Number) – upper-bound of the range to be clamped to
  • out (Tensor, optional) – the output tensor

Example:

>>> a = torch.randn(4)
>>> a
tensor([-1.7120,  0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.1734, -0.0478, -0.0922])

16.torch.ge(input, other, out=None) → Tensor

Computes input≥other element-wise.

input大于等于other的取1 其他取零

>>> torch.ge(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[ 1,  1],
        [ 0,  1]], dtype=torch.uint8)

torch.gt(input, other, out=None) → Tensor

Computes input>other

element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare
  • other (Tensor or float) – the tensor or value to compare
  • out (Tensor, optional) – the output tensor that must be a ByteTensor
Returns:

A torch.ByteTensor containing a 1 at each location where comparison is true

Return type:

Tensor

Example:

>>> torch.gt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[ 0,  1],
        [ 0,  0]], dtype=torch.uint8)

17

 

 

 

 

你可能感兴趣的:(pytorch)