簡體   English   中英

在使用tf.keras的tensorflow渴望執行時警告`試圖取消分配nullptr`

[英]Warning `tried to deallocate nullptr` when using tensorflow eager execution with tf.keras

根據tensorflow團隊的建議,我已經習慣了使用tf.keras進行tensorflow的熱切執行。 但是,每當訓練模型時,我都會收到一條警告(編輯:實際上,我多次重復收到此警告,每次訓練步驟均多次收到此警告,從而淹沒了我的標准輸出):

E tensorflow/core/common_runtime/bfc_allocator.cc:373] tried to deallocate nullptr

該警告似乎並沒有影響培訓的質量,但是我想知道這意味着什么以及是否有可能消除它。

我將conda虛擬環境與在CPU上運行的python 3.7和tensorflow 1.12結合使用。 (編輯:使用python 3.6進行的測試給出了相同的結果。)下面是再現警告的最小代碼。 有趣的是,可以在tf.enable_eager_execution()行中添加注釋,並看到警告消失。

import numpy as np
import tensorflow as tf

tf.enable_eager_execution()
N_EPOCHS = 50
N_TRN = 10000
N_VLD = 1000

# the label is positive if the input is a number larger than 0.5
# a little noise is added, just for fun
x_trn = np.random.random(N_TRN)
x_vld = np.random.random(N_VLD)
y_trn = ((x_trn + np.random.random(N_TRN) * 0.02) > 0.5).astype(float)
y_vld = ((x_vld + np.random.random(N_VLD) * 0.02) > 0.5).astype(float)

# a simple logistic regression
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, input_dim=1))
model.add(tf.keras.layers.Activation('sigmoid'))

model.compile(
    optimizer=tf.train.AdamOptimizer(),
    # optimizer=tf.keras.optimizers.Adam(),  # doesn't work at all with tf eager execution
    loss='binary_crossentropy',
    metrics=['accuracy']
)

# Train model on dataset
model.fit(
    x_trn, y_trn,
    epochs=N_EPOCHS,
    validation_data=(x_vld, y_vld),
)
model.summary()

快速解決方案:

  • 當我執行優化以在合成數據集上達到相同的最終驗證精度時,當我在TF 1.11中運行相同的腳本時,它沒有出現。

    要么

  • 使用本機os模塊(改編自https://stackoverflow.com/a/38645250/2374160 )抑制錯誤/警告。 通過將Tensorflow日志記錄環境變量設置為不顯示任何錯誤消息。

      import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf 

更多信息:

  • 以正確的方式解決此錯誤可能需要熟悉MKL庫調用及其在用C語言編寫的Tensorflow上的接口(這超出了我目前的TF專業知識)

  • 就我而言,每當調用優化程序的apply_gradients()方法時, 都會發生此內存釋放錯誤。 在您的腳本中,將模型擬合到訓練數據時會調用它。

  • 從這里引發此錯誤: tensorflow / core / common_runtime / mkl_cpu_allocator.h

我希望這可以為您提供方便的臨時解決方案。

暫無
暫無

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

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