keras load_model报错

  统计/机器学习 深度学习 Python 人工神经网络 TensorFlow    浏览次数:7740        分享
0

我用Keras构建了一个神经网络,可以训练,也可以用model.save("model.h5")保存模型,但是当我用model = load_model('model.h5')载入模型时就有如下报错:

Traceback (most recent call last):
  File "C:/programming/pycharm/cnn_attention_lstm/cnn_attention_lstm/train7.py", line 31, in
    main()
  File "C:/programming/pycharm/cnn_attention_lstm/cnn_attention_lstm/train7.py", line 23, in main
    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, data_set_name=data_set_name)
  File "C:\programming\pycharm\cnn_attention_lstm\cnn_attention_lstm\model7again.py", line 113, in fit
    model = self.create_model()
  File "C:\programming\pycharm\cnn_attention_lstm\cnn_attention_lstm\model7again.py", line 66, in create_model
    model = load_model('model.h5')
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 263, in load_model
    load_weights_from_hdf5_group(f['model_weights'], model.layers)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 915, in load_weights_from_hdf5_group
    reshape=reshape)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 554, in preprocess_weights_for_loading
    weights = convert_nested_time_distributed(weights)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 513, in convert_nested_time_distributed
    layer.layer, weights, original_keras_version, original_backend)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 556, in preprocess_weights_for_loading
    weights = convert_nested_model(weights)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 532, in convert_nested_model
    original_backend=original_backend))
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 556, in preprocess_weights_for_loading
    weights = convert_nested_model(weights)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 544, in convert_nested_model
    original_backend=original_backend))
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 673, in preprocess_weights_for_loading
    elif layer_weights_shape != weights[0].shape:
IndexError: list index out of range
Process finished with exit code 1

卡在这里好几天了,萌新求大神点拨,不胜感激

 

chichi   2018-10-31 12:28



   1个回答 
1

不知道你的keras的版本是什么,一些版本的确load_model的时候有bug,

比如这个https://github.com/keras-team/keras/issues/4143 

还有这个https://github.com/keras-team/keras/issues/10608

不同的版本都有类似的问题

有人自己写了一个save和load,题主可以试试https://github.com/keras-team/keras/issues/10608#issuecomment-427817070

SofaSofa数据科学社区DS面试题库 DS面经

abuu   2018-10-31 14:22



  相关讨论

keras和tensorflow有什么关系?

keras sequential model怎么增加一个dropout层?

Keras模型针对同一数据集做fit和evaluation,accuracy值却不相同

keras.models.Sequential()是一种什么模型

keras怎么设置/改动rmsprop的学习率?

keras加载imdb数据时num_words参数是干嘛的?

训练完keras模型后怎么画出loss和val_loss的对比图?

怎么在keras中加载MNSIT数据集?

keras里的predict,predict_classes,predict_proba有什么区别?

keras无法加载imdb文本:Object arrays cannot be loaded when allow_pickle=False

  随便看看

请问在sklearn怎么在已有的模型的基础上训练新的数据?

推荐系统算法里的cold start是什么意思?

训练集中有的特征含有缺失值,一般怎么处理

怎么让DataFrame按照某一列绝对值从小到按排列?

xgboost怎么调参?