簡體   English   中英

在pyspark中使用帶有輟學的Keras序列化模型

[英]Using Keras serialized model with dropout in pyspark

我有幾個使用Keras構建的神經網絡,到目前為止,我大多數時候都在Jupyter中使用它。 我經常使用joblib從scikit-learn中保存模型,並使用json + hdf5從Keras中保存模型,並在其他筆記本中使用它們而不會出現問題。

我制作了一個Python Spark應用程序,可以在集群模式下使用那些序列化的模型。 joblib模型運行正常,但是,我遇到了Keras的問題。

這是用於筆記本和pyspark的模型:

def build_gru_model():
    model = Sequential()
    model.add(Embedding(max_nb_words, 128, input_length=max_sequence_length, dropout=0.2))
    model.add(GRU(128, dropout_W=0.2, dropout_U=0.2))
    model.add(Dense(2, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

兩者都以相同的方式調用:

preds = model.predict_proba(data, verbose=0)

但是,僅在Spark中,我得到了錯誤:

MissingInputError: ("An input of the graph, used to compute DimShuffle{x,x,x,x}(keras_learning_phase), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.", keras_learning_phase)

我已經完成了強制搜索,發現: https : //github.com/fchollet/keras/issues/2430指向https://keras.io/getting-started/faq/

如果我確實從模型中刪除了輟學,那就行得通。 但是,我無法理解如何實施一些使我在培訓階段保持輟學的方法,如常見問題解答中所述。

根據模型代碼,如何做到這一點?

您可以嘗試放置(在進行預測之前)

import keras.backend as K
K.set_learning_phase(0)

它應該將您的學習階段設置為0(測試時間)

暫無
暫無

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

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