python torch div()函数

一、描述

div()函数 表示除法

二、函数
torch.div(input, other, out=None)  -->Tensor
三、用法
解释:

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.

参数:
  • input(Tensor)-- the input tensor
  • other(Number)–the number to be divided to each element of input
  • out(Tensor, optional) – the output tensor
四、实例
实例1
>>> 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  以此类推 
实例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. 竖着除
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐