python - 值错误 : axes don't match array when loading previously saved model

标签 python tensorflow keras

系统信息

  • 我是否编写了自定义代码(而不是使用示例目录):是
  • 操作系统平台和发行版(例如,Linux Ubuntu 16.04):Linux Ubuntu 16.04
  • TensorFlow 后端(是/否):是
  • TensorFlow 版本:1.14.0
  • Keras 版本:2.2.5
  • Python 版本:3.6.8
  • CUDA/cuDNN 版本:不适用
  • GPU 型号和内存:不适用

  • 描述当前行为:
  • 使用 model = load_model(file.h5)
  • 加载模型时出错
    ValueError: axes don't match array
    

    描述预期行为
  • 使用 model.save(file.h5)
  • 保存后模型不再加载

    这是我想要做的:
  • 我有 15 个模型作为单个 merged_model,它们基本上只是具有 15 个输入和 15 个输出的分类模型。
  • 我试图将这 15 个模型的输入组合成一个输入模型。所以我不必提供 15 个输入!
  • 这是我的工作方式:(工作没有任何问题)
  • >> model_single_input = layers.Input((15,), dtype='int32', name='single.input')
    >> model_multiple_inputs = layers.Lambda(lambda x: [x] * 15, name='single.input.multiplier')(model_single_input)
    >> single_input_model = Model(inputs=model_single_input, outputs=model_multiple_inputs)
    >> single_input_model.input, single_input_model.output
    
    (<tf.Tensor 'single.input:0' shape=(?, 15) dtype=int32>,
     [<tf.Tensor 'single.input.multiplier/Identity:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_1:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_2:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_3:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_4:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_5:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_6:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_7:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_8:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_9:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_10:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_11:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_12:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_13:0' shape=(?, 15) dtype=int32>,
      <tf.Tensor 'single.input.multiplier/Identity_14:0' shape=(?, 15) dtype=int32>])
    
  • 现在我将单输入模型与 15 个模型结合起来。 (这没有任何问题)
  • >> single_input_merged_output_model = Model(inputs  = single_input_model.input, outputs = merged_model(single_input_model.output))
    >> encoded_data = np.array([
    [12073, 14512, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [336, 0, 744, 481, 13043, 118, 2563, 0, 0, 0, 0, 0, 0, 0, 0]
    ])
    >> predictions = single_input_merged_output_model.predict(encoded_data)
    >> predictions
    
    [array([[ 0.        , 18.        ,  0.23679169],
            [ 0.        , 13.        ,  0.5127094 ]], dtype=float32),
     array([[1.0000000e+00, 2.0700000e+02, 4.9950428e-02],
            [1.0000000e+00, 9.2000000e+01, 3.4491304e-01]], dtype=float32),
     array([[  2.       , 229.       ,   0.9984485],
            [  4.       ,  60.       ,   0.9372796]], dtype=float32),
     array([[2.000000e+00, 1.194000e+03, 9.985555e-01],
            [3.000000e+00, 1.030000e+02, 9.584518e-01]], dtype=float32),
     array([[2.000000e+00, 1.558000e+03, 9.996946e-01],
            [3.000000e+00, 8.800000e+01, 9.738545e-01]], dtype=float32),
     array([[2.000000e+00, 1.997000e+03, 9.998343e-01],
            [7.000000e+00, 7.020000e+02, 9.954461e-01]], dtype=float32),
     array([[2.0000000e+00, 1.7690000e+03, 9.9997449e-01],
            [3.0000000e+00, 1.7900000e+02, 9.9776447e-01]], dtype=float32),
     array([[2.000000e+00, 1.448000e+03, 9.999393e-01],
            [3.000000e+00, 2.430000e+02, 9.982481e-01]], dtype=float32),
     array([[2.0000000e+00, 1.0770000e+03, 9.9984264e-01],
            [3.0000000e+00, 2.0700000e+02, 9.9882430e-01]], dtype=float32),
     array([[  2.        , 754.        ,   0.9998847 ],
            [  3.        , 493.        ,   0.99971205]], dtype=float32),
     array([[  2.       , 536.       ,   0.9996455],
            [  3.       , 239.       ,   0.9998828]], dtype=float32),
     array([[  2.        , 444.        ,   0.99973446],
            [  3.        ,  98.        ,   0.99974567]], dtype=float32),
     array([[8.0000000e+00, 1.0400000e+02, 1.3962857e-01],
            [2.0000000e+00, 2.3600000e+02, 7.3362941e-01]], dtype=float32),
     array([[ 2.        , 34.        ,  0.06541887],
            [ 2.        , 46.        ,  0.3399737 ]], dtype=float32),
     array([[ 2.        , 52.        ,  0.24562976],
            [ 2.        ,  7.        ,  0.5339988 ]], dtype=float32)]
    
  • 这是我保存最终模型的方法。 (这没有任何问题)
  • >> single_input_merged_output_model.save('file.h5', include_optimizer=False)
    
  • ...这是当我尝试加载我刚刚保存的上述模型时的问题!
  • >> single_input_merged_output_model = load_model('file.h5', compile=False)
    
  • 错误:
  • ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <timed exec> in <module>
    
    ~/anaconda3/lib/python3.6/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
    
    ~/anaconda3/lib/python3.6/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):
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in _deserialize_model(h5dict, custom_objects, compile)
        290                                                        original_keras_version,
        291                                                        original_backend,
    --> 292                                                        reshape=False)
        293         if len(weight_values) != len(symbolic_weights):
        294             raise ValueError('Layer #' + str(k) +
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
        821         weights = convert_nested_time_distributed(weights)
        822     elif layer.__class__.__name__ in ['Model', 'Sequential']:
    --> 823         weights = convert_nested_model(weights)
        824 
        825     if original_keras_version == '1':
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in convert_nested_model(weights)
        809                     weights=weights[:num_weights],
        810                     original_keras_version=original_keras_version,
    --> 811                     original_backend=original_backend))
        812                 weights = weights[num_weights:]
        813         return new_weights
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
        821         weights = convert_nested_time_distributed(weights)
        822     elif layer.__class__.__name__ in ['Model', 'Sequential']:
    --> 823         weights = convert_nested_model(weights)
        824 
        825     if original_keras_version == '1':
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in convert_nested_model(weights)
        797                     weights=weights[:num_weights],
        798                     original_keras_version=original_keras_version,
    --> 799                     original_backend=original_backend))
        800                 weights = weights[num_weights:]
        801 
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
        940             weights[0] = np.reshape(weights[0], layer_weights_shape)
        941         elif layer_weights_shape != weights[0].shape:
    --> 942             weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
        943             if layer.__class__.__name__ == 'ConvLSTM2D':
        944                 weights[1] = np.transpose(weights[1], (3, 2, 0, 1))
    
    ~/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py in transpose(a, axes)
        637 
        638     """
    --> 639     return _wrapfunc(a, 'transpose', axes)
        640 
        641 
    
    ~/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
         54 def _wrapfunc(obj, method, *args, **kwds):
         55     try:
    ---> 56         return getattr(obj, method)(*args, **kwds)
         57 
         58     # An AttributeError occurs if the object does not have
    
    ValueError: axes don't match array
    

    我已经尝试过的事情:
  • 分别保存模型架构和权重并加载 (不工作)

  • 关于加载模型的任何建议?

    最佳答案

    解决了!如果您在 model.save() 之前卡住模型层的权重,然后保存模型; load_model()工作没有任何问题!这仅在您不想进一步重新训练模型时才有效。

    from keras.models import Model
    
    def freeze_layers(model):
        for i in model.layers:
            i.trainable = False
            if isinstance(i, Model):
                freeze_layers(i)
        return model
    
    >> model_freezed = freeze_layers(model)
    >> model_freezed.save('file.tf')
    
    # refresh the notebook
    from keras.models import load_model
    >> model = load_model('file.tf', compile=False)
    
    

    关于python - 值错误 : axes don't match array when loading previously saved model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57779022/

    相关文章:

    python - 导入 theano 给出 AttributeError : module 'theano' has no attribute 'gof'

    python - Tensorflow 的 TimeDistributed

    python - 如何使用 Pandas 找到每个主成分的前三个特征?

    python - 为什么 SeparableConv2D 比 Conv2D 慢?

    python : How to create a dynamic list of class values

    javascript - 在这个简单的神经网络示例中,为什么 Tensorflow 比 convnetjs 慢 100 倍?

    python - 如何将提取的特征传递给keras模型?

    python - Keras - 在 `compile()` 之后修改 lambda 层

    python - 使用 pyuic 将 .ui 转换为 .py?

    未找到 Python 3.5 和 GLIBCXX_3.4.26