第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Keras 不一致的預(yù)測(cè)時(shí)間

Keras 不一致的預(yù)測(cè)時(shí)間

蝴蝶刀刀 2022-08-02 18:36:54
我試圖估計(jì)我的keras模型的預(yù)測(cè)時(shí)間,并意識(shí)到一些奇怪的東西。除了通常相當(dāng)快之外,每隔一段時(shí)間,模型需要相當(dāng)長(zhǎng)的時(shí)間才能得出預(yù)測(cè)。不僅如此,這些時(shí)間也隨著模型運(yùn)行時(shí)間的延長(zhǎng)而增加。我添加了一個(gè)最小的工作示例來重現(xiàn)錯(cuò)誤。import timeimport numpy as npfrom sklearn.datasets import make_classificationfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense, Flatten# Make a dummy classification problemX, y = make_classification()# Make a dummy modelmodel = Sequential()model.add(Dense(10, activation='relu',name='input',input_shape=(X.shape[1],)))model.add(Dense(2, activation='softmax',name='predictions'))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])model.fit(X, y, verbose=0, batch_size=20, epochs=100)for i in range(1000):    # Pick a random sample    sample = np.expand_dims(X[np.random.randint(99), :], axis=0)    # Record the prediction time 10x and then take the average    start = time.time()    for j in range(10):        y_pred = model.predict_classes(sample)    end = time.time()    print('%d, %0.7f' % (i, (end-start)/10))時(shí)間不取決于樣本(它是隨機(jī)挑選的)。如果重復(fù)測(cè)試,則預(yù)測(cè)需要較長(zhǎng)時(shí)間的 for 循環(huán)中的索引將(幾乎)再次相同。我正在使用:tensorflow 2.0.0 python 3.7.4對(duì)于我的應(yīng)用程序,我需要保證在一定時(shí)間內(nèi)執(zhí)行。然而,考慮到這種行為,這是不可能的。哪里出錯(cuò)了?是 Keras 中的 bug 還是 tensorflow 后端中的 bug?
查看完整描述

2 回答

?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

在我遇到的幾個(gè)實(shí)例中,TF2通常表現(xiàn)出糟糕且類似錯(cuò)誤的內(nèi)存管理 - 此處此處的簡(jiǎn)要說明。特別是對(duì)于預(yù)測(cè),最有效的喂養(yǎng)方法是直接通過 - 請(qǐng)參閱此處及其鏈接的討論。model(x)

簡(jiǎn)而言之:通過其方法(它從base_layer繼承)起作用。Layer),而 、 等通過 _select_training_loop();每個(gè)都利用不同的數(shù)據(jù)預(yù)處理和后處理方法,適合不同的用例,并且在2.1中專門設(shè)計(jì)用于產(chǎn)生最快的小模型/小批量(可能是任何尺寸)性能(并且在2.0中仍然最快)。model(x)__call__predict()predict_classes()model(x)

引用鏈接討論中一位TensorFlow開發(fā)者的話:

您可以使用模型調(diào)用來預(yù)測(cè)輸出,而不是模型預(yù)測(cè),也就是說,調(diào)用將使這更快,因?yàn)闆]有“轉(zhuǎn)換為數(shù)據(jù)集”部分,并且它還直接調(diào)用緩存。model(x)tf.function

注意:這在2.1中應(yīng)該不是一個(gè)問題,尤其是2.2 - 但無論如何都要測(cè)試每種方法。我也意識(shí)到這并不能直接回答你關(guān)于時(shí)間峰值的問題;我懷疑它與 Eager 緩存機(jī)制有關(guān),但最可靠的確定方法是通過 TF Profiler,這在 2.1 中被破壞了


更新:關(guān)于增加峰值,可能的GPU限制;你已經(jīng)做了大約1000次迭代,嘗試10,000次 - 最終,增加應(yīng)該停止。正如您在評(píng)論中指出的那樣,這在 ;這是有道理的,因?yàn)樯婕暗腉PU步驟更少(“轉(zhuǎn)換為數(shù)據(jù)集”)。model(x)

Update2:如果您遇到此問題,您可以在此處對(duì)開發(fā)人員進(jìn)行錯(cuò)誤處理;主要是我在那里唱歌


查看完整回答
反對(duì) 回復(fù) 2022-08-02
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

雖然我無法解釋執(zhí)行時(shí)間的不一致,但我可以建議您嘗試將模型轉(zhuǎn)換為TensorFlow Lite,以加快對(duì)單個(gè)數(shù)據(jù)記錄或小批量的預(yù)測(cè)。

我在這個(gè)模型上運(yùn)行了一個(gè)基準(zhǔn)測(cè)試:

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(384, activation='elu', input_shape=(256,)),
    tf.keras.layers.Dense(384, activation='elu'),
    tf.keras.layers.Dense(256, activation='elu'),
    tf.keras.layers.Dense(128, activation='elu'),
    tf.keras.layers.Dense(32, activation='tanh')
])

單個(gè)記錄的預(yù)測(cè)時(shí)間為:

  1. model.predict(input): 18毫秒

  2. model(input): 1.3毫秒

  3. 轉(zhuǎn)換為TensorFlow Lite的模型:43us

轉(zhuǎn)換模型的時(shí)間為 2 秒。

下面的類演示如何轉(zhuǎn)換和使用模型,并提供類似于 Keras 模型的方法。請(qǐng)注意,它需要修改以用于不僅具有單個(gè)1-D輸入和單個(gè)1-D輸出的模型。predict

class LiteModel:


    @classmethod

    def from_file(cls, model_path):

        return LiteModel(tf.lite.Interpreter(model_path=model_path))


    @classmethod

    def from_keras_model(cls, kmodel):

        converter = tf.lite.TFLiteConverter.from_keras_model(kmodel)

        tflite_model = converter.convert()

        return LiteModel(tf.lite.Interpreter(model_content=tflite_model))


    def __init__(self, interpreter):

        self.interpreter = interpreter

        self.interpreter.allocate_tensors()

        input_det = self.interpreter.get_input_details()[0]

        output_det = self.interpreter.get_output_details()[0]

        self.input_index = input_det["index"]

        self.output_index = output_det["index"]

        self.input_shape = input_det["shape"]

        self.output_shape = output_det["shape"]

        self.input_dtype = input_det["dtype"]

        self.output_dtype = output_det["dtype"]


    def predict(self, inp):

        inp = inp.astype(self.input_dtype)

        count = inp.shape[0]

        out = np.zeros((count, self.output_shape[1]), dtype=self.output_dtype)

        for i in range(count):

            self.interpreter.set_tensor(self.input_index, inp[i:i+1])

            self.interpreter.invoke()

            out[i] = self.interpreter.get_tensor(self.output_index)[0]

        return out


    def predict_single(self, inp):

        """ Like predict(), but only for a single record. The input data can be a Python list. """

        inp = np.array([inp], dtype=self.input_dtype)

        self.interpreter.set_tensor(self.input_index, inp)

        self.interpreter.invoke()

        out = self.interpreter.get_tensor(self.output_index)

        return out[0]

完整的基準(zhǔn)代碼和繪圖可以在這里找到:https://medium.com/@micwurm/using-tensorflow-lite-to-speed-up-predictions-a3954886eb98


查看完整回答
反對(duì) 回復(fù) 2022-08-02
  • 2 回答
  • 0 關(guān)注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)