簡體   English   中英

測量 Keras 層執行時間的正確方法

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

我正在嘗試檢查 Keras model 不同層的執行速度(使用 keras 來自 Z2C39BC19B701AC36FDC04D vv 的 keras)。

我從這個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()

這是衡量不同層執行時間的正確方法嗎?

我會說沒有正確的方法來測量不同層的執行時間,因為

  1. 神經網絡作為一個整體工作(整體大於其部分的總和)。 你不能在不破壞系統的情況下從訓練網絡的中間拔出層,因此測量它處理某事的時間並不是特別有用。

  2. 一層的執行時間也取決於前一層。 如果您將先前的層從具有 1 個神經元更改為具有 [插入大量] 神經元,則即使該層本身保持不變,下一層的執行時間也會發生變化。 所以基本上不可能測量一個層在日照中的執行時間。

衡量一個合理的事情是,如果添加額外的層,執行時間會發生多少變化 - 比較有層的網絡與沒有層的網絡的總體執行時間。 但這需要您重新訓練 model。

您可以測量的另一件事是,當您將附加層添加到網絡的基礎時,執行時間會發生多少變化(類似於您正在做的事情,但僅將前 N 層的總體執行時間與 N+1 層的執行時間進行比較)。 當您考慮在進行遷移學習時要保留多少個基礎層時,這可能會稍微有用(假設 NN 架構允許這樣做),但即便如此,准確性也可能成為決定因素,所以......

暫無
暫無

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

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