Pytorch commonly used functions

1. torch.renorm(inputpdimmaxnormout=None) → Tensor

Returns a tensor where each sub-tensor of input along dimension dim is normalized such that the p-norm of the sub-tensor is lower than the value maxnorm。

Explanation: Returns a tensor, the respective sub-tensors comprises standardized, so that along the dimrange dimension of each sub-divided tensor number is less than p maxnorm.

>>> x = torch.Tensor([[1,2,3]])
>>> torch.renorm(x,2,0,1) tensor([[ 0.2673, 0.5345, 0.8018]])

2. torch. scatter_(dimindexsrc) → Tensor

The srcAll values in accordance with indexan index determined tensor is written in the present. Wherein the index is based on a given dimension, dim in accordance with gather()the rules described be determined.

Note, index value must be in the 0 to (self.size (dim) -1) between,

parameter:

  • input (Tensor)-源tensor
  • Dim  ( int ) - axial index
  • index  ( LongTensor index index scatterer -)
  • the src  ( the Tensor or a float ) - scattering source element

X = torch.rand >>> (2,. 5)
>>> X
 .4319 0.6500 0.4080 0.8760 .2355
 .2609 0.4711 0.8486 0.8573 0.1029
[torch.FloatTensor size of 2x5]
>>> torch.zeros (. 3,. 5) .scatter_ (0 , torch.LongTensor ([[0, 1 , 2, 0, 0], [2, 0, 0, 1, 2]]), x) # x will be written in accordance with the new format in Tensor
 0.4319 0.4711 0.8486 0.8760 0.2355
 0.6500 0.0000 0.8573 0.0000 0.0000
 0.2609 0.0000 0.4080 0.0000 .1029
[torch.FloatTensor size of 3x5]
>>> torch.zeros Z = (2,. 4) .scatter_ (. 1, torch.LongTensor ([[2], [. 3]]) , 1.23)
>>> Z
 0.0000 0.0000 1.2300 0.0000
 0.0000 0.0000 0.0000 1.2300
[torch.FloatTensor size of 2x4]

 

3.  torch.gather(input, dim, index, out=None) Tensor

Along a given axis dim, the index tensor of the input indexposition specified by the polymerization.

parameter:

  • input (Tensor) - source tensor
  • dim (int) - indexing shaft
  • index (LongTensor) - Aggregate elements subscript
  • out (Tensor, optional) - target tensor
>>> t = torch.Tensor([[1,2],[3,4]])
>>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
 1  1
 4  3
[torch.FloatTensor of size 2x2]

 or:

>>> s=torch.randn(3,6)
>>> s
tensor([[-0.4857, -0.0982, -0.6532, -1.0273, -0.9205, -0.7440],
        [-0.6890, -0.3474, -1.4337, -0.3511, -0.2443, -0.6398],
        [ 1.2902,  1.1210,  1.7374,  0.0902, -0.4524, -0.6898]])
>>> s.gather(1,torch.LongTensor([[1,2,1],[1,2,3],[1,2,3]]))
tensor([[-0.0982, -0.6532, -0.0982],
        [-0.3474, -1.4337, -0.3511],
        [ 1.1210,  1.7374,  0.0902]])

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160679.htm