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

具体报错情况:

AttributeError                            Traceback (most recent call last)
 in 
      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 
    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,重新打开即可

你可能感兴趣的:(Python,python,tensorflow,开发语言)