![](/img/trans.png)
[英]Deep learning with Tensorflow: personalized training loop that learn with one element at a time
[英]How to measure training time per batches during Deep Learning in Tensorflow?
我想在 Tensorflow 中測量深度學習期間每批次的訓練時間。 有幾種方法可以測量每個時期的訓練時間,但我找不到如何測量每批次的訓練時間。
我試過 Tensorboard,但我不知道如何在 tensorboard 回調中添加某種“執行時間”標量。
而且我還嘗試覆蓋 function 'train_step' 但它沒有按我想要的方式工作。
您可以通過創建 keras 自定義回調來做到這一點。 下面的代碼將打印出處理每個批次所需的時間、該批次的訓練准確度以及該批次的損失
class batch_timer(keras.callbacks.Callback):
def __init__(self ):
super(batch_timer, self).__init__()
def on_train_batch_begin(self, batch, logs=None):
self.start_time=time.time()
def on_train_batch_end(self, batch, logs=None):
stop_time=time.time()
duration =stop_time-self.start_time
acc=logs.get('accuracy')* 100 # get training accuracy
loss=logs.get('loss')
msg='processing batch {0:6s} duration= {1:12.4f} accuracy= {2:8.3f} loss: {3:8.5f}'.format(str(batch), duration, acc, loss)
print(msg )
現在您將獲得大量數據。 當你運行 model.fit 包括
history=model.fit(etc.... ,verbose=0, callbacks=[batch_timer()]
我設置了 verbose=0 因為如果你不這樣做打印輸出會有點亂,因為 model.fit 打印被回調打印搞砸了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.