Python/Numba:scipy.special.gammainc()出现未知属性错误 [英] Python/Numba: Unknown attribute error with scipy.special.gammainc()

查看:128
本文介绍了Python/Numba:scipy.special.gammainc()出现未知属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用@jit装饰器运行代码时出现错误.似乎找不到函数scipy.special.gammainc()的某些信息:

I am having an error when running code using the @jit decorator. It appears that some information for the function scipy.special.gammainc() can't be located:

Failed at nopython (nopython frontend)
Unknown attribute 'gammainc' for Module(<module 'scipy.special' from 'C:\home\Miniconda\lib\site-packages\scipy\special\__init__.pyc'>) $164.2 $164.3 = getattr(attr=gammainc, value=$164.2)

没有@jit装饰器,代码将正常运行.也许需要一些使Numba可见scipy.special模块的属性吗?

Without the @jit decorator the code will run fine. Maybe there is something required to make the attributes of the scipy.special module visible to Numba?

在此先感谢您的任何建议,评论等.

Thanks in advance for any suggestions, comments, etc.

推荐答案

问题是gammainc并不是Numba固有的处理方法的一小部分(请参阅

The problem is that gammainc isn't one of the small list of functions that Numba inherently knows how to deal with (see http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html) - in fact none of the scipy functions are. This means you can't use it in "nopython" mode, unfortunately - it just has to treat it as a normal python function call.

如果删除nopython=True,它将正常工作.但是,这并不十分令人满意,因为它可能会更慢.没有看到您的代码,很难知道确切的建议.但是,通常:

If you remove nopython=True, it should work. However, that isn't hugely satisfactory, because it may well be slower. Without seeing your code it's difficult to know exact what to suggest. However, in general:

    即使没有nopython,也会加快
  • 循环(不包含gammainc之类的循环).

  • loops (that don't contain things like gammainc) will be sped up, even without nopython.

gammainc是一个"ufunc",这意味着它可以一次轻松地应用于整个阵列,并且无论如何应该迅速运行.

gammainc is a "ufunc", which means it can be readily applied to a whole array at a time, and should run quickly anyway.

您可以调用func.inspect_types()来查看它是否可以编译.

you can call func.inspect_types() to see it's been able to compile.

一个简单的例子:

from scipy.special import gammainc
import numba as nb
import numpy as np

@nb.jit # note - no "nopython"
def f(x):
  for n in range(x.shape[0]):
    x[n] += 1
  y = gammainc(x,2.5)
  for n in range(y.shape[0]):
    y[n] -= 1
  return y

f(np.linspace(0,20)) # forces it to be JIT'd and outputs an array

然后f.inspect_types()将这两个循环标识为提升循环",这意味着它们将被JIT并快速运行. gammainc的位不是JIT'd,而是立即应用于整个数组,因此也应该很快.

Then f.inspect_types() identifies the two loops as "lifted loops", meaning they'll be JIT'd and run quickly. The bit with gammainc is not JIT'd, but is applied to the whole array at once and so should be fast too.

这篇关于Python/Numba:scipy.special.gammainc()出现未知属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆