简体   繁体   English

用自定义层加载Keras中保存的模型,预测结果不一样?

[英]Loading a saved model in Keras with a custom layer and prediction results are different?

My network was achieving 96% accuracy on my dataset (edit: on predicting 9 classes).我的网络在我的数据集上实现了 96% 的准确率(编辑:预测 9 个类别)。 I saved the entire model for every epoch (weights included) whenever I ran it.每当我运行它时,我都会为每个时代(包括权重)保存整个模型。 I ran it 3 times, each time testing different hyperparameters, each time achieving around 96% accuracy.我运行了 3 次,每次测试不同的超参数,每次都达到 96% 左右的准确率。

When I try to load any of these tests now and run it again, it achieves around 50% accuracy.当我现在尝试加载这些测试中的任何一个并再次运行它时,它达到了大约 50% 的准确率。 I'm extremely confident I'm running it on the same dataset.我非常有信心在同一个数据集上运行它。

Here's the interesting thing: if I train a new network instance of the exact same architecture, same size, shape, etc., it only ever reaches a max of 85% accuracy.有趣的是:如果我训练一个具有完全相同架构、相同大小、形状等的新网络实例,它最多只能达到 85% 的准确率。 Additionally, saving and loading these new training models works correctly , as in the model will reach the same 85% accuracy.此外,保存和加载这些新的训练模型可以正常工作,因为在模型中将达到相同的 85% 准确率。

So, there's no problem with loading and no problem with my dataset.所以,有一个与负载没有问题,我的数据集没有问题。 The only way this could happen is if something is wrong in my custom layer, or something else is happening.发生这种情况的唯一方法是我的自定义层出现问题,或者发生了其他事情。

Unfortunately I haven't been committing all of my changes to the custom layer to git.不幸的是,我还没有将我对自定义层的所有更改都提交给 git。 While I can't guarantee that my custom layer is the exact same, I'm almost completely confident it is.虽然我不能保证我的自定义图层完全相同,但我几乎完全有信心。

Any ideas to what could be causing this discrepancy?关于可能导致这种差异的任何想法?

Edit: To add more context, the layer is ripped from the ConvLSTM2d class but I replaced the call() function to simply be a vanilla RNN that uses convolution instead of the dot product.编辑:为了添加更多上下文,该层从 ConvLSTM2d 类中剥离,但我将 call() 函数替换为简单的使用卷积而不是点积的普通 RNN。 I am confident that the call() function is the same as before, but I am not confident that the rest of the class is the same.相信,在()函数调用是和以前一样,但我不相信,班里的其他同学也一样。 Is there anything else in the class that could affect performance?课堂上还有其他可能影响性能的东西吗? I've checked activation function already.我已经检查了激活功能。

Well, let´s think about it, there are basically two possible reasons for your problem.好吧,让我们考虑一下,您的问题基本上有两个可能的原因。 Your custom layer is not the same and therefore some weights are not set up correctly, which leads to missclassification.您的自定义层不相同,因此某些权重设置不正确,从而导致误分类。 In case of a binary classification you will end up in 50% accuracy on balanced data.在二进制分类的情况下,您最终会在平衡数据上获得 50% 的准确率。

The second possible reason is, that threre is a bug in keras serialization.第二个可能的原因是,keras 序列化中存在错误。 In this case your new trained and saved models should also show this issue, after deserialization.在这种情况下,您的新训练和保存的模型在反序列化后也应该显示此问题。

While I can't guarantee that my custom layer is the exact same, I'm almost completely confident it is.虽然我不能保证我的自定义图层完全相同,但我几乎完全有信心。

So the problem is not reproducable with a new model, you accuracy is 50% and you cannot guarantee that the custom layer matches.所以这个问题不能用新模型重现,你的准确率为 50%,你不能保证自定义层匹配。 -> I guess it is you custom layer. -> 我猜是你自定义层。

I finally found the answer.终于找到了答案。

I have implemented a custom Lambda layer to handle reshaping.我已经实现了一个自定义 Lambda 层来处理整形。 This layer has difficulties loading.该层加载困难。 Specifically, it reshapes one dimension into two dimensions on an arbitrary interval.具体来说,它将一维重塑为任意间隔的二维。 The interval was defaulting to one specific value every time I loaded the model, even though it was incorrect.每次我加载模型时,间隔默认为一个特定值,即使它不正确。 When I force it to be the correct interval, it works.当我强制它是正确的间隔时,它就起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM