簡體   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