我如何用tensorflow警告修复此错误?



程序启动后,在控制台中输出:

WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.base has been moved to tensorflow.python.trackable.base. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.checkpoint_management has been moved to tensorflow.python.checkpoint.checkpoint_management. The old module will be deleted in version 2.9.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.resource has been moved to tensorflow.python.trackable.resource. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.util has been moved to tensorflow.python.checkpoint.checkpoint. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.base_delegate has been moved to tensorflow.python.trackable.base_delegate. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.graph_view has been moved to tensorflow.python.checkpoint.graph_view. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.python_state has been moved to tensorflow.python.trackable.python_state. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.saving.functional_saver has been moved to tensorflow.python.checkpoint.functional_saver. The old module will be deleted in version 2.11.
WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.saving.checkpoint_options has been moved to tensorflow.python.checkpoint.checkpoint_options. The old module will be deleted in version 2.11.

如何解决这个问题?

更新有

要减少tensorflow给出的警告数量,您需要做的就是定义您希望在多大程度上避免更改操作系统。这里的环境变量是一个样本。我将确保不打印任何内容

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

您可以在导入tensorflow之前尝试设置日志级别吗?

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

日志记录发生在这个函数中:

def getter(name):
if getter not in _PRINTED_WARNING and _PRINT_DEPRECATION_WARNINGS:
_PRINTED_WARNING[getter] = True
logging.warning(
'Please fix your imports. Module %s has been moved to %s. The old '
'module will be deleted in version %s.', deprecated_name,
new_module.__name__, deletion_version)
return getattr(new_module, name)
return getter

在不同的模块中,你会发现:

deprecation.deprecate_moved_module(
__name__, <MODULE_NAME>, "<VERSION_NUMBER>")
提供的代码片段中的一个示例是tensorflow.python.training.saving.checkpoint_options:
from tensorflow.python.checkpoint import checkpoint_options
from tensorflow.python.util import deprecation
__getattr__ = deprecation.deprecate_moved_module(
__name__, checkpoint_options, "2.11")

表示从该模块导入在2.11中已被弃用(尽管在技术上它在2.11之前仍然可以工作)。

根据提供的代码片段,似乎没有直接导入这些模块,但可能是外部依赖项或其他模块。如果无法修复导入,则在同一个deprecations模块中,有一个名为silence的上下文管理器,它将上下文块中的全局变量_PRINT_DEPRECATION_WARNINGS更改为False,从而防止出现警告日志。用法类似于:


from tensorflow.python.util import deprecation
with deprecation.silence():
<INSERT_CODE_HERE>

或者,如果您不想在上下文管理器中放置大的代码块,您可以直接在模块的顶部设置变量_PRINT_DEPRECATION_WARNINGS:

from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
<INSERT_CODE_HERE>

注意,将环境变量TF_CPP_MIN_LOG_LEVEL设置为1-3以减少日志记录不适用于弃用警告

相关内容

  • 没有找到相关文章

最新更新