What is the cause of "RuntimeWarning: invalid value encountered in matmul ret = a @ b" when using sklearn?

huangapple go评论90阅读模式
英文:

What is the cause of "RuntimeWarning: invalid value encountered in matmul ret = a @ b" when using sklearn?

问题

在使用 Ridge().fit(X_train, y_train) 时,我遇到了 RuntimeWarning,这发生在一个数据集上,其中 X_train.shape = (9440, 1900)

当我减小数据集的大小,使得 X_train.shape = (1000, 1900) 时,RuntimeWarning 就消失了。

可能导致这个警告的原因是什么,我该如何避免它?我确保没有出现 np.nan、np.inf 和 -np.inf。

英文:

I encountered RuntimeWarning when using Ridge().fit(X_train, y_train) on a dataset where X_train.shape = (9440, 1900)

/Users/username/micromamba/envs/data-science/lib/python3.11/site-packages/sklearn/utils/extmath.py:189: RuntimeWarning: invalid value encountered in matmul
  ret = a @ b

When I reduce the size of dataset X_train.shape = (1000, 1900) the RuntimeWarning goes away.

What might be causing this warning and how can I avoid it? I made sure there are no np.nan, np.inf, and -np.inf.

答案1

得分: 4

这个错误有点知名度,不幸的是,它与架构和环境相关。这在numpy中是一个纯粹的矩阵操作问题。我在我的Colab中使用以下代码复现了这个问题:

# 在numpy==1.25下有警告,在numpy==1.22.4下正常
M = np.random.random((30000, 270)) # 在Colab上运行
M.T @ M

@Stef测试了导致numpy版本崩溃的问题。

请查看这个最近的GitHub讨论,链接为 this recent GitHub discussion,我在其中提供了这个导致问题的示例。

我创建了这个Colab笔记本来帮助找出问题的根本原因(在Colab的选择中,numpy==1.22.4正常运行)。您可以在Colab中进行训练(可以上传您的数据)。

总的建议是降级numpy版本或等待维护者修复此问题。

英文:

This error is somewhat known, and is sadly architecture / environment dependent. This is a purely matrix op issue in numpy. I reproduced it in my Colab with the following code:

# warning under numpy==1.25, OK under numpy==1.22.4
M = np.random.random((30000, 270)) # on Colab
M.T @ M

and @Stef tested the breaking numpy version.

See this recent GitHub discussion under numpy, where I contributed this breaking example.

I created this Colab notebook to help root-causing the issue (works fine under numpy==1.22.4 which is Colab's choice). Try your training under Colab (you can upload your data).

Overall my suggestion would be to downgrade the numpy version or wait for a fix from the maintainers.

huangapple
  • 本文由 发表于 2023年6月22日 06:35:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定