其他分享
首页 > 其他分享> > 我使用numba的“ Jit”装饰器运行代码时,Anaconda提示符冻结

我使用numba的“ Jit”装饰器运行代码时,Anaconda提示符冻结

作者:互联网

我有此python代码,应该可以正常运行.我正在Anaconda的Spyder Ipython控制台或Anaconda终端本身上运行它,因为这是我可以使用“ numba”库及其“ jit”装饰器的唯一方法.

但是,无论何时运行它,总是会“冻结”或“挂起”.代码本身没有错,否则我会得到一个错误.

有时,代码会一直正常运行,有时只是从第一个函数打印第一行,有时代码会停在中间的任何位置.

我尝试查看在哪种情况下会重现相同的问题,但是我无法获得任何见解.

我的代码是:

import time
import numpy as np
import random
from numba import vectorize, cuda, jit, njit, prange, float64, float32, int64
from numba.numpy_support import from_dtype
import numba

@jit(nopython = True)
def make_array(number_of_rows, row_size, starting_size):
    q = np.zeros((number_of_rows,row_size))
    q[:,0]=starting_size
    return(q)

q = make_array(5,5,5)

@jit(nopython = True)
def row_size(array):
    return(array.shape[1])
@jit(nopython = True)
def number_of_rows(array):
    return(array.shape[0])

@jit(nopython = True)
def foo(array):

    result = np.zeros(array.size).reshape(1,array.shape[1])
    result[:] = array[:]
    shedding_row = np.zeros(array.size).reshape(1,array.shape[1])
    birth_row = np.zeros(array.size).reshape(1,array.shape[1])
    for i in range((array.shape[0])):
        for j in range((array.shape[1])-1):
            if  result[i,j] !=0:
                shedding = (np.random.poisson( (result[i,j])**.2, 1))[0]
                birth = (np.random.poisson( (3), 1))[0]
                birth = 0
                result[i,j+1] = result[i,j] - shedding + birth
                shedding_row[i,j+1] = shedding
                birth_row[i,j+1] = birth
            if result[i,j] == 0:
                result[i,j] = result[i,j]
    return(result, shedding_row)


@jit(nopython = True)    
def foo_two(array):

    result = np.zeros(array.size).reshape(array.shape[0],array.shape[1])
    result_two = np.zeros(array.size).reshape(array.shape[0],array.shape[1])       
    i = 0

    while i != (result.shape[0]):

        fill_in_row=  0*np.arange(1 * result.shape[1]).reshape(1, result.shape[1])
        fill_in_row[0] = array[i]
        result[i], shedding_row = foo(fill_in_row)
        result_two[i] = shedding_row
        i+=1            
    return(result, result_two)

@jit(nopython = True)
def foo_three(array):
    array_sum = np.sum(array, axis = 0)
    array_sum = array_sum.reshape(1,array_sum.size)
    result = np.zeros(array_sum.size).reshape(1,array_sum.size)

    for i in range((result.shape[0])):
        for j in range((result.shape[1])):

            shed_death_param = .2
            shed_metastasis_param = .3
            combined_number = (int(array_sum[i,j])) *    (shed_death_param+shed_metastasis_param)
            for q in range(int(combined_number)):
                random_number = random.randint(1, 7)
                if random_number == 5:
                    result[i,j]+=1
            number_to_add = (int(array_sum[i,j])) - (int(combined_number))
            if j < row_size(array_sum) - 1:
                (array_sum[i,j+1]) += number_to_add
    return(result)


@jit(nopython = True)
def foo_four(array):
    result = np.zeros(array.size).reshape(1,array.size)
    for i in range((result.shape[0])):
        for j in range((result.shape[1])):
            if int(array[i,j])!= 0:
                for q in range(int(array[i,j])):
                     addition = np.zeros((1,result.shape[1]))
                     addition[0][j] = 1
                     result = np.concatenate((result, addition), axis=0)
    if result.shape[0]!=1:
        result = result[1:]
    return(result)


def the_process(array):

    array, master_shedding_array = (foo_two(array))
    master_metastasis_array = foo_three(master_shedding_array)
    new_array = (foo_four(master_metastasis_array))
    print("new_array is\n", new_array)
    return(array,new_array)

def the_bigger_process(array):
    big_array = make_array(1,row_size(array),0)
    big_metastasis_array = make_array(1,row_size(array),0)
    counter =0
    i = 0

    while counter < row_size(array)-1:
        print("We begin, before the_process is called")
        updated_array,metastasis_array = the_process(array)
        big_array = np.concatenate((big_array, updated_array), axis=0)      
        if sum( metastasis_array[0] ) != 0:
            big_metastasis_array = np.concatenate((big_metastasis_array, metastasis_array), axis=0)        
        i+=1           
        third_big_metastasis_array = big_metastasis_array[np.where(big_metastasis_array[:,i] == 1)]        
        array = third_big_metastasis_array
        counter+=1

    big_array = big_array[1:]
    big_metastasis_array = big_metastasis_array[1:]
    return(big_array,big_metastasis_array)   

something, big_metastasis_array = the_bigger_process(q)
print("something is\n",something)
print("big_metastasis_array is\n",big_metastasis_array)

我知道最好只发布与代码相关的部分,但是在这种情况下,代码实际上很好,所以我认为我应该全部发布.

这是当我连续两次运行代码时的屏幕截图,很明显,它第一次打印出我想要的输出,然后再次冻结.有时它冻结在enter image description here之间

当然,当我测试是否可以看到某种模式时,我到处都放置了许多打印功能,但是我没有,所以我在上面的代码中取出了所有这些打印功能.但是事实是,该代码将冻结在中间,并且没有一致性或“可复制性”.

我已经四处搜寻,但找不到其他遇到类似问题的人.

解决方法:

您正在将错误的值传递给np.random.poisson.在您的代码中,result [i,j]有时可以为负数,这会在numba中导致NaN,而在python中,它会返回实际(负数)值.在python中,您可能会遇到ValueError,但是numba失败的原因不同,它导致进程挂起.

您必须确定对您的特定问题是否有意义,但是如果我添加,请在#******注释之间进行检查:

@jit(nopython=True)
def foo(array):
    result = np.zeros(array.size).reshape(1, array.shape[1])
    result[:] = array[:]
    shedding_row = np.zeros(array.size).reshape(1, array.shape[1])
    birth_row = np.zeros(array.size).reshape(1, array.shape[1])
    for i in range((array.shape[0])):
        for j in range((array.shape[1]) - 1):
            if result[i, j] != 0:

                # ******
                if result[i, j] < 0:
                    continue
                # ******
                shedding = (np.random.poisson( (result[i, j])**.2, 1))[0]
                birth = (np.random.poisson((3), 1))[0]
                ....

在foo中,然后代码停止挂起.

作为一般的调试技巧,最好将带有jit装饰器的代码注释掉以运行代码,以查看是否发生了任何奇怪的情况.

标签:spyder,python,anaconda,jit,numba
来源: https://codeday.me/bug/20191013/1906249.html