- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我想加速我在 numba 的帮助下编写的程序 jit
.然而jit
似乎与许多 scipy 函数不兼容,因为它们使用 try
... except
...结构jit
无法处理(我对这一点是否正确?)
我想到的一个相对简单的解决方案是复制我需要的 scipy 源代码并删除 try
except
部分(我已经知道它不会出错,所以 try
部分将始终有效)
但是我不喜欢这个解决方案,我不确定它是否可行。
我的代码结构如下所示
import scipy.integrate as integrate
from scipy optimize import curve_fit
from numba import jit
def fitfunction():
...
@jit
def function(x):
# do some stuff
try:
fit_param, fit_cov = curve_fit(fitfunction, x, y, p0=(0,0,0), maxfev=500)
for idx in some_list:
integrated = integrate.quad(lambda x: fitfunction(fit_param), lower, upper)
except:
fit_param=(0,0,0)
...
现在这会导致以下错误:
LoweringError: Failed at object (object mode backend)
我假设这是由于 jit
无法处理 try
except
(如果我只将 jit
放在 curve_fit
和 integrate.quad
部分并围绕我自己的 try
except
结构工作,它也不起作用)
import scipy.integrate as integrate
from scipy optimize import curve_fit
from numba import jit
def fitfunction():
...
@jit
def integral(lower, upper):
return integrate.quad(lambda x: fitfunction(fit_param), lower, upper)
@jit
def fitting(x, y, pzero, max_fev)
return curve_fit(fitfunction, x, y, p0=pzero, maxfev=max_fev)
def function(x):
# do some stuff
try:
fit_param, fit_cov = fitting(x, y, (0,0,0), 500)
for idx in some_list:
integrated = integral(lower, upper)
except:
fit_param=(0,0,0)
...
有没有办法使用jit
与 scipy.integrate.quad
和 curve_fit
无需手动删除所有 try
except
来自 scipy 代码的结构?
它甚至会加速代码吗?
最佳答案
Numba 只是不是一个用于加速代码的通用库。有一类问题可以用 numba 以更快的方式解决(特别是如果你有数组循环,数字运算)但其他一切要么(1)不受支持要么(2)只是稍微快一点甚至很多较慢。
[...] would it even speed up the code?
SciPy 已经是一个高性能库,所以在大多数情况下,我预计 numba 的性能会更差(或者很少:稍微好一点)。你可能会做一些profiling找出瓶颈是否真的存在于您jit
的代码中,然后您可以得到一些改进。但我怀疑瓶颈将出现在 SciPy 的编译代码中,并且该编译代码可能已经进行了高度优化(因此您真的不太可能找到可以“仅”与该代码竞争的实现)。
Is there a way to use jit with scipy.integrate.quad and curve_fit without manually deleting all try except structures from the scipy code?
正如您正确假设的那样,目前 numba 根本不支持 try
和 except
。
2.6.1. Language
2.6.1.1. Constructs
Numba strives to support as much of the Python language as possible, but some language features are not available inside Numba-compiled functions. The following Python language features are not currently supported:
[...]
- Exception handling (
try
..except
,try
..finally
)
所以这里的答案是否。
关于python - Numba jit 与 scipy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317665/
我想将一个类对象传递给一个函数。我可以让它工作,但我想知道是否有我可以分配的类型?我有一个我正在尝试做的“最小”示例。 spec = [("a", float64),("b",float64)] @j
我有一个简单的函数来对扑克手牌进行排序(手牌是字符串)。 我用 rA,rB = rank(a),rank(b) 调用它,这是我的实现。没有 @jit(nopython=True) 也能很好地工作,但是
我在这里有一个简单的例子来帮助我理解使用 numba 和 cython。我是 numba 和 cython 的新手。我已经尽力结合所有技巧来使 numba 更快,并且在某种程度上,cython 也是如
我正在使用 numbas @jit 装饰器在 python 中添加两个 numpy 数组。如果我使用 @jit 与 python 相比,性能是如此之高。 然而,即使我传入 @numba.jit(nop
我需要为通用指标构建相异矩阵。由于我需要算法快速运行,所以我在 nopython 模式下使用了 numba 0.35。这是我的代码 import numpy as np from numba impo
Numba Cuda 有 syncthreads() 来同步一个 block 中的所有线程。如何在不退出当前内核的情况下同步网格中的所有 block ? 在 C-Cuda 中有一个 cooperati
有人尝试在Google合作伙伴中使用numba吗?我只是不知道如何在此环境中进行设置。 此刻,我陷入了错误library nvvm not found。 最佳答案 将此代码复制到单元格中。这个对我有用
我想编写一个函数,它既可以作为 jitted 函数运行,也可以作为普通 python 或对象模式 numba 运行,具体取决于 numba 是否能够进行类型推断。我实际上更喜欢普通的 python,但
我有一个非常简单的问题我无法解决。 我正在使用 Numba 和 Cuda。我有一个列表 T=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0] 我想要一个包含列表元素的元组,如
我正在测试一些采用 numpy 数组的函数的 numba 性能,并比较: import numpy as np from numba import jit, vectorize, float64 im
我正在使用 Scipy 的 interpolate.interp1d 在 Python3 中插入一维数组。我想将它与 numba 一起使用,但不支持 scipy 和此功能。是否有 numba 支持
我是 Numba 的新手,我正在尝试使用 Numba(版本 0.54.1)在 Python 中实现旧的 Fortran 代码,但是当我添加 parallel = True 时,程序实际上变慢了.我的程
我需要在 Python 中创建一个位数组。到目前为止,我发现可以使用 bitarray 生成非常节省内存的数组。模块。 然而,我的最终目的是使用来自Numba 的@vectorize 装饰器。 . N
我认为这是一个简单的问题,但我发现 numba 文档缺乏关于如何将字符串类型与 numpy 数组和字典一起使用的信息。我有一个我想使用 numba 的函数,它需要一个邮政编码列表,然后是一个映射邮政编
假设我有两个功能 def my_sub1(a): return a + 2 def my_main(a): a += 1 b = mysub1(a) return b
在以下用于逻辑比较的 numba 编译函数中,性能下降的原因可能是什么: from numba import njit t = (True, 'and_', False) #@njit(boolean
我的代码使用如下列表的笛卡尔积: import itertools cartesian_product = itertools.product(list('ABCDEF'), repeat=n) n可
我正在使用 Numba(版本 0.37.0)来优化 GPU 代码。我想使用组合矢量化函数(使用 Numba 的 @vectorize 装饰器)。 导入和数据: import numpy as np f
我想知道在 numba 函数中计算两个列表的交集的最快方法。只是为了澄清:两个列表的交集示例: Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2
我正在使用 Numba 非 python 模式和一些 NumPy 函数。 @njit def invert(W, copy=True): ''' Inverts elementwise
我是一名优秀的程序员,十分优秀!