簡體   English   中英

如何使用 tqdm 在一行中打印雙循環 output

[英]how to print double loop output in one line using tqdm

我想使用 tqdm 在一行中打印雙循環 output

我嘗試:

from tqdm import tqdm

epochs = 3
num_batch=5
for epoch_step in range(epochs):
    with tqdm(range(num_batch)) as pbar:
        for batch_step in pbar:
            pbar.set_description(f"Epochs {epoch_step+1}/{epochs}")
            pbar.set_postfix({'batch loss': 'This is batch loss' })
        pbar.set_postfix({'epoch loss': 'This is epoch loss'})           

並期待 output:

Epochs 1/3: 100%|██████████| 5/5 [00:00<00:00, 890.13it/s, batch loss=This is epoch loss]
Epochs 2/3: 100%|██████████| 5/5 [00:00<00:00, 677.24it/s, batch loss=This is epoch loss]
Epochs 3/3: 100%|██████████| 5/5 [00:00<00:00, 969.78it/s, batch loss=This is epoch loss]

但這是真正的 output:

Epochs 1/3: 100%|██████████| 5/5 [00:00<00:00, 890.13it/s, batch loss=This is batch loss]
Epochs 2/3: 100%|██████████| 5/5 [00:00<00:00, 677.24it/s, batch loss=This is batch loss]
Epochs 3/3: 100%|██████████| 5/5 [00:00<00:00, 969.78it/s, batch loss=This is batch loss]

我如何嘗試修復我的代碼以獲得預期的 output?

不確定這是否是你想要的:

epochs = 3
num_batch=5
for epoch_step in range(epochs):
    pbar = tqdm(range(num_batch))
    pbar.set_postfix({'epoch loss': 'This is epoch loss'})
    time.sleep(2)
    for batch_step in pbar:
        pbar.set_description(f"Epochs {epoch_step+1}/{epochs}")
        pbar.set_postfix({'batch loss': 'This is batch loss' })
        time.sleep(2)

您也可以使用 tqdm.trange() 代替 tqdm.tqdm(range())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM