Python问题:AttributeError: ‘str‘ object has no attribute ‘decode‘

具体报错情况:

AttributeError                            Traceback (most recent call last)
<ipython-input-29-7b65833fd81b> in <module>
      8 from keras.utils.data_utils import get_file
      9 from tensorflow.keras.preprocessing.sequence import pad_sequences
---> 10 from shakespeare_utils import *
     11 # import sys
     12 # import io

D:\jupyter\5.1\shakespeare_utils.py in <module>
    131 x, y = vectorization(X, Y, n_x = len(chars), char_indices = char_indices)
    132 print("Loading model...")
--> 133 model = load_model('models/model_shakespeare_kiank_350_epoch.h5')
    134 
    135 

D:\Anaconda3\lib\site-packages\keras\engine\saving.py in load_wrapper(*args, **kwargs)
    456                 os.remove(tmp_filepath)
    457             return res
--> 458         return load_function(*args, **kwargs)
    459 
    460     return load_wrapper

D:\Anaconda3\lib\site-packages\keras\engine\saving.py in load_model(filepath, custom_objects, compile)
    548     if H5Dict.is_supported_type(filepath):
    549         with H5Dict(filepath, mode='r') as h5dict:
--> 550             model = _deserialize_model(h5dict, custom_objects, compile)
    551     elif hasattr(filepath, 'write') and callable(filepath.write):
    552         def load_function(h5file):

D:\Anaconda3\lib\site-packages\keras\engine\saving.py in _deserialize_model(h5dict, custom_objects, compile)
    245 
    246     if 'keras_version' in model_weights_group:
--> 247         original_keras_version = model_weights_group['keras_version'].decode('utf8')
    248     else:
    249         original_keras_version = '1'

AttributeError: 'str' object has no attribute 'decode'

 keras与h5py版本不兼容。
查看h5py的版本,未安装,自动安装后

pip install h5py==2.10.0

在2.10版本中,字符串的存储是经过编码的,而在3.0.0的版本中,则不会检查是否经过编码。所以才会出现最初的AttributeError: ‘str’ object has no attribute ‘decode’。

发现h5dict与h5py不兼容

卸载h5dict

 pip uninstall h5dict

安装

pip install h5dict==0.2.0 

运行一下 继续报错了

AttributeError: 'bytes' object has no attribute 'encode'

 但报错文档未出现encode(“utf-8”)

重启Jupyter notebook,重新打开即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值