如何屏蔽TensorFlow控制台日志?已尝试多种方法未解决
屏蔽TensorFlow 2.9.1启动警告日志(Python 3.10)
我使用Python 3.10 + TensorFlow 2.9.1开发了一款聊天机器人,每次启动运行时都会输出大量TensorFlow警告日志,必须等日志输出完毕才能正常使用。尝试了多种屏蔽方法,但均未奏效。
环境信息
- Python版本:3.10
- TensorFlow版本:2.9.1
项目完整代码
import nltk from nltk.stem.lancaster import LancasterStemmer stemmer = LancasterStemmer() import numpy import tflearn import tensorflow import random import json import pickle import warnings with open("intents.json") as file: data = json.load(file) try: with open("data.pickle", "rb") as f: words, labels, training, output = pickle.load(f) except Exception as e: words = [] labels = [] docs_x = [] docs_y = [] for intent in data["intents"]: for pattern in intent["patterns"]: wrds = nltk.word_tokenize(pattern) words.extend(wrds) docs_x.append(wrds) docs_y.append(intent["tag"]) if intent["tag"] not in labels: labels.append(intent["tag"]) words = [stemmer.stem(w.lower()) for w in words if w != "?"] words = sorted(list(set(words))) labels = sorted(labels) training = [] output = [] out_empty = [0 for _ in range(len(labels))] for x, doc in enumerate(docs_x): bag = [] wrds = [stemmer.stem(w) for w in doc] for w in words: if w in wrds: bag.append(1) else: bag.append(0) output_row = out_empty[:] output_row[labels.index(docs_y[x])] = 1 training.append(bag) output.append(output_row) with open("data.pickle", "wb") as f: pickle.dump((words, labels, training, output), f) training = numpy.array(training) output = numpy.array(output) tensorflow.compat.v1.reset_default_graph() net = tflearn.input_data(shape=[None, len(training[0])]) net = tflearn.fully_connected(net, 8) net = tflearn.fully_connected(net, 8) net = tflearn.fully_connected(net, len(output[0]), activation="softmax") net = tflearn.regression(net) model = tflearn.DNN(net) try: model.load("model.tflearn") except Exception as e: model.fit(training, output, n_epoch=2500, batch_size=8, show_metric=True) model.save("model.tflearn") def bag_of_words(s, words): bag = [0 for _ in range(len(words))] s_words = nltk.word_tokenize(s) s_words = [stemmer.stem(word.lower()) for word in s_words] for se in s_words: for i, w in enumerate(words): if w == se: bag[i] = 1 return numpy.array(bag) def get_output(inp): inp = str(inp) responses = "" results = model.predict([bag_of_words(inp, words)])[0] results_index = numpy.argmax(results) tag = labels[results_index] if results[results_index] > 0.7: for tg in data["intents"]: if tg['tag'] == tag: responses = tg['responses'] responses = str(random.choice(responses)) else: responses = "I didn't get that" return responses if __name__ == "__main__": inp = input(">>> ") get_out = get_output(inp) print(get_out)
控制台输出的警告日志
2022-09-26 00:49:10.472563: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2022-09-26 00:49:10.485966: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. WARNING:tensorflow:From C:\Python 310\lib\site-packages\tensorflow\python\compat\v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version. Instructions for updating: non-resource variables are not supported in the long term curses is not supported on this machine (please install/reinstall curses for an optimal experience) 2.9.1 2022-09-26 00:50:40.291595: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2022-09-26 00:50:40.305928: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. WARNING:tensorflow:From C:\Python 310\lib\site-packages\tensorflow\python\compat\v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version. Instructions for updating: non-resource variables are not supported in the long term curses is not supported on this machine (please install/reinstall curses for an optimal experience) WARNING:tensorflow:From C:\Python 310\lib\site-packages\tflearn\initializations.py:164: calling TruncatedNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor 2022-09-26 00:50:48.663462: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2022-09-26 00:50:48.677293: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found 2022-09-26 00:50:48.692157: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublasLt64_11.dll'; dlerror: cublasLt64_11.dll not found 2022-09-26 00:50:48.707790: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found 2022-09-26 00:50:48.723267: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found 2022-09-26 00:50:48.738537: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found 2022-09-26 00:50:48.754577: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found 2022-09-26 00:50:48.770482: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found 2022-09-26 00:50:48.784207: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2022-09-26 00:50:48.817031: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-09-26 00:50:49.258725: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
已尝试但无效的方法
import logging import warnings import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow tensorflow.get_logger().setLevel('INFO') tensorflow.compat.v1.logging.set_verbosity(tensorflow.compat.v1.logging.ERROR) logging.getLogger('tensorflow').setLevel(logging.FATAL) warnings.filterwarnings(action="ignore")
有效解决方案
问题核心是环境变量设置时机太晚,且未处理tflearn自身的日志输出。需将日志屏蔽代码放在所有TensorFlow相关库导入的最前端,同时覆盖tflearn的日志级别。
修改后的完整代码开头部分如下:
# 先设置环境变量,必须在导入tensorflow/tflearn之前执行 import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 屏蔽TensorFlow C层日志(含GPU库缺失警告) os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' # 关闭oneDNN优化提示日志 # 屏蔽Python全局警告 import warnings warnings.filterwarnings("ignore") # 屏蔽tensorflow和tflearn的日志输出 import logging logging.getLogger('tensorflow').setLevel(logging.FATAL) logging.getLogger('tflearn').setLevel(logging.FATAL) # 再导入TensorFlow相关库并配置日志级别 import tensorflow as tf tf.get_logger().setLevel('ERROR') tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) # 最后导入其他业务库 import nltk from nltk.stem.lancaster import LancasterStemmer import numpy import tflearn import random import json import pickle # 后续原有代码保持不变...
关键说明
TF_CPP_MIN_LOG_LEVEL=3:彻底屏蔽TensorFlow底层C++模块的所有日志TF_ENABLE_ONEDNN_OPTS=0:关闭oneDNN优化相关的提示日志- 日志屏蔽逻辑必须前置,确保在TensorFlow/tflearn初始化前生效
- 新增
logging.getLogger('tflearn').setLevel(logging.FATAL),覆盖tflearn自身的警告输出
内容的提问来源于stack exchange,提问作者user20084690
相关产品推荐
相关产品推荐

