运行非局部循环网络GitHub代码遇KeyError: 'INFO',求调试方法
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'intf.logging.__dict__(common in TF 2.x wheretf.loggingis 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 intf.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 constanttf.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=10for INFO,verbosity=20for 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

