PyTorch 阶乘函数
PyTorch Factorial Function
似乎没有用于计算阶乘的 PyTorch 函数。在 PyTorch 中有没有办法做到这一点?我希望在 Torch 中手动计算泊松分布(我知道它存在:https://pytorch.org/docs/stable/generated/torch.poisson.html)并且该公式需要分母中的阶乘。
内置的 math
模块 (docs) 提供了 returns 给定积分的阶乘作为 int
.
的函数
import math
x = math.factorial(5)
print(x)
print(type(x))
输出
120
<class 'int'>
我认为您可以找到 torch.jit._builtins.math.factorial
BUT pytorch
以及 numpy
和 scipy
(Factorial in numpy and scipy) 使用 python
的内置函数 math.factorial
:
import math
import numpy as np
import scipy as sp
import torch
print(torch.jit._builtins.math.factorial is math.factorial)
print(np.math.factorial is math.factorial)
print(sp.math.factorial is math.factorial)
True
True
True
但是,相比之下,scipy
除了“主流”math.factorial
之外,还包含非常“特殊”的阶乘函数scipy.special.factorial
。与 math
模块中的函数不同,它对数组进行操作:
from scipy import special
print(special.factorial is math.factorial)
False
# the all known factorial functions
factorials = (
math.factorial,
torch.jit._builtins.math.factorial,
np.math.factorial,
sp.math.factorial,
special.factorial,
)
# Let's run some tests
tnsr = torch.tensor(3)
for fn in factorials:
try:
out = fn(tnsr)
except Exception as err:
print(fn.__name__, fn.__module__, ':', err)
else:
print(fn.__name__, fn.__module__, ':', out)
factorial math : 6
factorial math : 6
factorial math : 6
factorial math : 6
factorial scipy.special._basic : tensor(6., dtype=torch.float64)
tnsr = torch.tensor([1, 2, 3])
for fn in factorials:
try:
out = fn(tnsr)
except Exception as err:
print(fn.__name__, fn.__module__, ':', err)
else:
print(fn.__name__, fn.__module__, ':', out)
factorial math : only integer tensors of a single element can be converted to an index
factorial math : only integer tensors of a single element can be converted to an index
factorial math : only integer tensors of a single element can be converted to an index
factorial math : only integer tensors of a single element can be converted to an index
factorial scipy.special._basic : tensor([1., 2., 6.], dtype=torch.float64)
似乎没有用于计算阶乘的 PyTorch 函数。在 PyTorch 中有没有办法做到这一点?我希望在 Torch 中手动计算泊松分布(我知道它存在:https://pytorch.org/docs/stable/generated/torch.poisson.html)并且该公式需要分母中的阶乘。
内置的 math
模块 (docs) 提供了 returns 给定积分的阶乘作为 int
.
import math
x = math.factorial(5)
print(x)
print(type(x))
输出
120
<class 'int'>
我认为您可以找到 torch.jit._builtins.math.factorial
BUT pytorch
以及 numpy
和 scipy
(Factorial in numpy and scipy) 使用 python
的内置函数 math.factorial
:
import math
import numpy as np
import scipy as sp
import torch
print(torch.jit._builtins.math.factorial is math.factorial)
print(np.math.factorial is math.factorial)
print(sp.math.factorial is math.factorial)
True
True
True
但是,相比之下,scipy
除了“主流”math.factorial
之外,还包含非常“特殊”的阶乘函数scipy.special.factorial
。与 math
模块中的函数不同,它对数组进行操作:
from scipy import special
print(special.factorial is math.factorial)
False
# the all known factorial functions
factorials = (
math.factorial,
torch.jit._builtins.math.factorial,
np.math.factorial,
sp.math.factorial,
special.factorial,
)
# Let's run some tests
tnsr = torch.tensor(3)
for fn in factorials:
try:
out = fn(tnsr)
except Exception as err:
print(fn.__name__, fn.__module__, ':', err)
else:
print(fn.__name__, fn.__module__, ':', out)
factorial math : 6
factorial math : 6
factorial math : 6
factorial math : 6
factorial scipy.special._basic : tensor(6., dtype=torch.float64)
tnsr = torch.tensor([1, 2, 3])
for fn in factorials:
try:
out = fn(tnsr)
except Exception as err:
print(fn.__name__, fn.__module__, ':', err)
else:
print(fn.__name__, fn.__module__, ':', out)
factorial math : only integer tensors of a single element can be converted to an index
factorial math : only integer tensors of a single element can be converted to an index
factorial math : only integer tensors of a single element can be converted to an index
factorial math : only integer tensors of a single element can be converted to an index
factorial scipy.special._basic : tensor([1., 2., 6.], dtype=torch.float64)