简体   繁体   English

如何在 Tensorflow 的深度学习期间测量每批次的训练时间?

[英]How to measure training time per batches during Deep Learning in Tensorflow?

I want to measure training time per batches during Deep Learning in Tensorflow.我想在 Tensorflow 中测量深度学习期间每批次的训练时间。 There are several ways to measure training time per epochs, but I cannot find how to measure training time per batches.有几种方法可以测量每个时期的训练时间,但我找不到如何测量每批次的训练时间。

I tried Tensorboard, but I don't know how to add some kind of 'execution time' scalars in tensorboard callbacks.我试过 Tensorboard,但我不知道如何在 tensorboard 回调中添加某种“执行时间”标量。

And I also tried to override the function 'train_step' but it didn't work the way I want.而且我还尝试覆盖 function 'train_step' 但它没有按我想要的方式工作。

You can do this by creating a keras custom callback.您可以通过创建 keras 自定义回调来做到这一点。 The code below will print out the time it takes to process each batch, the training accuracy for that batch and the loss for that batch下面的代码将打印出处理每个批次所需的时间、该批次的训练准确度以及该批次的损失

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 ) 

Now you are going to get tons of data.现在您将获得大量数据。 When you run model.fit include当你运行 model.fit 包括

history=model.fit(etc.... ,verbose=0, callbacks=[batch_timer()]

I set verbose=0 because if you don't the printout gets a bit messy because model.fit printing gets messed up by the callback printing我设置了 verbose=0 因为如果你不这样做打印输出会有点乱,因为 model.fit 打印被回调打印搞砸了

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

相关问题 使用 Tensorflow 进行深度学习:一次学习一个元素的个性化训练循环 - Deep learning with Tensorflow: personalized training loop that learn with one element at a time Tensorflow 将训练数据拆分为批次 - Tensorflow splitting training data to batches 如何从TFrecord创建批量以在Tensorflow中训练网络? - how to create batches from TFrecord for training network in tensorflow? TensorFlow:如何确定我们是否要将训练数据集分为几批 - TensorFlow: how to determine if we want to break the training dataset into batches 如何在输入tensorflow会话进行训练时将numpy数组转换为批次 - How to convert numpy array to batches while feeding tensorflow session for training 我将如何测量/记录 keras / tensorflow 的人工神经网络算法的总训练时间? - How would I measure/record the total training time for an artificial neural network algorithm for keras / tensorflow? Tensorflow深度学习中的未实现错误 - UnimplementedError in Tensorflow deep learning PyTorch - 如何在训练期间获得学习率? - PyTorch - How to get learning rate during training? 使用 Keras 训练深度学习检查点 - Training checkpoints for deep learning with Keras 如何为深度学习训练数据集创建真实边界框? - How are ground truth bounding boxes created for a deep learning training dataset?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM