代码之家  ›  专栏  ›  技术社区  ›  ddd

如何在python中抑制keras日志

  •  0
  • ddd  · 技术社区  · 8 年前

    我正在编写一个python应用程序,它运行用于分类的TensorFlow模型。图书馆 Keras 是为了简单。以下是我的日志配置:

    logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
    handler = RotatingFileHandler(LOG_DIR + '/' + LOG_FILE_NAME, maxBytes=LOG_FILE_MAX_BYTES,backupCount=LOG_FILE_BACKUP_COUNT)
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    handler.setFormatter(formatter)
    logger = logging.getLogger('')
    logger.addHandler(handler)
    logging.getLogger('boto').setLevel(logging.WARNING)
    logging.getLogger('keras').setLevel(logging.CRITICAL)
    logging.getLogger('botocore').setLevel(logging.CRITICAL)
    

    尽管我将keras的日志记录级别设置为 critical 它仍然会在开始时打印出某种警告:

    UserWarning: Update your `InputLayer` call to the Keras 2 API: `InputLayer(batch_input_shape=[None, 64,..., sparse=False, name="input_1", dtype="float32")`
      return cls(**config)
    UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(trainable=True, name="convolution2d_1", activity_regularizer=None, activation="relu", kernel_size=(3, 3), filters=64, strides=[1, 1], padding="same", data_format="channels_last", kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
      return cls(**config)
    UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(strides=[2, 2], trainable=True, name="maxpooling2d_1", pool_size=[2, 2], padding="valid", data_format="channels_last")`
      return cls(**config)
    

    为什么这个输出没有被记录到日志文件中?我是否需要为 keras 模块化并指定与应用程序其余部分相同的日志文件? CRITICAL 高于 Warning . 为什么它仍然输出某种类型的警告?

    1 回复  |  直到 8 年前
        1
  •  1
  •   zimmerrol    8 年前

    你只需关掉所有的 python 警告,通过使用

    python -W ignore script.py
    

    或使用

    import warnings
    warnings.filterwarnings("ignore")
    

    根据 this 所以POST。你可以在官方网站上找到更多关于第二种方法的信息 蟒蛇 documentation .

    第三种方法是使用上述模块并使用“catch”警告上下文管理器

    def fxn():
        warnings.warn("deprecated", DeprecationWarning)
    
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        # the warning will be ignored
        fxn()