RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 907565
Accepted
Viktorov
Viktorov
Asked:2020-11-17 03:39:25 +0000 UTC2020-11-17 03:39:25 +0000 UTC 2020-11-17 03:39:25 +0000 UTC

使用 sklearn.linear_model.RidgeCV 时出现 MemoryError

  • 772

我正在尝试训练我的模型

from sklearn.linear_model import RidgeCV
alphas = [0.00001, 0.0001, 0.001, 0.01, 0.01, 0.5, 1, 3,  5]
clf = RidgeCV(alphas=alphas, normalize=True, gcv_mode = 'eigen').fit(x_train, y_train)

其中:

print(x_train.shape, y_train.shape)
(62313, 100600) (62313,)

type(x_train)
scipy.sparse.csr.csr_matrix

在发布之前,我有大约 10 GB 的可用 RAM。启动后,它需要大约 2 GB 并保持空闲 8 字节。我的代码运行了大约半小时,而空闲内存一直在 8 GB 左右,然后崩溃并出现错误:

---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-9-e9d3d7210319> in <module>()
      2 from sklearn.linear_model import RidgeCV
      3 alphas = [0.00001, 0.0001, 0.001, 0.01, 0.01, 0.5, 1, 3,  5]
----> 4 clf = RidgeCV(alphas=alphas, normalize=True, gcv_mode = 'eigen').fit(x_train, y_train)

~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in fit(self, X, y, sample_weight)
   1112                                   gcv_mode=self.gcv_mode,
   1113                                   store_cv_values=self.store_cv_values)
-> 1114             estimator.fit(X, y, sample_weight=sample_weight)
   1115             self.alpha_ = estimator.alpha_
   1116             if self.store_cv_values:

~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in fit(self, X, y, sample_weight)
   1027         centered_kernel = not sparse.issparse(X) and self.fit_intercept
   1028 
-> 1029         v, Q, QT_y = _pre_compute(X, y, centered_kernel)
   1030         n_y = 1 if len(y.shape) == 1 else y.shape[1]
   1031         cv_values = np.zeros((n_samples * n_y, len(self.alphas)))

~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in _pre_compute(self, X, y, centered_kernel)
    883     def _pre_compute(self, X, y, centered_kernel=True):
    884         # even if X is very sparse, K is usually very dense
--> 885         K = safe_sparse_dot(X, X.T, dense_output=True)
    886         # the following emulates an additional constant regressor
    887         # corresponding to fit_intercept=True

~/anaconda3/lib/python3.6/site-packages/sklearn/utils/extmath.py in safe_sparse_dot(a, b, dense_output)
    133     """
    134     if issparse(a) or issparse(b):
--> 135         ret = a * b
    136         if dense_output and hasattr(ret, "toarray"):
    137             ret = ret.toarray()

~/anaconda3/lib/python3.6/site-packages/scipy/sparse/base.py in __mul__(self, other)
    477             if self.shape[1] != other.shape[0]:
    478                 raise ValueError('dimension mismatch')
--> 479             return self._mul_sparse_matrix(other)
    480 
    481         # If it's a list or whatever, treat it like a matrix

~/anaconda3/lib/python3.6/site-packages/scipy/sparse/compressed.py in _mul_sparse_matrix(self, other)
    500                                     maxval=nnz)
    501         indptr = np.asarray(indptr, dtype=idx_dtype)
--> 502         indices = np.empty(nnz, dtype=idx_dtype)
    503         data = np.empty(nnz, dtype=upcast(self.dtype, other.dtype))
    504 

MemoryError: 

你能帮我弄清楚发生了什么以及可以做些什么吗?

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    MaxU - stop genocide of UA
    2020-11-17T17:57:52Z2020-11-17T17:57:52Z

    注意来自的评论error traceback:

    # even if X is very sparse, K is usually very dense
    

    从这个块:

    ~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/ridge.py in _pre_compute(self, X, y, centered_kernel)
        883     def _pre_compute(self, X, y, centered_kernel=True):
        884         # even if X is very sparse, K is usually very dense
    --> 885         K = safe_sparse_dot(X, X.T, dense_output=True)
    

    在您的情况下,您正在使用(62313, 100600)具有66112691非零元素(大约占元素总数的 1%)的稀疏维度矩阵。要存储这样一个矩阵,您需要大约 500MiB 的内存:

    In [21]: 66112691 * 8 / 1024**3
    Out[21]: 0.4925779327750206
    

    从上面的评论如下,执行后:

    K = safe_sparse_dot(X, X.T, dense_output=True)
    

    K将包含很大比例的非零元素。要存储矩阵K(假设它不包含零元素),您将需要额外的 29GiB RAM:

    In [23]: 62313**2 * 8 / 1024**3
    Out[23]: 28.929933674633503
    

    注意:在所有计算中,我从每个非零矩阵单元占用 8 个字节(对于数据类型int64或float64)这一事实出发。如果您有不同的数据类型,请将 8 替换为相应的数字(存储一个非零元素的字节数)。

    我认为您应该考虑减少输入矩阵的维度。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5