保存 FittedModel 有问题
在 Mathematica 7 和 8 中,我们发现 Mathematica 无法正确保存并获取拟合例程的结果。我希望这里有人知道:此错误的最佳实践解决方法是什么?
我们的任务是使用 NonlinearModelFit 拟合大量数据集,它为每个数据集返回一个 FittedModel。这些拟合需要一段时间,我们尝试使用 Save 和 DumpSave 将结果存储到文件中。后续分析使用 Get 将 FittedModel 拉回。Mathematica
执行 Save & 操作。没有警告,但返回的 FittedModel 有问题。我们调用 Get 并从磁盘加载 FittedModel,将结果命名为“foo”。尝试 foo[10] 使用输入 10 评估拟合模型。我们看到的错误是 foo["BestFitParameters"] 应该返回一个规则列表,而是将字符串插入到函数中,因为它是像 10 这样的数字。 [{"BestFitParameters","BestFitParameters"}] 应该在列表中给出两次规则,但对于有问题的 foo 保持未评估状态。
除此之外,还有一个奇怪且误导性的行为,具体取决于我们是否删除/清除 foo 或退出内核:
- 将 FittedModel 计算为 foo 将 FittedModel
- 保存或转储保存 foo 到磁盘
- 删除和/或清除 foo
- 从磁盘获取 foo
- foo 工作正常
但是如果我们重新启动内核,它将失败
- 将 FittedModel 计算为 foo
- 保存或转储 将 foo 保存到磁盘
- 终止并重新启动内核
- 从磁盘获取 foo
- foo 有问题
有人以前见过这种行为吗?
这个错误背后有一个很好的解释吗?
有没有好的解决方法?
With Mathematica 7 and 8 we have discovered that Mathematica fails to properly Save and Get the result of a fitting routine. I am hoping someone here knows: What is the best practice workaround for this bug?
Our task is that we are fitting lots of data sets with NonlinearModelFit, which returns a FittedModel for each data set. These fits take a while and we tried using Save and DumpSave to memorize the results to a file. Subsequent analysis uses Get to pull the FittedModel(s) back in.
Mathematica executes the Save & Get without warnings but the FittedModel that comes back is buggy. We call Get and load a FittedModel from disk, call the result "foo". Trying foo[10] evaluates the fitted model with input 10. The bug we see is that foo["BestFitParameters"] should return a list a rules but instead plugs the string into the function as it it were a number like 10. Trying foo[{"BestFitParameters","BestFitParameters"}] should give the rules twice in a list, but stays unevaluated with the buggy foo.
There is a strange and misleading behavior on top of this, depending on whether we Remove/Clear foo or quit the kernel:
- Compute the FittedModel as foo
- Save or DumpSave foo to disk
- Remove and/or Clear foo
- Get foo from disk
- foo works fine
But if we restart the kernel it fails
- Compute the FittedModel as foo
- Save or DumpSave foo to disk
- Kill and restart the kernel
- Get foo from disk
- foo is buggy
Has anyone seen this kind of behavior before?
Is there a good explanation behind this bug?
Is there a good workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个已知的错误,幸运的是有一个简单的解决方法。在评估
Get
以读取保存的FittedModel
之前,评估虚拟拟合模型代码。如果跳过此初始评估,则由于无法自动加载实现该功能的所有依赖代码,拟合模型似乎无法正常工作。
In[1]
的评估基本上强制加载。This is a known bug, and luckily there is an easy workaround. Before evaluating
Get
to read the savedFittedModel
, evaluate a dummy fitted model code.If this initial evaluation is skipped, the fitted model does not appear to work correctly as stated reportedly due to failure to automatically load all the dependent code that implements the functionality. The evaluation of
In[1]
basically forces the loading.