Keras干扰python日志记录 [英] Keras interfering with python logging

查看:51
本文介绍了Keras干扰python日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录一些在经过训练的keras模型中加载的python代码.出于某些原因,如果导入了keras load_model ,则(python)日志记录将不起作用.但是,如果我不导入keras,则(python)日志记录可以很好地工作.

I want to log some python code which loads in a trained keras model. For some reason, (python) logging doesn't not work if the keras load_model is imported. But (python) logging works fine if I don't import keras.

这有效:

import logging

LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename="logs/my_logs.log",
                    level=logging.DEBUG,
                    format=LOG_FORMAT)
logger = logging.getLogger()

def do_stuff():
    logging.info("About to do stuff")
    ... stuff gets done here...

这不起作用

import logging
from keras.models import load_model

my_model = load_model("fetch_preprocess/lstm_model.h5")

LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename="logs/my_logs.log",
                    level=logging.DEBUG,
                    format=LOG_FORMAT)
logger = logging.getLogger()

def do_stuff():
    logging.info("About to do stuff")
    pred = my_model.predict(data)
    ... stuff gets done here...

不起作用"是指 logging 模块不起作用

By "doesn't work", I mean the logging module doesn't

  • 创建一个新的日志文件
  • 随时调用 logging 写入该文件

但不会抛出任何错误.所以我觉得很奇怪.

but no error's are ever thrown. So I find this strange.

我相信Keras/张量流会干扰日志记录(因为我对代码所做的唯一更改是排除了Keras之后再进行日志记录就可以了)

I believe Keras / tensorflow is interfering with the logging (as the only change I make to the code is to exclude Keras and then logging works fine).

是否有某种方法可以抑制keras在后台执行的操作,所以我可以使用python日志记录?

Is there some way to suppress whatever keras is doing in the background so I can use the python logging?

推荐答案

此问题是由于软件包 0.7.0 abseil 和如 0.8.0 版本.="nofollow noreferrer"> Github链接.

This issue is because of the bug in the version 0.7.0 of the package, abseil and it has been fixed in its 0.8.0 version, as mentioned in this Github link.

此问题的解决方案是将软件包 abseil 的版本升级到大于或等于0.8.

The solution to this problem is to upgrade the version of the package abseil to greater than or equal to 0.8.

示例代码

  • 创建一个新的日志文件,并
  • 在调用日志记录的任何时间写入该文件

如下所示:

`

!pip install --upgrade absl-py

import logging
from keras.models import load_model

my_model = load_model("fetch_preprocess/lstm_model.h5")

LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename="logs/my_logs.log",
                    level=logging.DEBUG,
                    format=LOG_FORMAT)
logger = logging.getLogger()

def do_stuff():
    logging.info("About to do stuff")
    pred = my_model.predict(data)
    ... stuff gets done here...

do_stuff()

`希望这可以帮助.学习愉快!

` Hope this helps. Happy Learning!

这篇关于Keras干扰python日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆