Keras:load_model ValueError:轴与数组不匹配 [英] Keras:load_model ValueError: axes don't match array

查看:1725
本文介绍了Keras:load_model ValueError:轴与数组不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与 keras-gan/wgan-g wgan.generator.save('generator.h5')

I'm studying gan with keras-gan/wgan-gp example with my own dataset. I save models with wgan.generator.save('generator.h5')

wgan.critic.save('critic.h5')

并加载

model = load_model('generator.h5')

model = load_model('critic.h5')

但这只在第一时间有效.当我在第二次训练后再次保存模型并运行

But this only works fine at the fist time.When I saved the models again after the second training and run

model = load_model('generator.h5')

model = load_model('critic.h5')

再次,发生错误:

ValueError跟踪(最近一次通话最后一次) 在 () ----> 1个模型= load_model('generator.h5')

ValueError Traceback (most recent call last) in () ----> 1 model = load_model('generator.h5')

D:\ load_model中的D:\ keras \ engine \ saving.py(文件路径,custom_objects,编译) 262 263#设置砝码 -> 264 load_weights_from_hdf5_group(f ['model_weights'],model.layers) 265 266(如果编译):

D:\keras\engine\saving.py in load_model(filepath, custom_objects, compile) 262 263 # set weights --> 264 load_weights_from_hdf5_group(f['model_weights'], model.layers) 265 266 if compile:

D:load_weights_from_hdf5_group中的D:\ keras \ engine \ saving.py(f,图层,重塑) 第914章 915 original_backend, -> 916 reshape = reshape) 第917章真相大白 918提高ValueError('Layer#'+ str(k)+

D:\keras\engine\saving.py in load_weights_from_hdf5_group(f, layers, reshape) 914 original_keras_version, 915 original_backend, --> 916 reshape=reshape) 917 if len(weight_values) != len(symbolic_weights): 918 raise ValueError('Layer #' + str(k) +

D:\ keras \ engine \ saving.py(图层,权重,original_keras_version,original_backend,重塑) 555权重= convert_nested_time_distributed(权重) 556个Elif图层..名称在['Model','Sequential']中: -> 557权重= convert_nested_model(权重) 558 559,如果original_keras_version =='1':

D:\keras\engine\saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape) 555 weights = convert_nested_time_distributed(weights) 556 elif layer.class.name in ['Model', 'Sequential']: --> 557 weights = convert_nested_model(weights) 558 559 if original_keras_version == '1':

D:\ keras \ engine \ saving.py在convert_nested_model中(权重) 543 weights = weights [:num_weights], 544 original_keras_version = original_keras_version, -> 545 original_backend = original_backend)) 546权重=权重[num_weights:] 547返回new_weights

D:\keras\engine\saving.py in convert_nested_model(weights) 543 weights=weights[:num_weights], 544 original_keras_version=original_keras_version, --> 545 original_backend=original_backend)) 546 weights = weights[num_weights:] 547 return new_weights

D:\ keras \ engine \ saving.py(图层,权重,original_keras_version,original_backend,重塑) 555权重= convert_nested_time_distributed(权重) 556个Elif图层..名称在['Model','Sequential']中: -> 557权重= convert_nested_model(权重) 558 559,如果original_keras_version =='1':

D:\keras\engine\saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape) 555 weights = convert_nested_time_distributed(weights) 556 elif layer.class.name in ['Model', 'Sequential']: --> 557 weights = convert_nested_model(weights) 558 559 if original_keras_version == '1':

D:\ keras \ engine \ saving.py在convert_nested_model中(权重) 531 weights = weights [:num_weights], 532 original_keras_version = original_keras_version, -> 533 original_backend = original_backend)) 534重量=重量[num_weights:] 535

D:\keras\engine\saving.py in convert_nested_model(weights) 531 weights=weights[:num_weights], 532 original_keras_version=original_keras_version, --> 533 original_backend=original_backend)) 534 weights = weights[num_weights:] 535

D:\ keras \ engine \ saving.py(图层,权重,original_keras_version,original_backend,重塑) 673权重[0] = np.reshape(权重[0],layer_weights_shape) 674 elif layer_weights_shape!= weights [0] .shape: -> 675 weights [0] = np.transpose(weights [0],(3,2,0,1)) 676 if layer..名称 =='ConvLSTM2D': 677权重 1 = np.transpose(权重

D:\keras\engine\saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape) 673 weights[0] = np.reshape(weights[0], layer_weights_shape) 674 elif layer_weights_shape != weights[0].shape: --> 675 weights[0] = np.transpose(weights[0], (3, 2, 0, 1)) 676 if layer.class.name == 'ConvLSTM2D': 677 weights1 = np.transpose(weights1, (3, 2, 0, 1))

c:\ users \ administrator \ appdata \ local \ programs \ python \ python35 \ lib \ site-packages \ numpy \ core \ fromnumeric.py在转置(a,轴)中 596 597" -> 598 return _wrapfunc(a,'transpose',axes) 599 600

c:\users\administrator\appdata\local\programs\python\python35\lib\site-packages\numpy\core\fromnumeric.py in transpose(a, axes) 596 597 """ --> 598 return _wrapfunc(a, 'transpose', axes) 599 600

c:\ users \ administrator \ appdata \ local \ programs \ python \ python35 \ lib \ site-packages \ numpy \ core \ fromnumeric.py in _wrapfunc(obj,method,* args,** kwds) 49 def _wrapfunc(obj,method,* args,** kwds): 50次尝试: ---> 51 return getattr(obj,method)(* args,** kwds) 52 53#如果对象不具有AttributeError

c:\users\administrator\appdata\local\programs\python\python35\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 49 def _wrapfunc(obj, method, *args, **kwds): 50 try: ---> 51 return getattr(obj, method)(*args, **kwds) 52 53 # An AttributeError occurs if the object does not have

ValueError:轴与数组不匹配.

ValueError: axes don't match array`

我正在使用

Python 3.5.3

Keras 2.2.2

h5py 2.8.0

tensorflow-gpu 1.9.0

keras-contrib 2.0.8

Keras-Applications 1.0.4

Keras-Preprocessing 1.0.2

任何建议都将受到赞赏.

Any advice and suggestions will be appreciated.

推荐答案

类似于以下问题:

https://github.com/keras-team/keras/pull/11847

https://github.com/tensorflow/tensorflow/issues/27769

尽管该错误尚未修复,但仅在模型中同时具有可训练和不可训练的权重时,才会出现此问题.如果您不需要进一步训练模型,可以通过在保存之前冻结所有权重来解决该问题:

While the bug has yet to be fixed, the problem only occurs when there are both trainable and non-trainable weights in the model. If you do not need to train the model further, you can work around the problem by freezing all the weights prior to saving:

from keras import models

def freeze(model):
    """Freeze model weights in every layer."""
    for layer in model.layers:
        layer.trainable = False

        if isinstance(layer, models.Model):
            freeze(layer)

这篇关于Keras:load_model ValueError:轴与数组不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆