我有这个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)
我知道最好只发布你相关的代码部分,但这种代码实际上很不寻常的情况,我认为我应该发布所有这些。
这是我连续两次运行代码的截图,显然是第一次打印出我想要的输出就好了,然后下次冻结。有时它会在两者之间冻结.
当然,当我测试时,如果我能看到一些模式,我会把很多打印功能全部放在一边,但我不能,而且我在上面的代码中取出了所有这些打印功能。但事实是,这段代码会在中间冻结,并且没有一致性或“可复制性”。
我用谷歌搜索过但找不到其他有类似问题的人。
你传递了一个不好的价值给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装饰器注释掉的情况下运行你的代码,看看是否有任何奇怪的事情发生。