简体   繁体   English

测量 Keras 层执行时间的正确方法

[英]Correct way of measuring execution time of Keras layers

I am trying to check the execution speed on different layers of a Keras model (Using keras from tensorflow 2.3.0 v)我正在尝试检查 Keras model 不同层的执行速度(使用 keras 来自 Z2C39BC19B701AC36FDC04D vv 的 keras)。

I took the code from this repo and just modified it, to calculate the time using timer() from from timeit import default_timer我从这个repo中获取了代码并对其进行了修改,以使用from timeit import default_timertimer()计算时间

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from timeit import default_timer as timer

def time_per_layer(model):
    new_model = model
    times = np.zeros((len(model.layers), 2))
    inp = np.ones((70, 140, 1))
    for i in range(1, len(model.layers)):
        new_model = tf.keras.models.Model(inputs=[model.input], outputs=[model.layers[-i].output])
        # new_model.summary()
        new_model.predict(inp[None, :, :, :])
        t_s = timer()
        new_model.predict(inp[None, :, :, :])
        t_e2 = timer() - t_s
        times[i, 1] = t_e2
        del new_model
    for i in range(0, len(model.layers) - 1):
        times[i, 0] = abs(times[i + 1, 1] - times[i, 1])
    times[-1, 0] = times[-1, 1]
    return times


times = time_per_layer(model)
plt.style.use('ggplot')
x = [model.layers[-i].name for i in range(1,len(model.layers))]
#x = [i for i in range(1,len(model.layers))]
g = [times[i,0] for i in range(1,len(times))]
x_pos = np.arange(len(x))
plt.bar(x, g, color='#7ed6df')
plt.xlabel("Layers")
plt.ylabel("Processing Time")
plt.title("Processing Time of each Layer")
plt.xticks(x_pos, x,rotation=90)

plt.show()

Is this the right way of measuring the execution time of different layers?这是衡量不同层执行时间的正确方法吗?

I would say that there is no right way to measure execution time of a different layers just like that because我会说没有正确的方法来测量不同层的执行时间,因为

  1. Neural networks work as a whole (the whole is more than the sum of its parts).神经网络作为一个整体工作(整体大于其部分的总和)。 You cannot unplug a layer from the middle of a trained network without breaking the system therefore measuring how long it processes something is not particularly useful.你不能在不破坏系统的情况下从训练网络的中间拔出层,因此测量它处理某事的时间并不是特别有用。

  2. The execution time of a layer also depends on the previous layer.一层的执行时间也取决于前一层。 If you change previous layers from having 1 neuron to having [insert large number] of neurons, the execution time of the following layer will change even it the layer itself stays unchanged.如果您将先前的层从具有 1 个神经元更改为具有 [插入大量] 神经元,则即使该层本身保持不变,下一层的执行时间也会发生变化。 So it is basically impossible to measure execution time of a layer in an insolation.所以基本上不可能测量一个层在日照中的执行时间。

One reasonable thing to measure is how much the execution time changes if you add additional layer - comparison of the overall execution time of a network with the layer vs network without the layer.衡量一个合理的事情是,如果添加额外的层,执行时间会发生多少变化 - 比较有层的网络与没有层的网络的总体执行时间。 But this will require you to retrain the model.但这需要您重新训练 model。

Another thing that you could measure is how much the execution time changes when you add additional layer to the base of the network (similar to what you are doing but only comparing overall execution time of the first N layers to execution time of N+1 layers).您可以测量的另一件事是,当您将附加层添加到网络的基础时,执行时间会发生多少变化(类似于您正在做的事情,但仅将前 N 层的总体执行时间与 N+1 层的执行时间进行比较)。 This might be slightly useful when you are considering how many base layers you want to keep when doing transfer learning (assuming that the NN architecture allows for that) but even then the accuracy is probably going to be the deciding factor so...当您考虑在进行迁移学习时要保留多少个基础层时,这可能会稍微有用(假设 NN 架构允许这样做),但即便如此,准确性也可能成为决定因素,所以......

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM