在拟合Sklearn模型时,"设备上没有剩余空间"错误

mac*_*rus 19 python multithreading ioerror scikit-learn

我正在使用scikit-learn为LDA模型提供大量数据.相关代码片段如下所示:

lda = LatentDirichletAllocation(n_topics = n_topics, 
                                max_iter = iters,
                                learning_method = 'online',
                                learning_offset = offset,
                                random_state = 0,
                                evaluate_every = 5,
                                n_jobs = 3,
                                verbose = 0)
lda.fit(X)
Run Code Online (Sandbox Code Playgroud)

(我想这里唯一可能相关的细节是我正在使用多个工作.)

经过一段时间后,即使磁盘上有足够的空间和足够的可用内存,我也会收到"设备上没有剩余空间"错误.我在两台不同的计算机上(在我的本地计算机和远程服务器上)多次尝试相同的代码,首先使用python3,然后使用python2,每次我都得到相同的错误.

如果我在较小的数据样本上运行相同的代码,一切正常.

整个堆栈跟踪:

Failed to save <type 'numpy.ndarray'> to .npy file:
Traceback (most recent call last):
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.py", line 271, in save
    obj, filename = self._write_array(obj, filename)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.py", line 231, in _write_array
    self.np.save(filename, array)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 491, in save
    pickle_kwargs=pickle_kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/numpy/lib/format.py", line 584, in write_array
    array.tofile(fp)
IOError: 275500 requested and 210934 written


IOErrorTraceback (most recent call last)
<ipython-input-7-6af7e7c9845f> in <module>()
      7                                 n_jobs = 3,
      8                                 verbose = 0)
----> 9 lda.fit(X)

/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/decomposition/online_lda.pyc in fit(self, X, y)
    509                     for idx_slice in gen_batches(n_samples, batch_size):
    510                         self._em_step(X[idx_slice, :], total_samples=n_samples,
--> 511                                       batch_update=False, parallel=parallel)
    512                 else:
    513                     # batch update

/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/decomposition/online_lda.pyc in _em_step(self, X, total_samples, batch_update, parallel)
    403         # E-step
    404         _, suff_stats = self._e_step(X, cal_sstats=True, random_init=True,
--> 405                                      parallel=parallel)
    406 
    407         # M-step

/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/decomposition/online_lda.pyc in _e_step(self, X, cal_sstats, random_init, parallel)
    356                                               self.mean_change_tol, cal_sstats,
    357                                               random_state)
--> 358             for idx_slice in gen_even_slices(X.shape[0], n_jobs))
    359 
    360         # merge result

/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    808                 # consumption.
    809                 self._iterating = False
--> 810             self.retrieve()
    811             # Make sure that we get a last message telling us we are done
    812             elapsed_time = time.time() - self._start_time

/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in retrieve(self)
    725                 job = self._jobs.pop(0)
    726             try:
--> 727                 self._output.extend(job.get())
    728             except tuple(self.exceptions) as exception:
    729                 # Stop dispatching any new job in the async callback thread

/home/ubuntu/anaconda2/lib/python2.7/multiprocessing/pool.pyc in get(self, timeout)
    565             return self._value
    566         else:
--> 567             raise self._value
    568 
    569     def _set(self, i, obj):

IOError: [Errno 28] No space left on device
Run Code Online (Sandbox Code Playgroud)

sil*_*ser 32

有同样的问题LatentDirichletAllocation.看来,你的共享内存不足(/dev/shm运行时df -h).尝试将JOBLIB_TEMP_FOLDER环境变量设置为不同的东西:例如,to /tmp.在我的情况下,它解决了这个问题.

或者只是增加共享内存的大小,如果您拥有正在训练LDA的机器的相应权限.

  • 这对我有用.在docker容器中使用iPython,尝试验证如下模型:`best_knn_clf = KNeighborsClassifier(weights ='distance',n_neighbors = 4,n_jobs = -1)得分= cross_val_score(best_knn_clf,X_train_expanded,y_train_expanded,cv = 3,n_jobs = -1,verbose = 3)`.在笔记本中添加了'%env JOBLIB_TEMP_FOLDER =/tmp`就可以了. (15认同)

abh*_*nav 6

使用共享内存且不允许进行I/O操作时会发生此问题.对于大多数Kaggle用户来说,这是一个令人沮丧的问题,同时适合机器学习模型.

我通过使用以下代码设置JOBLIB_TEMP_FOLDER变量来克服此问题.

%env JOBLIB_TEMP_FOLDER=/tmp
Run Code Online (Sandbox Code Playgroud)