[英]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.