githubEdit

PyTorch Functions

Tensor Operation

Tensor.scatter_(dim, index, src, reduce=None) → Tensorarrow-up-right

  • dim (intarrow-up-right) – the axis along which to index

  • index (LongTensor) – the indices of elements to scatter, can be either empty or of the same dimensionality as src. When empty, the operation returns self unchanged.

  • src (Tensorarrow-up-right or floatarrow-up-right) – the source element(s) to scatter.

  • reduce (strarrow-up-right, optional) – reduction operation to apply, can be either 'add' or 'multiply'.

The operation replaces values in self by values in src in ways specified by index.

>>> src = torch.arange(1, 11).reshape((2, 5))
>>> src
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10]])
>>> index = torch.tensor([[0, 1, 2, 0]])
>>> torch.zeros(3, 5, dtype=src.dtype).scatter_(0, index, src)
tensor([[1, 0, 0, 4, 0],
        [0, 2, 0, 0, 0],
        [0, 0, 3, 0, 0]])
>>> index = torch.tensor([[0, 1, 2], [0, 1, 4]])
>>> torch.zeros(3, 5, dtype=src.dtype).scatter_(1, index, src)
tensor([[1, 2, 3, 0, 0],
        [6, 7, 0, 0, 8],
        [0, 0, 0, 0, 0]])
  • The scatter function dim is 0, so we know the index input is specifying the new index in dimension 0, and the rest of the dimensions follow the original dimension in src.

  • Look at src[0][1]=2, the corresponding index value is index[0][1]=1, so we know that src[0][1] will be placed into self[1][1].

  • Look at src[0][2]=3, the corresponding index value is index[0][2] = 2, so we know that src[0][2] will be placed into self[2][2]

  • Because index tensor shape is [1,4], so the second row of source tensors are not placed

torch.mean(input, dim, keepdim=False, *, dtype=None, out=None) → Tensorarrow-up-right

  1. Reducing over dimension 0. e.g.,: Output[0][0] is the mean of Input[0][0][0] and input[1][0][0]

  2. Reducing over dimension 1. e.g.,: Output[0][0] is the mean of Input[0][0][0], Input[0][1][0] and Input[0][2][0]

  3. Reducing over dimension 0 and 2. e.g.,: Output[0] is the mean of [0][0][0], [0][0][1], [0][0][2], [0][0][3], [1][0][0], [1][0][1], [1][0][2], [1][0][3]

torchvision.transforms.Normalize(mean, std, inplace=False)

  • mean (sequence) – Sequence of means for each channel.

  • std (sequence) – Sequence of standard deviations for each channel.

  • inplace (boolarrow-up-right,optional) – Bool to make this operation in-place.

  • For every image, the transformer will subtract 0 from every cell in channel 0, and subtract 1 from every cell in channel 1

Last updated