使用tensorflow C api禁用Tensorflow日志

问题描述 投票:0回答:2

我在 C 中使用 TensorFlow 模型,我需要一个干净的控制台,只有一个输出用于其他程序。但现在,当我运行模型时,它会打印如下内容:

Successfully opened dynamic library cublas64_100.dll, Successfully opened dynamic library cudnn64_7.dll tensorflow/stream_executor/cuda/redzone_allocator.cc:312] Internal: Invoking ptxas not supported on Windows Relying on driver to perform ptx compilation. This message will be only logged once.

有没有办法去掉调试信息?例如,在Python中,可以通过使用this线程中的信息来解决:

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

但除了这个之外我似乎找不到任何其他 C 资源:

putenv("TF_CPP_MIN_LOG_LEVEL=3");

但是由于某种原因这不起作用。 (所以我的想法是在 TF 源代码或任何地方全局更改此变量的默认值(TF_CPP_MIN_LOG_LEVEL),因此每次我使用 Tensorflow 时它都设置为 3,但我不太确定该怎么做那)

c tensorflow
2个回答
0
投票

您可以通过设置一个名为 TF_CPP_MIN_LOG_LEVEL 的新环境变量来使用 c api 禁用 TensorFlow 日志。

char* new_env = "TF_CPP_MIN_LOG_LEVEL=3";

通过创建此环境变量,tensorflow 日志将被禁用。谢谢你。


0
投票

如果你包括

    char env[] = "TF_CPP_MIN_LOG_LEVEL=3";
    putenv(env);

在 main() 的开头,它应该可以工作(在 C 和 C++ 中)。它阻止打印到 stderr 的张量流的所有日志消息(3 是最大严重级别)。

© www.soinside.com 2019 - 2024. All rights reserved.