簡體   English   中英

在Keras / Tensorflow中計算困惑和內存問題

[英]Calculating Perplexity and Memory Issues in Keras/Tensorflow

我想在每個訓練時期后以困惑度評估我的模型。 我正在將Keras與Tensorflow后端一起使用。 問題在於,每次評估之后,都會使用越來越多的內存,但從未釋放過。 因此,經過幾個時期后,我的系統崩潰了。 如果我不使用keras和tensorflow函數,它將不會出現內存問題。 但這太慢了。 這是代碼:

def compute_perplexity(self, modelName, sentences):
    all_labels, all_predictions = self.predictLabels_for_perplexity_evaluation(self.models[modelName], sentences)
    # add an axis to fit tensor shape
    for i in range(len(all_labels)):
        all_labels[i] = all_labels[i][:,:, np.newaxis]

#calculate perplexity for each sentence length and each datapoint and append to list
perplexity = []
for i in range(10,15): #range(len(all_labels)):
    start = time.time()
    xentropy = K.sparse_categorical_crossentropy(tf.convert_to_tensor(all_labels[i]), tf.convert_to_tensor(all_predictions[i]))
    perplexity.append(K.eval(K.pow(2.0, xentropy)))
    print('time for one set of sentences. ', time.time()- start)
#average for each datapoint
for i in range(len(perplexity)):
    perplexity[i] = np.average(perplexity[i], axis=1)
    perplexity[i] = np.average(perplexity[i])

return np.mean(perplexity)

無需使用TensorFlow評估該指標,您編寫的代碼是每次調用圖時將all_labels數組添加到圖形中,從而說明了您所看到的內存使用情況。

考慮使用numpy實現所有這些計算,或者考慮使用feed_dict (不使用tf.convert_to_tensor )對會話中的新數據進行評估的操作。

暫無
暫無

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

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