参考 torch(七)、Math operations(1) - 云+社区 - 腾讯云
目录
torch.abs(input, out=None) → Tensor
torch.acos(input, out=None) → Tensor
torch.add(input, alpha=1, other, out=None)
out (Tensor, optional) – the output tensor.
torch.addcdiv(input, value=1, tensor1, tensor2, out=None) → Tensor
torch.addcmul(input, value=1, tensor1, tensor2, out=None) → Tensor
torch.asin(input, out=None) → Tensor
torch.atan(input, out=None) → Tensor
torch.atan2(input, other, out=None) → Tensor
torch.bitwise_not(input, out=None) → Tensor
torch.ceil(input, out=None) → Tensor
torch.clamp(input, min, max, out=None) → Tensor
torch.clamp(input, *, min, out=None) → Tensor
torch.clamp(input, *, max, out=None) → Tensor
torch.cos(input, out=None) → Tensor
torch.cosh(input, out=None) → Tensor
torch.div(input, other, out=None) → Tensor
torch.digamma(input, out=None) → Tensor
torch.erf(input, out=None) → Tensor
torch.erfc(input, out=None) → Tensor
torch.erfinv(input, out=None) → Tensor
torch.exp(input, out=None) → Tensor
torch.expm1(input, out=None) → Tensor
torch.floor(input, out=None) → Tensor
torch.fmod(input, other, out=None) → Tensor
torch.frac(input, out=None) → Tensor
torch.lerp(input, end, weight, out=None)
torch.log(input, out=None) → Tensor
torch.log10(input, out=None) → Tensor
torch.log1p(input, out=None) → Tensor
torch.log2(input, out=None) → Tensor
torch.logical_not(input, out=None) → Tensor
torch.logical_xor(input, other, out=None) → Tensor
torch.mul(input, other, out=None)
torch.mvlgamma(input, p) → Tensor
torch.neg(input, out=None) → Tensor
torch.pow(self, exponent, out=None) → Tensor
torch.reciprocal(input, out=None) → Tensor
torch.remainder(input, other, out=None) → Tensor
torch.round(input, out=None) → Tensor
torch.rsqrt(input, out=None) → Tensor
torch.sigmoid(input, out=None) → Tensor
torch.sign(input, out=None) → Tensor
torch.sin(input, out=None) → Tensor
torch.sinh(input, out=None) → Tensor
torch.sqrt(input, out=None) → Tensor
torch.tan(input, out=None) → Tensor
torch.tanh(input, out=None) → Tensor
torch.trunc(input, out=None) → Tensor
torch.argmax(input, dim, keepdim=False) → LongTensor
torch.argmin(input, dim, keepdim=False, out=None) → LongTensor
torch.cumprod(input, dim, out=None, dtype=None) → Tensor
torch.cumsum(input, dim, out=None, dtype=None) → Tensor
torch.dist(input, other, p=2) → Tensor
torch.logsumexp(input, dim, keepdim=False, out=None)
torch.mean(input, dim, keepdim=False, out=None) → Tensor
torch.median(input, dim=-1, keepdim=False, values=None, indices=None) -> (Tensor, LongTensor)
torch.mode(input, dim=-1, keepdim=False, values=None, indices=None) -> (Tensor, LongTensor)
torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None)[source]
torch.prod(input, dim, keepdim=False, dtype=None) → Tensor
torch.std(input, dim, keepdim=False, unbiased=True, out=None) → Tensor
torch.std(input, dim, keepdim=False, unbiased=True) -> (Tensor, Tensor)
torch.sum(input, dim, keepdim=False, dtype=None) → Tensor
torch.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None)[source]
torch.unique_consecutive(input, return_inverse=False, return_counts=False, dim=None)[source]
torch.var(input, dim, keepdim=False, unbiased=True, out=None) → Tensor
torch.var_mean(input, dim, keepdim=False, unbiased=True) -> (Tensor, Tensor)
torch.allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) → bool
torch.argsort(input, dim=-1, descending=False, out=None) → LongTensor
torch.eq(input, other, out=None) → Tensor
torch.equal(input, other) → bool
torch.ge(input, other, out=None) → Tensor
torch.gt(input, other, out=None) → Tensor
torch.isfinite(tensor)[source]
tensor (Tensor) – A tensor to check
torch.kthvalue(input, k, dim=None, keepdim=False, out=None) -> (Tensor, LongTensor)
torch.le(input, other, out=None) → Tensor
torch.lt(input, other, out=None) → Tensor
torch.max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
torch.max(input, other, out=None) → Tensor
torch.min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
torch.min(input, other, out=None) → Tensor
torch.ne(input, other, out=None) → Tensor
torch.sort(input, dim=-1, descending=False, out=None) -> (Tensor, LongTensor)
torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)
torch.
abs
(input, out=None) → Tensor
Computes the element-wise absolute value of the given input
tensor.
outi=∣inputi∣\text{out}_{i} = |\text{input}_{i}| outi=∣inputi∣
Parameters
Example:
>>> torch.abs(torch.tensor([-1, -2, 3]))
tensor([ 1, 2, 3])
torch.
acos
(input, out=None) → Tensor
Returns a new tensor with the arccosine of the elements of input
.
outi=cos−1(inputi)\text{out}_{i} = \cos^{-1}(\text{input}_{i}) outi=cos−1(inputi)
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.3348, -0.5889, 0.2005, -0.1584])
>>> torch.acos(a)
tensor([ 1.2294, 2.2004, 1.3690, 1.7298])
torch.
add
()
torch.
add
(input, other, out=None)
Adds the scalar other
to each element of the input input
and returns a new resulting tensor.
out=input+other\text{out} = \text{input} + \text{other} out=input+other
If input
is of type FloatTensor or DoubleTensor, other
must be a real number, otherwise it should be an integer.
Parameters
-
input (Tensor) – the input tensor.
-
value (Number) – the number to be added to each element of
input
Keyword Arguments
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.0202, 1.0985, 1.3506, -0.6056])
>>> torch.add(a, 20)
tensor([ 20.0202, 21.0985, 21.3506, 19.3944])
torch.
add
(input, alpha=1, other, out=None)
Each element of the tensor other
is multiplied by the scalar alpha
and added to each element of the tensor input
. The resulting tensor is returned.
The shapes of input
and other
must be broadcastable.
out=input+alpha×other\text{out} = \text{input} + \text{alpha} \times \text{other} out=input+alpha×other
If other
is of type FloatTensor or DoubleTensor, alpha
must be a real number, otherwise it should be an integer.
Parameters
-
input (Tensor) – the first input tensor
-
alpha (Number) – the scalar multiplier for
other
-
other (Tensor) – the second input tensor
Keyword Arguments
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.9732, -0.3497, 0.6245, 0.4022])
>>> b = torch.randn(4, 1)
>>> b
tensor([[ 0.3743],
[-1.7724],
[-0.5811],
[-0.8017]])
>>> torch.add(a, 10, b)
tensor([[ 2.7695, 3.3930, 4.3672, 4.1450],
[-18.6971, -18.0736, -17.0994, -17.3216],
[ -6.7845, -6.1610, -5.1868, -5.4090],
[ -8.9902, -8.3667, -7.3925, -7.6147]])
torch.
addcdiv
(input, value=1, tensor1, tensor2, out=None) → Tensor
Performs the element-wise division of tensor1
by tensor2
, multiply the result by the scalar value
and add it to input
.
outi=inputi+value×tensor1itensor2i\text{out}_i = \text{input}_i + \text{value} \times \frac{\text{tensor1}_i}{\text{tensor2}_i} outi=inputi+value×tensor2itensor1i
The shapes of input
, tensor1
, and tensor2
must be broadcastable.
For inputs of type FloatTensor or DoubleTensor, value
must be a real number, otherwise an integer.
Parameters
-
input (Tensor) – the tensor to be added
-
value (Number, optional) – multiplier for tensor1/tensor2\text{tensor1} / \text{tensor2}tensor1/tensor2
-
tensor1 (Tensor) – the numerator tensor
-
tensor2 (Tensor) – the denominator tensor
-
out (Tensor, optional) – the output tensor.
Example:
>>> t = torch.randn(1, 3)
>>> t1 = torch.randn(3, 1)
>>> t2 = torch.randn(1, 3)
>>> torch.addcdiv(t, 0.1, t1, t2)
tensor([[-0.2312, -3.6496, 0.1312],
[-1.0428, 3.4292, -0.1030],
[-0.5369, -0.9829, 0.0430]])
torch.
addcmul
(input, value=1, tensor1, tensor2, out=None) → Tensor
Performs the element-wise multiplication of tensor1
by tensor2
, multiply the result by the scalar value
and add it to input
.
outi=inputi+value×tensor1i×tensor2i\text{out}_i = \text{input}_i + \text{value} \times \text{tensor1}_i \times \text{tensor2}_i outi=inputi+value×tensor1i×tensor2i
The shapes of tensor, tensor1
, and tensor2
must be broadcastable.
For inputs of type FloatTensor or DoubleTensor, value
must be a real number, otherwise an integer.
Parameters
-
input (Tensor) – the tensor to be added
-
value (Number, optional) – multiplier for tensor1.∗tensor2tensor1 .* tensor2tensor1.∗tensor2
-
tensor1 (Tensor) – the tensor to be multiplied
-
tensor2 (Tensor) – the tensor to be multiplied
-
out (Tensor, optional) – the output tensor.
Example:
>>> t = torch.randn(1, 3)
>>> t1 = torch.randn(3, 1)
>>> t2 = torch.randn(1, 3)
>>> torch.addcmul(t, 0.1, t1, t2)
tensor([[-0.8635, -0.6391, 1.6174],
[-0.7617, -0.5879, 1.7388],
[-0.8353, -0.6249, 1.6511]])
torch.
asin
(input, out=None) → Tensor
Returns a new tensor with the arcsine of the elements of input
.
outi=sin−1(inputi)\text{out}_{i} = \sin^{-1}(\text{input}_{i}) outi=sin−1(inputi)
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.5962, 1.4985, -0.4396, 1.4525])
>>> torch.asin(a)
tensor([-0.6387, nan, -0.4552, nan])
torch.
atan
(input, out=None) → Tensor
Returns a new tensor with the arctangent of the elements of input
.
outi=tan−1(inputi)\text{out}_{i} = \tan^{-1}(\text{input}_{i}) outi=tan−1(inputi)
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.2341, 0.2539, -0.6256, -0.6448])
>>> torch.atan(a)
tensor([ 0.2299, 0.2487, -0.5591, -0.5727])
torch.
atan2
(input, other, out=None) → Tensor
Element-wise arctangent of inputi/otheri\text{input}_{i} / \text{other}_{i}inputi/otheri with consideration of the quadrant. Returns a new tensor with the signed angles in radians between vector (otheri,inputi)(\text{other}_{i}, \text{input}_{i})(otheri,inputi) and vector (1,0)(1, 0)(1,0) . (Note that otheri\text{other}_{i}otheri , the second parameter, is the x-coordinate, while inputi\text{input}_{i}inputi , the first parameter, is the y-coordinate.)
The shapes of input
and other
must be broadcastable.
Parameters
-
input (Tensor) – the first input tensor
-
other (Tensor) – the second input tensor
-
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.9041, 0.0196, -0.3108, -2.4423])
>>> torch.atan2(a, torch.randn(4))
tensor([ 0.9833, 0.0811, -1.9743, -1.4151])
torch.
bitwise_not
(input, out=None) → Tensor
Computes the bitwise NOT of the given input tensor. The input tensor must be of integral or Boolean types. For bool tensors, it computes the logical NOT.
Parameters
Example
>>> torch.bitwise_not(torch.tensor([-1, -2, 3], dtype=torch.int8))
tensor([ 0, 1, -4], dtype=torch.int8)
torch.
ceil
(input, out=None) → Tensor
Returns a new tensor with the ceil of the elements of input
, the smallest integer greater than or equal to each element.
outi=⌈inputi⌉=⌊inputi⌋+1\text{out}_{i} = \left\lceil \text{input}_{i} \right\rceil = \left\lfloor \text{input}_{i} \right\rfloor + 1 outi=⌈inputi⌉=⌊inputi⌋+1
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.6341, -1.4208, -1.0900, 0.5826])
>>> torch.ceil(a)
tensor([-0., -1., -1., 1.])
torch.
clamp
(input, min, max, out=None) → Tensor
Clamp all elements in input
into the range [ min, max ] and return a resulting tensor:
yi={minif xi<minxiif min≤xi≤maxmaxif xi>maxy_i = \begin{cases} \text{min} & \text{if } x_i < \text{min} \\ x_i & \text{if } \text{min} \leq x_i \leq \text{max} \\ \text{max} & \text{if } x_i > \text{max} \end{cases} yi=⎩⎪⎪⎨⎪⎪⎧minximaxif xi<minif min≤xi≤maxif xi>max
If input
is of type FloatTensor or DoubleTensor, args min and max must be real numbers, otherwise they should be integers.
Parameters
-
input (Tensor) – the input tensor.
-
min (Number) – lower-bound of the range to be clamped to
-
max (Number) – upper-bound of the range to be clamped to
-
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
torch.
clamp
(input, *, min, out=None) → Tensor
Clamps all elements in input
to be larger or equal min.
If input
is of type FloatTensor or DoubleTensor, value
should be a real number, otherwise it should be an integer.
Parameters
-
input (Tensor) – the input tensor.
-
value (Number) – minimal value of each element in the output
-
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.0299, -2.3184, 2.1593, -0.8883])
>>> torch.clamp(a, min=0.5)
tensor([ 0.5000, 0.5000, 2.1593, 0.5000])
torch.
clamp
(input, *, max, out=None) → Tensor
Clamps all elements in input
to be smaller or equal max.
If input
is of type FloatTensor or DoubleTensor, value
should be a real number, otherwise it should be an integer.
Parameters
-
input (Tensor) – the input tensor.
-
value (Number) – maximal value of each element in the output
-
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.7753, -0.4702, -0.4599, 1.1899])
>>> torch.clamp(a, max=0.5)
tensor([ 0.5000, -0.4702, -0.4599, 0.5000])
torch.
cos
(input, out=None) → Tensor
Returns a new tensor with the cosine of the elements of input
.
outi=cos(inputi)\text{out}_{i} = \cos(\text{input}_{i}) outi=cos(inputi)
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 1.4309, 1.2706, -0.8562, 0.9796])
>>> torch.cos(a)
tensor([ 0.1395, 0.2957, 0.6553, 0.5574])
torch.
cosh
(input, out=None) → Tensor
Returns a new tensor with the hyperbolic cosine of the elements of input
.
outi=cosh(inputi)\text{out}_{i} = \cosh(\text{input}_{i}) outi=cosh(inputi)
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.1632, 1.1835, -0.6979, -0.7325])
>>> torch.cosh(a)
tensor([ 1.0133, 1.7860, 1.2536, 1.2805])
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=inputiother\text{out}_i = \frac{\text{input}_i}{\text{other}} outi=otherinputi
If input
is of type FloatTensor or DoubleTensor, other
should be a real number, otherwise it should be an integer
Parameters
-
{input} –
-
other (Number) – the number to be divided to each element of
input
-
{out} –
Example:
>>> a = torch.randn(5)
>>> a
tensor([ 0.3810, 1.2774, -0.2972, -0.3719, 0.4637])
>>> torch.div(a, 0.5)
tensor([ 0.7620, 2.5548, -0.5944, -0.7439, 0.9275])
torch.
div
(input, other, out=None) → Tensor
Each element of the tensor input
is divided by each element of the tensor other
. The resulting tensor is returned. The shapes of input
and other
must be broadcastable.
outi=inputiotheri\text{out}_i = \frac{\text{input}_i}{\text{other}_i} outi=otheriinputi
Parameters
-
input (Tensor) – the numerator tensor
-
other (Tensor) – the denominator tensor
-
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
[ 0.1815, -1.0111, 0.9805, -1.5923],
[ 0.1062, 1.4581, 0.7759, -1.2344],
[-0.1830, -0.0313, 1.1908, -1.4757]])
>>> b = torch.randn(4)
>>> b
tensor([ 0.8032, 0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051, 0.5676, 1.2637],
[ 0.2260, -3.4507, -1.2086, 6.8988],
[ 0.1322, 4.9764, -0.9564, 5.3480],
[-0.2278, -0.1068, -1.4678, 6.3936]])
torch.
digamma
(input, out=None) → Tensor
Computes the logarithmic derivative of the gamma function on input.
ψ(x)=ddxln(Γ(x))=Γ′(x)Γ(x)\psi(x) = \frac{d}{dx} \ln\left(\Gamma\left(x\right)\right) = \frac{\Gamma'(x)}{\Gamma(x)} ψ(x)=dxdln(Γ(x))=Γ(x)Γ′(x)
Parameters
input (Tensor) – the tensor to compute the digamma function on
Example:
>>> a = torch.tensor([1, 0.5])
>>> torch.digamma(a)
tensor([-0.5772, -1.9635])
torch.
erf
(input, out=None) → Tensor
Computes the error function of each element. The error function is defined as follows:
erf(x)=2π∫0xe−t2dt\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt erf(x)=π
2∫0xe−t2dt
Parameters
Example:
>>> torch.erf(torch.tensor([0, -1., 10.]))
tensor([ 0.0000, -0.8427, 1.0000])
torch.
erfc
(input, out=None) → Tensor
Computes the complementary error function of each element of input
. The complementary error function is defined as follows:
erfc(x)=1−2π∫0xe−t2dt\mathrm{erfc}(x) = 1 - \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt erfc(x)=1−π
2∫0xe−t2dt
Parameters
Example:
>>> torch.erfc(torch.tensor([0, -1., 10.]))
tensor([ 1.0000, 1.8427, 0.0000])
torch.
erfinv
(input, out=None) → Tensor
Computes the inverse error function of each element of input
. The inverse error function is defined in the range (−1,1)(-1, 1)(−1,1) as:
erfinv(erf(x))=x\mathrm{erfinv}(\mathrm{erf}(x)) = x erfinv(erf(x))=x
Parameters
Example:
>>> torch.erfinv(torch.tensor([0, 0.5, -1.]))
tensor([ 0.0000, 0.4769, -inf])
torch.
exp
(input, out=None) → Tensor
Returns a new tensor with the exponential of the elements of the input tensor input
.
yi=exiy_{i} = e^{x_{i}} yi=exi
Parameters
Example:
>>> torch.exp(torch.tensor([0, math.log(2.)]))
tensor([ 1., 2.])
torch.
expm1
(input, out=None) → Tensor
Returns a new tensor with the exponential of the elements minus 1 of input
.
yi=exi−1y_{i} = e^{x_{i}} - 1 yi=exi−1
Parameters
Example:
>>> torch.expm1(torch.tensor([0, math.log(2.)]))
tensor([ 0., 1.])
torch.
floor
(input, out=None) → Tensor
Returns a new tensor with the floor of the elements of input
, the largest integer less than or equal to each element.
outi=⌊inputi⌋\text{out}_{i} = \left\lfloor \text{input}_{i} \right\rfloor outi=⌊inputi⌋
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.8166, 1.5308, -0.2530, -0.2091])
>>> torch.floor(a)
tensor([-1., 1., -1., -1.])
torch.
fmod
(input, other, out=None) → Tensor
Computes the element-wise remainder of division.
The dividend and divisor may contain both for integer and floating point numbers. The remainder has the same sign as the dividend input
.
When other
is a tensor, the shapes of input
and other
must be broadcastable.
Parameters
-
input (Tensor) – the dividend
-
other (Tensor or float) – the divisor, which may be either a number or a tensor of the same shape as the dividend
-
out (Tensor, optional) – the output tensor.
Example:
>>> torch.fmod(torch.tensor([-3., -2, -1, 1, 2, 3]), 2)
tensor([-1., -0., -1., 1., 0., 1.])
>>> torch.fmod(torch.tensor([1., 2, 3, 4, 5]), 1.5)
tensor([ 1.0000, 0.5000, 0.0000, 1.0000, 0.5000])
torch.
frac
(input, out=None) → Tensor
Computes the fractional portion of each element in input
.
outi=inputi−⌊∣inputi∣⌋∗sgn(inputi)\text{out}_{i} = \text{input}_{i} - \left\lfloor |\text{input}_{i}| \right\rfloor * \operatorname{sgn}(\text{input}_{i}) outi=inputi−⌊∣inputi∣⌋∗sgn(inputi)
Example:
>>> torch.frac(torch.tensor([1, 2.5, -3.2]))
tensor([ 0.0000, 0.5000, -0.2000])
torch.
lerp
(input, end, weight, out=None)
Does a linear interpolation of two tensors start
(given by input
) and end
based on a scalar or tensor weight
and returns the resulting out
tensor.
outi=starti+weighti×(endi−starti)\text{out}_i = \text{start}_i + \text{weight}_i \times (\text{end}_i - \text{start}_i) outi=starti+weighti×(endi−starti)
The shapes of start
and end
must be broadcastable. If weight
is a tensor, then the shapes of weight
, start
, and end
must be