[英]Warning `tried to deallocate nullptr` when using tensorflow eager execution with tf.keras
As per the tensorflow team suggestion, I'm getting used to tensorflow's eager execution with tf.keras. 根据tensorflow团队的建议,我已经习惯了使用tf.keras进行tensorflow的热切执行。 However, whenever I train a model, I receive a warning (EDIT: actually, I receive this warning repeated many times, more than once per training step, flooding my standard output):
但是,每当训练模型时,我都会收到一条警告(编辑:实际上,我多次重复收到此警告,每次训练步骤均多次收到此警告,从而淹没了我的标准输出):
E tensorflow/core/common_runtime/bfc_allocator.cc:373] tried to deallocate nullptr
The warning doesn't seem to affect the quality of the training but I wonder what it means and if it is possible to get rid of it. 该警告似乎并没有影响培训的质量,但是我想知道这意味着什么以及是否有可能消除它。
I use a conda virtual environment with python 3.7 and tensorflow 1.12 running on a CPU. 我将conda虚拟环境与在CPU上运行的python 3.7和tensorflow 1.12结合使用。 (EDIT: a test with python 3.6 gives the same results.) A minimal code that reproduces the warnings follows.
(编辑:使用python 3.6进行的测试给出了相同的结果。)下面是再现警告的最小代码。 Interestingly, it is possible to comment the line tf.enable_eager_execution() and see that the warnings disappear.
有趣的是,可以在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()
Quick solutions: 快速解决方案:
It did not appear when I ran the same script in TF 1.11 while the optimization was performed to reach the same final validation accuracy on a synthetic dataset. 当我执行优化以在合成数据集上达到相同的最终验证精度时,当我在TF 1.11中运行相同的脚本时,它没有出现。
OR 要么
Suppress the errors/warning using the native os module (adapted from https://stackoverflow.com/a/38645250/2374160 ). 使用本机os模块(改编自https://stackoverflow.com/a/38645250/2374160 )抑制错误/警告。 ie;
即 by setting the Tensorflow logging environment variable to not show any error messages.
通过将Tensorflow日志记录环境变量设置为不显示任何错误消息。
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf
More info: 更多信息:
Solving this error in the correct way may require familiarity with MKL library calls and its interfacing on Tensorflow which is written in C (this is beyond my current TF expertise) 以正确的方式解决此错误可能需要熟悉MKL库调用及其在用C语言编写的Tensorflow上的接口(这超出了我目前的TF专业知识)
In my case, this memory deallocation error occurred whenever the apply_gradients() method of an optimizer was called. 就我而言,每当调用优化程序的apply_gradients()方法时, 都会发生此内存释放错误。 In your script, it is called when the model is being fitted to the training data.
在您的脚本中,将模型拟合到训练数据时会调用它。
This error is raised from here: tensorflow/core/common_runtime/mkl_cpu_allocator.h 从这里引发此错误: tensorflow / core / common_runtime / mkl_cpu_allocator.h
I hope this helps as a temporary solution for convenience. 我希望这可以为您提供方便的临时解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.