【问题标题】:Memory Error in Scikit Learn: NystroemScikit Learn 中的内存错误:Nystroem
【发布时间】:2013-10-05 07:07:54
【问题描述】:

我正在使用 scikit learn 进行 Nystroem 近似。主要代码是:

feature_map_fourier = RBFSampler(gamma=0.5, random_state=1)
feature_map_nystroem = Nystroem(gamma=0.5, random_state=1)
fourier_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fourier),
                                        ("svm", svm.LinearSVC(C=4))])
nystroem_approx_svm = pipeline.Pipeline([("feature_map", feature_map_nystroem),
                                        ("svm", svm.LinearSVC(C=4))])
# fit and predict using linear and kernel svm:
sample_sizes = np.arange(1,20)
print sample_sizes
fourier_scores = []
nystroem_scores = []
fourier_times = []
nystroem_times = []
for D in sample_sizes:
    avgtime = 0.0
    avgscore = 0.0
    avgftime = 0.0
    avgfscore = 0.0
    ns = []
    fs = []
    for i in range(0, 10):
    feature_map_fourier = RBFSampler(gamma=0.5, random_state=i) 
        feature_map_nystroem = Nystroem(gamma=0.5, random_state=i)
        fourier_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fourier),
                                        ("svm", svm.LinearSVC(C=1))])
        nystroem_approx_svm = pipeline.Pipeline([("feature_map", feature_map_nystroem),("svm", svm.LinearSVC(C=1))])
    nystroem_approx_svm.set_params(feature_map__n_components=D)
        nystroem_approx_svm.fit(data_train, targets_train)
        fourier_approx_svm.set_params(feature_map__n_components=D)
        fourier_approx_svm.fit(data_train, targets_train)
        start = time()
        fourier_score = fourier_approx_svm.score(data_test, targets_test)
        t = time() - start

        avgftime += t
        avgfscore += fourier_score     
        start = time()
        nystroem_score = nystroem_approx_svm.score(data_test, targets_test)
        t = time() - start
        avgtime +=  t
        avgscore += nystroem_score
        ns.append(avgscore)
        fs.append(avgfscore)
    print 'Nstrrom '+str(np.std(ns))
    print 'fs '+str(np.std(ns))    
    nystroem_times.append(avgtime/10.0)
    nystroem_scores.append(avgscore/10.0)
    fourier_times.append(avgftime/10.0)
    fourier_scores.append(avgfscore/10.0)

我在尝试运行此代码时遇到以下错误。

C:\Users\t-sujain\Documents\LDKL BaseLine\Nystreom>forestNormalized_kernel_appro
x.py
522910
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
Traceback (most recent call last):
  File "C:\Users\t-sujain\Documents\LDKL BaseLine\Nystreom\forestNormalized_kern
el_approx.py", line 70, in <module>
    nystroem_approx_svm.fit(data_train, targets_train)
  File "F:\Python27\lib\site-packages\sklearn\pipeline.py", line 126, in fit
    Xt, fit_params = self._pre_transform(X, y, **fit_params)
  File "F:\Python27\lib\site-packages\sklearn\pipeline.py", line 116, in _pre_tr
ansform
    Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
  File "F:\Python27\lib\site-packages\sklearn\base.py", line 364, in fit_transfo
rm
    return self.fit(X, y, **fit_params).transform(X)
  File "F:\Python27\lib\site-packages\sklearn\kernel_approximation.py", line 470
, in transform
    gamma=self.gamma)
  File "F:\Python27\lib\site-packages\sklearn\metrics\pairwise.py", line 808, in
 pairwise_kernels
    return func(X, Y, **kwds)
  File "F:\Python27\lib\site-packages\sklearn\metrics\pairwise.py", line 345, in
 rbf_kernel
    K = euclidean_distances(X, Y, squared=True)
  File "F:\Python27\lib\site-packages\sklearn\metrics\pairwise.py", line 148, in
 euclidean_distances
    XX = X.multiply(X).sum(axis=1)
  File "F:\Python27\lib\site-packages\scipy\sparse\compressed.py", line 251, in
multiply
    return self._binopt(other,'_elmul_')
  File "F:\Python27\lib\site-packages\scipy\sparse\compressed.py", line 676, in
_binopt
    data    = np.empty(maxnnz, dtype=upcast(self.dtype,other.dtype))
MemoryError

我正在使用 cygbin 和具有 100GB RAM 的系统,因此系统不可能内存不足。有人可以帮我吗?

【问题讨论】:

  • 显然您的系统内存不足。这可能是transform 方法的sklearn 实现中的问题。您的数据(n_samples, n_features) 的形状是什么?显然您将 n_components 保留为默认值 100。对吗?
  • 其实我看错了代码。能不能打印出D的值,程序崩溃前的组件个数?
  • 另外你使用的 scikit-learn 是什么版本的?
  • 可以尝试搭建scikit-learn的master分支吗?我认为@larsmans 最近检查了一些可能解决您的问题的稀疏数据案例的优化。
  • 是的,我可以打印 D 的值。

标签: python windows cygwin scikit-learn


【解决方案1】:

根据 cmets 中的讨论:此崩溃是由于在传递稀疏数据作为输入时在 transform 方法中发生的二次过度分配造成的。在 0.14.1 发布后,它已在 master 分支中修复。

另请注意:在高维稀疏输入上使用 RBF 内核可能不是很有用。通常稀疏矩阵表示用于稀疏的高维数据,例如文本文档的词袋特征。对于此类数据,线性内核通常与非线性内核一样好或更好,因此Nystroem 方法在这种情况下可能无用。

【讨论】:

  • 即使是 0.14.1 版本也没有解决这个问题。这是 32 位版本的问题。 64位版本解决了这些问题。
  • 这可能意味着您正在使用 4GB+ 内存。有兴趣的可以看看memory_profiler来查明罪魁祸首。
猜你喜欢
  • 2014-05-31
  • 2018-02-15
  • 2016-06-28
  • 2012-06-27
  • 1970-01-01
  • 2017-09-10
  • 2014-06-28
  • 2016-01-26
  • 2016-11-24
相关资源
最近更新 更多