每当我尝试在类方法上使用@jit时,都会收到IndentationError:意外缩进 [英] Whenever I try to use @jit on my class method, I get IndentationError: unexpected indent

查看:99
本文介绍了每当我尝试在类方法上使用@jit时,都会收到IndentationError:意外缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几天,以使@jit能够加快我的代码的速度. 最后,我遇到了这个问题,描述了将@jit添加到对象方法: http://williamjshipman.wordpress .com/2013/12/24/learning-python-eight-ways-to-filter-an-image

I've been trying for several days to get @jit working to speed up my code. Finally I came across this, describing adding @jit to object methods: http://williamjshipman.wordpress.com/2013/12/24/learning-python-eight-ways-to-filter-an-image

我有一个名为GentleBoostC的类,并且我想加快其中称为train的方法的速度. train接受三个参数(一个2D数组,一个1D数组和一个整数),并且不返回任何内容.

I have a class called GentleBoostC and I want to speed up the method within it that's called train. train accepts three arguments (a 2D array, a 1D array, and an integer), and returns nothing.

这就是我在代码中所拥有的:

This is what I have in code:

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

但是我一直遇到缩进错误,指向定义训练函数的行.我的缩进没有错.我已经缩进了我的整个代码.而且,如果我用@jit注释掉这一行,那么就没有问题.

But I keep getting an indentation error, pointing to the line that defines the train function. There is nothing wrong with my indents. I have re-indented my entire code. And if I comment out the line with @jit, then there are no problems.

这是确切的错误:

   @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

推荐答案

根据我在文档中看到的内容,您不能将装饰器应用于方法;您看到的错误是由于JIT解析器不在class语句的上下文中而不处理源代码缩进.

From what I can see from the documentation, you cannot apply the decorator to a method; the error you see is from the JIT parser not handling the source code indentation when not in the context of a class statement.

如果要编译该方法的主体,则需要将其分解为一个单独的函数,然后从该方法中调用该函数:

If you want the body of that method to be compiled, you'll need to factor it out to a separate function, and call that function from the method:

@jit(void(object_, float_[:,:], int_[:], int_)) 
def train_function(instance, X, y, H):
    # do stuff

class GentleBoostC(object):
    def train(self, X, y, H):
        train_function(self, X, y, H)    

这篇关于每当我尝试在类方法上使用@jit时,都会收到IndentationError:意外缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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