繁体   English   中英

使用 Numba 时的进度条(tqdm 不工作)-Python

[英]Progress bar when using Numba (tqdm is not working) -Python

我一直在尝试使用 numba 运行代码,我还添加了打印以查看我的代码的进度:

from numba import jit,njit,prange
import numpy as np
# for minimum reproducible example
a=1e5
ar = np.random.rand(a)
at = np.random.rand(a)
an = np.random.rand(a)
###############################3


tau    = 1        # time lag
window = 6000


@njit(parallel=True)
def func_DB(ar,at,an):
    DBtotal= np.zeros((len(an)-tau))
    k = 0
    for i in prange(0,len(an)-tau,1):
        DBtotal[i] = np.sqrt((ar[i + tau]- ar[i])**2 +(at[i + tau]- at[i])**2 +(an[i + tau]- an[i])**2)
       ## To print the progress
         if i%1e5==0:
            k+=1
            print(k*1e5/len(DBtotal))
    return DBtotal


@njit(parallel=True)
def func_PVI(tau, window):
    PVI = np.zeros((len(DBtotal)))
    k = 0
    for i in prange(int(window/2),len(DBtotal)-int(window/2)): 
        PVI[i] = DBtotal[i]/np.sqrt((np.mean(DBtotal[i-int(window/2):i+int(window/2)]**2)))
       # To print the progress
        if i%1e5==0:
            k+=1
            print(k*1e5/len(DBtotal))
    return PVI 
DBtotal = func_DB(ar,at,an)
PVI     = func_PVI(DBtotal,tau, window)

但是,在代码运行时,我没有得到我预期的结果(即随着代码的进行,go 的值从 0 到 1)相反,我得到了这个:

Out[:] 0.009479390005044932
      0.009479390005044932
      0.009479390005044932
      0.009479390005044932
      0.009479390005044932
      0.018958780010089864

有人可以建议一种查看代码进度的方法吗?

此外,我们将不胜感激任何使代码更高效的建议!

我将 function 拆成碎片,并在其周围包裹了一个 tqdm。

代替

@jit(nopython=True)
def dothings(A, rows, cols):
    for r in range(rows):
        for c in range(cols):
            stuff...

dothings(data, data.shape[0], data.shape[1])

我用了

rows=data.shape[0]
@jit(nopython=True)
def dothings(A, cols, r):
#    for r in range(rows):
        for c in range(cols):
            stuff...
    
for r in tqdm.tqdm(range(rows), total=rows):
    dothings(data, data.shape[1], r)

尝试这个:

from numba import njit,prange,objmode
@njit(parallel=True)
def harmonic_load_flow_func_time_inside():
    with objmode(time1='f8'):
        time1 = time.perf_counter()
    calc = 0
    for x in prange(1000000):
        calc += x

    with objmode():
        print('time: {}'.format(time.perf_counter() - time1),end='\r')

我在 github 上找到了一个小项目,它可以自动完成所有这些工作。 它被称为numba-progress

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM