如何修复"ValueError:输入0与图层展平不兼容:预期的min_ndim = 3,找到的ndim = 2"加载模型时出错 [英] How to fix ''ValueError: Input 0 is incompatible with layer flatten: expected min_ndim=3, found ndim=2" error when loading model

查看:184
本文介绍了如何修复"ValueError:输入0与图层展平不兼容:预期的min_ndim = 3,找到的ndim = 2"加载模型时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存和加载我的keras模型.它训练,评估并保存良好(使用.h5保存模型),但是当我尝试加载模型时,出现以下错误: ValueError:输入0与图层展平不兼容:预期的min_ndim = 3,找到的ndim = 2. 我加载模型不正确吗?任何帮助将不胜感激!

I'm trying to save and load my keras model. It trains, evaluates, and saves fine (using .h5 to save model) but when I try to load the model I get the following error: ValueError: Input 0 is incompatible with layer flatten: expected min_ndim=3, found ndim=2. Am I loading the model incorrectly? Any help would be appreciated!

这是我保存模型的代码块.

This is the code block from where I'm saving the model.

    def ml(self):
            model = tf.keras.models.Sequential()
            model.add(tf.keras.layers.Flatten())
            self.addLayer(model,145,6)
            model.add(tf.keras.layers.Dense(1))

            optimizer = tf.keras.optimizers.Adam()

            model.compile(loss='mean_squared_error',
                              optimizer=optimizer,
                              metrics=['mean_absolute_error', 
                                       'mean_squared_error'])

            model.fit(self.x_train, self.y_train,epochs=130)


            lm = model.evaluate(self.x_test, self.y_test, batch_size=300)
            model.save('my_model.h5')


    def addLayer(self, model, numNodes, numLayers):
            for i in range(numLayers):
                model.add(tf.keras.layers.Dense(numNodes,activation=tf.nn.relu))

要从其他脚本加载:

    import keras
    from keras.models import load_model
    from keras.utils import CustomObjectScope
    from keras.initializers import glorot_uniform

    with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
            model = load_model(mlPath)

在尝试加载模型时,出现以下错误:

While attempting to load the model I get the following error:

ValueError:输入0与图层展平不兼容:预期的min_ndim = 3,找到的ndim = 2

ValueError: Input 0 is incompatible with layer flatten: expected min_ndim=3, found ndim=2

推荐答案

,您可以尝试仅保存模型的权重,然后使用完全相同的代码重新创建它,并仅加载权重. 所以改变

you can try to save only the weights of your model, then re-create it using exactly the same code, and loading only the weights. so change

model.save

model.save_weights('my_model.h5')

然后,当您要加载模型时,首先,重新创建模型:

Then when you want to load your model, first, you re-create your model:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
self.addLayer(model,145,6)
model.add(tf.keras.layers.Dense(1))

最后加载权重:

model.load_weights('my_model.h5')

这对我有用. 另外,您可以在模型中添加一个Input层以及显式的input_shape.

This worked for me. Also, you could add an Input layer, together with the explicit input_shape in your model.

这篇关于如何修复"ValueError:输入0与图层展平不兼容:预期的min_ndim = 3,找到的ndim = 2"加载模型时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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