keras load_model报错

  统计/机器学习 深度学习 Python 人工神经网络 TensorFlow    浏览次数:1381        分享
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加载imdb数据时num_words参数是干嘛的?   1回答

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

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

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

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

keras模型合并请教   2回答

keras里layer默认的激活函数是什么?   1回答

keras中怎么增加Leaky relu的激活层   1回答

Tensorflow多层神经网络训练最后输出的值都一样的,找不到是什么原因?   3回答

训练时的数据集问题   1回答

tensorflow一定要用gpu吗?   2回答

一维数据怎么输入卷积网络。   2回答



回答问题时需要注意什么?

我们谢绝在回答前讲“生动”的故事。

我们谢绝“这么简单,你自己想”、“书上有的,你认真看”这类的回答;如果你认为对方的提问方式或者内容不妥,你可以直接忽略该问题,不用进行任何作答,甚至可以对该问题投反对票。

我们谢绝答非所问。

我们谢绝自己不会、硬要回答。

我们感激每一个用户在编写答案时的努力与付出!