ValueError:加载先前保存的模型时,轴与数组不匹配

时间:2019-09-03 21:35:38

标签: python tensorflow keras

系统信息

  • 我是否编写了自定义代码(与使用示例目录相对):是
  • OS平台和发行版(例如Linux Ubuntu 16.04):Linux Ubuntu 16.04
  • TensorFlow后端(是/否):是
  • TensorFlow版本:1.14.0
  • Keras版本:2.2.5
  • Python版本:3.6.8
  • CUDA / cuDNN版本:N / A
  • GPU模型和内存:N / A

描述当前行为:

  • 使用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

我已经尝试过的事情:

  • 分别保存模型架构和权重并加载(不起作用)

对加载模型有何建议?

1 个答案:

答案 0 :(得分:0)

解决了!如果冻结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)