参考链接: torch.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的每个元素与标量other相除,并返回一个新的结果张量。
如果输入类型为FloatTensor或DoubleTensor,则other应为实数,否则应为整数
参数:
input(Tensor)-- the input tensor
other(Number)–the number to be divided to each element of input
out(Tensor, optional) – the output tensor
三.使用案例
案例一:
>>> 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 以此类推
案例二:
>>> 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. 竖着除