限制XGBoost使用的线程数

时间:2018-01-15 19:06:14

标签: python scikit-learn python-multithreading xgboost

我想将XGBoost用于在线制作(Python 2.7 XGBoost API)。 为了能够做到这一点,我想在预测操作中控制并限制XGBoost 使用的线程数。

我正在使用XGBoost(xgboost.XGBRegressor)提供的 sklearn 兼容回归程序,并尝试使用param nthread 在regressor的构造函数中将最大线程限制为1。

不幸的是,无论 nthread 中设置的值如何,XGBoost都会继续使用多个线程。

是否有其他方法可以限制XGBoost并强制它使用n = 1个线程执行预测操作?

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,并弄清楚了。正确的答案是设置系统环境变量。 对于python脚本:

import os
os.environ['OMP_NUM_THREADS'] = "1"

会工作的。

在导入任何其他软件包之前,请确保将这两行放在一起,否则可能无法正常工作。

答案 1 :(得分:1)

答案: 带有nthread的标准set_params失败,但在使用regr._Booster.set_param('nthread', 1)时,我能够将XGBoost限制为使用单个线程。

如上所述,env变量OMP_NUM_THREADS=1也可以。

答案 2 :(得分:0)

当前import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; public class Main { public static void main(String[] args) { LocalDateTime ldt = LocalDateTime.of(2018, 12, 05, 18, 11, 27, 187000000); // Prints default format i.e. LocalDateTime#toString System.out.println(ldt); // Custom format DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH); String formatted = dtf.format(ldt); System.out.println(formatted); // Get LocalDate from LocalDateTime LocalDate date = ldt.toLocalDate(); System.out.println(date); } } 可用于在预测时间限制线程:

2018-12-05T18:11:27.187 2018-12-05 18:11:27.187 2018-12-05

更多信息

以前(但现在已弃用):

const array = [ { id: 1, size: 1 } , { id: 2, size: 2 } , { id: 3, size: 4 } , { id: 4, size: 1 } , { id: 5, size: 2 } , { id: 6, size: 3 } // , ... ] , szMax = array.reduce((t,c)=>Math.max(t,c.size),0) , temp = array.map(e=>({...e})) , result = [] ; while (temp.length > 0) { let sz = szMax , nv = [] ; while( sz > 0 ) { let idx = temp.findIndex(x=>x.size <= sz) if (idx===-1) break nv.push( temp[idx] ) sz -= temp[idx].size temp.splice(idx,1) } result.push([...nv]) nv = [] } console.log( result )