python torch.div() function usage instructions

Reference link:  torch.div()

 1. Function:

torch.div(input, other, out=None)  -->Tensor

 2. Explanation:

        Divides each element of the input input with the scalar other and returns a new resulting tensor.
        outi = inputi / other
        if input is of type FloatTensor or DoubleTensor ,other should be a real number, otherwise it should be integer.

       Divides each element of input by the scalar other and returns a new resulting tensor.

        If the input type is FloatTensor or DoubleTensor, other should be a real number, otherwise it should be an integer

参数:
        input(Tensor)-- the input tensor
        other(Number)–the number to be divided to each element of input
        out(Tensor, optional) – the output tensor

 3. Use cases

Case number one:

>>> import torch
a = >>> a = torch.randn(5)
>>> print(a)
tensor([ 0.5585, -0.4110,  0.4115, -0.8100, -1.6765])
>>> print(torch.div(a,0.5))
tensor([ 1.1169, -0.8221,  0.8230, -1.6200, -3.3530])
# 0.5585/0.5=1.1169   -0.4110/0.5=-0.8221  以此类推 

Case 2: 

>>> b = torch.randn(4,4)
>>> print(b)
tensor([[-0.4071, -0.0925, -0.3788, -0.0377],
        [-0.3488, -0.3844,  0.4662,  0.8078],
        [-0.6133,  1.1852, -0.9490,  0.1832],
        [-0.3004,  1.9121,  0.4630, -0.6038]])
>>> c = torch.randn(4)
>>> print(c)
tensor([-0.7244,  0.4043,  0.0782, -0.9177])
>>> print(torch.div(b,c))
tensor([[  0.5620,  -0.2288,  -4.8435,   0.0411],
        [  0.4815,  -0.9507,   5.9605,  -0.8802],
        [  0.8466,   2.9316, -12.1326,  -0.1997],
        [  0.4146,   4.7295,   5.9190,   0.6579]])
# -0.4071/-0.7244=0.5620. 竖着除

Guess you like

Origin blog.csdn.net/m0_62278731/article/details/130344564