代码之家  ›  专栏  ›  技术社区  ›  Mel

使用tensorflow,如何在拟合过程中找到一个纪元所需的时间?

  •  0
  • Mel  · 技术社区  · 7 年前

    我将继续学习本教程, https://www.tensorflow.org/tutorials/keras/basic_classification

    model.fit(train_images, train_labels, epochs=5, verbose =1) ,时间显示在python控制台中。我想通过使用 time.clock() .

    我假设当添加更多的时间段时,拟合时间会线性增加,但我想用图表来确定这一点。

    除了拟合1个历元,然后拟合2个历元,然后拟合3个历元等,对于越来越多的历元,如何计算训练时间(拟合时间)?

    1 回复  |  直到 7 年前
        1
  •  0
  •   timedacorn    7 年前

    使用自定义回调,可以绘制适合特定时期所需的总时间。

    class timecallback(tf.keras.callbacks.Callback):
        def __init__(self):
            self.times = []
            # use this value as reference to calculate cummulative time taken
            self.timetaken = time.clock()
        def on_epoch_end(self,epoch,logs = {}):
            self.times.append((epoch,time.clock() - self.timetaken))
        def on_train_end(self,logs = {}):
            plt.xlabel('Epoch')
            plt.ylabel('Total time taken until an epoch in seconds')
            plt.plot(*zip(*self.times))
            plt.show()
    

    然后将其作为回调传递给model.fit函数,如下所示

    timetaken = timecallback()
    model.fit(train_images, train_labels, epochs=5,callbacks = [timetaken])
    

    如果你想绘制每个历元的时间。您可以用on_epoch_end替换on_train_end方法。

    def on_epoch_end(self,epoch,logs= {}):
        # same as the on_train_end function
    
    推荐文章