【发布时间】:2014-09-07 15:11:05
【问题描述】:
我几天来一直在努力让@jit 工作以加快我的代码速度。
最后我遇到了这个,描述了在对象方法中添加@jit:
http://williamjshipman.wordpress.com/2013/12/24/learning-python-eight-ways-to-filter-an-image
我有一个名为GentleBoostC 的类,我想加快其中名为train 的方法。
train 接受三个参数(一个二维数组、一个一维数组和一个整数),并且不返回任何内容。
这是我在代码中的内容:
import numba
from numba import jit, autojit, int_, void, float_, object_
class GentleBoostC(object):
# lots of functions
# and now the function I want to speed up
@jit (void(object_,float_[:,:],int_[:],int_))
def train(self, X, y, H):
# do stuff
但我不断收到缩进错误,指向定义 train 函数的行。我的缩进没有任何问题。我重新缩进了我的整个代码。如果我用@jit 注释掉这一行,那么就没有问题了。
这是确切的错误:
@jit (void(object_,float_[:,:],int_[:],int_))
File "C:\Users\app\Anaconda\lib\site-packages\numba\decorators.py", line 224, in _jit_decorator
nopython=nopython, func_ast=func_ast, **kwargs)
File "C:\Users\app\Anaconda\lib\site-packages\numba\decorators.py", line 133, in compile_function
func_env = pipeline.compile2(env, func, restype, argtypes, func_ast=func_ast, **kwds)
File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 133, in compile2
func_ast = functions._get_ast(func)
File "C:\Users\app\Anaconda\lib\site-packages\numba\functions.py", line 89, in _get_ast
ast.PyCF_ONLY_AST | flags, True)
File "C:\Users\app\Documents\Python Scripts\gentleboost_c_class_jit_v5_nolimit.py", line 1
def train(self, X, y, H):
^
IndentationError: unexpected indent
【问题讨论】:
-
使用
python -tt运行您的脚本。您可能确实有缩进问题。cat -eT filename.py显示了什么?查看@jit行和周围的上下文。 -
那么当我注释掉@jit 行时,为什么它完全没有问题?无论如何,我该如何使用 python -tt?我正在使用 Anaconda 的 Spyder 运行 python - 我将如何从那里做呢?
-
两个选项:
@jit行混合制表符和空格与其他行不一致,或者您正在使用尚不支持装饰器的古老 Python 版本。 -
我使用的是 Python 2.7
-
我的@jit 行正确吗?