You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行非局部循环网络GitHub代码遇KeyError: 'INFO',求调试方法

Fixing KeyError: 'INFO' in TensorFlow Logging for Non-Local Loop Network Code

Let's break down this error and work through debugging and fixes step by step.

Understanding the Error

The KeyError: 'INFO' happens because your code is trying to use the string 'INFO' as a key to access tf.logging.__dict__, but this approach has one of two issues:

  • Either your TensorFlow version no longer exposes log level names like 'INFO' in tf.logging.__dict__ (common in TF 2.x where tf.logging is deprecated),
  • Or the logic is flawed to begin with—TensorFlow's log levels (like INFO, DEBUG) are numeric constants, not string keys meant to index the module's dictionary directly.

Debugging Steps

First, let's gather more context to pinpoint the exact issue:

  • Check TensorFlow logging module status
    Add this line right before the error-causing code to see what attributes are available in tf.logging:

    print("Available attributes in tf.logging:", dir(tf.logging))
    

    If 'INFO' doesn't show up in the output, you're likely using TF 2.x and need to use the compatibility version of logging.

  • Inspect hparams.verbosity
    Verify the type and value of the verbosity parameter with this print statement:

    print(f"hparams.verbosity: type={type(hparams.verbosity)}, value={hparams.verbosity}")
    

    You’ll almost certainly find it’s a string ('INFO') instead of the numeric constant tf.logging.INFO (which equals 10).

  • List all keys in tf.logging.__dict__
    Confirm exactly what keys exist in the module's dictionary:

    print("Keys in tf.logging.__dict__:", list(tf.logging.__dict__.keys()))
    

    If 'INFO' is missing here, that’s the root cause of the KeyError.

Fix Solutions

Here are several actionable fixes depending on your setup:

1. Map String Verbosity to TensorFlow Numeric Constants

Create an explicit mapping to avoid direct dictionary indexing:

# Define a mapping of string names to TF log level constants
verbosity_mapping = {
    'DEBUG': tf.logging.DEBUG,
    'INFO': tf.logging.INFO,
    'WARN': tf.logging.WARN,
    'ERROR': tf.logging.ERROR,
    'FATAL': tf.logging.FATAL
}
# Replace the error line with this
verbosity_level = verbosity_mapping[hparams.verbosity] / 10

For TF 2.x, replace tf.logging with tf.compat.v1.logging to access the deprecated logging module.

2. Use Numeric Values for Verbosity Configuration

If you control the hparams setup, configure verbosity with numeric values instead of strings:

  • Set verbosity=10 for INFO, verbosity=20 for WARN, etc.
  • Then your code can simplify to:
    verbosity_level = hparams.verbosity / 10
    

3. Switch to Python's Standard logging Module

For a more stable, TF-version-agnostic solution, use Python's built-in logging:

import logging
# Map string names to standard logging levels
verbosity_mapping = {
    'DEBUG': logging.DEBUG,
    'INFO': logging.INFO,
    'WARNING': logging.WARNING,
    'ERROR': logging.ERROR,
    'CRITICAL': logging.CRITICAL
}
verbosity_level = verbosity_mapping[hparams.verbosity] / 10

4. Compatibility Mode for TF 2.x

If you must retain TF's logging for legacy code, enable v1 compatibility:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# Now use tf.logging as you would in TF 1.x
verbosity_level = tf.logging.__dict__[hparams.verbosity] / 10

Note: This is a temporary fix—long-term, migrating to standard logging is better.


内容的提问来源于stack exchange,提问作者LathaH Narayan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:20:25