bert4keras icon indicating copy to clipboard operation
bert4keras copied to clipboard

#版本不同错误不同 AttributeError: module 'keras.utils' has no attribute 'get_custom_objects'

Open chu8129 opened this issue 4 years ago • 8 comments

提问时请尽可能提供如下信息:

基本信息

  • 你使用的操作系统:
4.4.0-31-generic #50~14.04.1-Ubuntu
  • 你使用的Python版本:
python -V
Python 3.6.3
  • 环境
pip freeze|egrep "(tensor|keras)"
bert4keras==0.8.1
keras==2.6.0
tensorboard==2.7.0
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.0
tensorflow==2.6.0
tensorflow-estimator==2.6.0

模型

https://github.com/bojone/P-tuning.git

核心代码

from bert4keras.backend import keras, K

输出信息

Python 3.6.3 (default, Oct  6 2017, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from bert4keras.backend import keras, K
2021-10-21 16:32:12.099799: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-10-21 16:32:12.099848: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data1/dmuser/***/P-tuning/env/lib/python3.6/site-packages/bert4keras/backend.py", line 291, in <module>
    keras.utils.get_custom_objects().update(custom_objects)
AttributeError: module 'keras.utils' has no attribute 'get_custom_objects'
>>> 

### 自我尝试

目前测试,只有2.2.0版本(cuda:10.0)是能和keras匹配的,每个版本异常都不一样; 本来想着服务器是cuda:11.0,不想换cuda,就用新版本的tf,但是和bert4keras有点问题;

现在能用的是:tf 2.2.0和bert4keras,cpu版本(此时gpu的cuda又是11.0但tf2.2.0用的是10.0)


另外一台机器: 4.15.0-143-generic #147-Ubuntu Python 3.6.9 bert4keras==0.8.1 bert4keras-1.14.0/tensorflow-2.0.0/tensorflow 2.1.0/tensorflow==2.3.0 from tensorflow.python.eager.context import get_config

tensorflow==2.6.0 AttributeError: module 'keras.utils' has no attribute 'get_custom_objects'

chu8129 avatar Oct 21 '21 08:10 chu8129

我有同样的问题,

python -V
Python 3.9.9

tensorflow==2.8.0 keras==2.8.0 bert4keras==0.11.1

问题描述:

/ven/lib/python3.9/site-packages/bert4keras/backend.py", line 417, in <module>
    keras.utils.get_custom_objects().update(custom_objects)
AttributeError: module 'keras.utils' has no attribute 'get_custom_objects'

binghuo15955 avatar Apr 16 '22 09:04 binghuo15955

我有同样的问题,

python -V
Python 3.9.9

tensorflow==2.8.0 keras==2.8.0 bert4keras==0.11.1

问题描述:

/ven/lib/python3.9/site-packages/bert4keras/backend.py", line 417, in <module>
    keras.utils.get_custom_objects().update(custom_objects)
AttributeError: module 'keras.utils' has no attribute 'get_custom_objects'

请问您如何解决版本问题,现在线上也是cuda 11

zxyscz avatar Jun 22 '22 09:06 zxyscz

我也碰到了这个问题,使用tensorflow==2.6.3 keras==2.6.0,看到苏神代码说明中的“keras 2.4+可以用,但事实上keras 2.4.x基本上已经完全等价于tf.keras了,因此如果你要用keras 2.4+,倒不如直接用tf.keras”,受到启发,我把出现错误的from bert4keras.backend import k这句改成了import tensorflow.keras.backend as K就跑通了(本身这段在bert4keras.backend中调用的是import keras.backend as K),试用的是examples中的task_relation_extraction.py。感谢苏神的代码和指导。

ap00314 avatar Jun 29 '22 11:06 ap00314

我在basic_language_model_gpt2_ml.py里面也遇到了一样的报错

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14936\15076559.py in <module>
      1 import numpy as np
----> 2 from bert4keras.models import build_transformer_model
      3 from bert4keras.tokenizers import Tokenizer
      4 from bert4keras.snippets import AutoRegressiveDecoder
      5 from bert4keras.snippets import uniout

c:\users\fchen\appdata\local\programs\python\python37\lib\site-packages\bert4keras\models.py in <module>
      3 
      4 import numpy as np
----> 5 from bert4keras.backend import get_available_gpus
      6 from bert4keras.layers import *
      7 from bert4keras.snippets import insert_arguments

c:\users\fchen\appdata\local\programs\python\python37\lib\site-packages\bert4keras\backend.py in <module>
    511 }
    512 
--> 513 keras.utils.get_custom_objects().update(custom_objects)

AttributeError: module 'keras.utils' has no attribute 'get_custom_objects'

CUDA11和tf==2.8,因为我这边客观上对tf版本没有限制,所以tf升到2.10.0再import就不报错了

nameless0704 avatar Nov 11 '22 01:11 nameless0704

我也碰到了这个问题,使用tensorflow==2.6.3 keras==2.6.0,看到苏神代码说明中的“keras 2.4+可以用,但事实上keras 2.4.x基本上已经完全等价于tf.keras了,因此如果你要用keras 2.4+,倒不如直接用tf.keras”,受到启发,我把出现错误的from bert4keras.backend import k这句改成了import tensorflow.keras.backend as K就跑通了(本身这段在bert4keras.backend中调用的是import keras.backend as K),试用的是examples中的task_relation_extraction.py。感谢苏神的代码和指导。

你好,我也是跑task_relation_extraction.py,但是遇到了下面的问题,您知道 是什么问题吗,谢谢 image

deutschoo avatar Mar 07 '24 07:03 deutschoo