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面经 问答 实战

abuu   2018-10-31 14:22



  相关主题

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

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

tensorflow一定要用gpu吗?   2回答

tf.placeholder(tf.float32, shape=(None, 1024))中的None是什么意思   1回答

tensorflow里出现的strides是什么意思   1回答

tensorflow 训练的时候输出nan   1回答

tf.placeholder和tf.variable什么区别?   1回答

怎么print或者查看tf.tensor中的数值   1回答

tf.reduce_sum是什么函数   1回答

如何通过docker安装TensorFlow   1回答

用tensorflow实现强化学习的dql算法报错:tensorflow.python.framework.errors_impl.InternalError: Could not find valid device for node. Node: {{node OneHot}} = OneHot[T=DT_FLOAT, TI=DT_FLOAT, axis=-1](dummy_input, dummy_input, dummy_input, dummy_input)   0回答

pycharm+anaconda的安装问题   1回答



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

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

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

我们谢绝答非所问。

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

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