当前位置: 首页 > 知识库问答 >
问题:

PyTorch阶乘函数

齐奕
2023-03-14

似乎没有一个PyTorch函数来计算阶乘。在Pytork中有这样做的方法吗?我希望手动计算火炬中的泊松分布(我知道存在这种情况:https://pytorch.org/docs/stable/generated/torch.poisson.html)这个公式要求分母中有一个阶乘。

泊松分布:https://en.wikipedia.org/wiki/Poisson_distribution

共有2个答案

皇甫智明
2023-03-14

内置的数学模块(docs)提供了一个函数,该函数以int的形式返回给定积分的阶乘。

import math

x = math.factorial(5)
print(x)
print(type(x))

输出

120
<class 'int'>
杜辉
2023-03-14

我想你可以在火炬上找到它。准时制_内置的。数学factorial但是pytorch以及numpyscipy(numpy和scipy中的factorial)使用python的内置数学。阶乘:

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模块中的函数不同,它在阵列上运行:

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)
 类似资料:
  • 计算一个数字的阶乘。 使用递归。如果 n 小于或等于 1 ,则返回 1 。否则返回 n 和 n - 1 的阶乘。如果 n 是负数,则会引发异常。 const factorial = n => n < 0 ? (() => { throw new TypeError('Negative numbers are not allowed!'); })()

  • 问题内容: 寻找JavaScript 中 阶乘 函数的真正快速实现。有什么建议吗? 问题答案: 您可以[搜索(1 …100)!在WolframAlpha上预先计算阶乘序列。 前100个数字是: 如果仍要自己计算值,则可以使用备忘录: 编辑:21.08.2014 解决方案2 我认为添加一个 懒惰的* 迭代 阶乘函数 的工作示例将很有用,该 函数 使用 大数 来获得带有 备忘录的 准确 结果,并将 缓

  • 问题内容: 我想使用for循环在Java中执行阶乘程序。例如,我想接受用户输入,说,然后相乘。我需要构建循环的帮助。到目前为止,我不知道去哪里。 问题答案: 尝试 正如@Marko Topolnik在评论中提到的那样,该代码将适用于输入最多12的输入。对于较大的输入,由于溢出将输出无穷大。 对于大于12的数字,您应使用更高的数据类型,例如 你可以试试:

  • 我如何使程序执行一个新的或重复的操作,或要求用户再次输入一个数字,并知道它的阶乘。

  • 我刚刚开始编写Haskell,基本上是因为我在寻找一种比C#更强大的数学语言,现在我很困惑。 现在我只想找到4的阶乘,然后打印出来,这是我到目前为止写的: 当我试着调试它时,我得到了 错误:(3,8)ghc:无法匹配预期类型

  • Python3 实例 整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,0的阶乘为1。即:n!=1×2×3×...×n。 #!/usr/bin/python3 # Filename : test.py # author by : www.runoob.com # 通过用户输入数字计算阶乘 # 获取用户输入的数字 num = int(input("请输入一个数字: ")) fa

  • 本文向大家介绍Julia命令式阶乘,包括了Julia命令式阶乘的使用技巧和注意事项,需要的朋友参考一下 示例 长格式语法可用于定义多行功能。当我们使用命令式结构(例如循环)时,这很有用。返回尾部位置的表达式。例如,下面的函数使用for循环来计算某个整数的阶乘n: 用法: 在较长的函数中,通常会看到所return使用的语句。该return语句在尾部位置不是必需的,但有时仍为清楚起见而使用。例如,编写

  • 我在Python中定义了一个阶乘函数,如下所示: python程序返回一个非常大的数值,计算值为100(如预期的那样)。朱莉娅返回0。对于较小的数字(如10),它们都起作用。 我有两个问题: 为什么Python可以处理这个问题,而Julia不能。 为什么Julia不抛出错误而只打印0?