2016-05-16 135 views
1

我們使用XGboost4J進行ML預測。我們使用平靜的web服務開發了預測器,以便在平臺內各個組件可以調用ML預測器。例如從產品標題和描述中找出產品類別樹。XGBoost4J同步問題?

只是描述了我們實現的基本方式的代碼。

//這是在 initialize方法中完成的,對於每個模型都有一個singleton Booster對象加載。

Class Predictor{ 
     private Booster xgboost; 
     //init call from Serivice initialization while injecting Predictor 
     public void init(final String modelFile, final Integer numThreads){ 
     if (!(new File(modelFile).exists())) { 
      throw new IOException("Modelfile " + modelFile + " does not exist"); 
     } 

     // we use a util class Params to handle parameters as example 
     final Iterable<Entry<String, Object>> param = new Params() { 
       { 
        put("nthread", numThreads); 
       } 
      }; 
      xgboost = new Booster(param, modelFile); 
    } 

     //Predict method 
     public String predict(final String predictionString){ 
       final String dummyLabel = "-1"; 
       final String x_n = dummyLabel + "\t" + x_n_libsvm_idxStr; 
       final DataLoader.CSRSparseData spData = XGboostSparseData.format(x_n); 
       final DMatrix x_n_dmatrix = new DMatrix(spData.rowHeaders, 
         spData.colIndex, spData.data, DMatrix.SparseType.CSR); 

       final float[][] predict = xgboost.predict(x_n_dmatrix); 
       // Then there is conversion logic of predict to predicted model result   which returns predictions 
        String prediction = getPrediction(predict); 
        return prediction 
     } 
    } 

以上預測類的WebServices服務類單注入 所以對於每一個服務調用線程調用的

service.predict(predictionString); 

有一個在Tomcat容器的問題時,多個併發線程調用預測方法助推器方法是同步的

private synchronized float[][] pred(DMatrix data, boolean outPutMargin, long treeLimit, boolean predLeaf) throws XGBoostError { 
     byte optionMask = 0; 
     if(outPutMargin) { 
      optionMask = 1; 
     } 

     if(predLeaf) { 
      optionMask = 2; 
     } 

     float[][] rawPredicts = new float[1][]; 
     ErrorHandle.checkCall(XgboostJNI.XGBoosterPredict(this.handle, data.getHandle(), optionMask, treeLimit, rawPredicts)); 
     int row = (int)data.rowNum(); 
     int col = rawPredicts[0].length/row; 
     float[][] predicts = new float[row][col]; 

     for(int i = 0; i < rawPredicts[0].length; ++i) { 
      int r = i/col; 
      int c = i % col; 
      predicts[r][c] = rawPredicts[0][i]; 
     } 

     return predicts; 
    } 

此創建的線程由於同步塊和等待而被鎖定和鎖定s導致web服務不可擴展。

我們嘗試從XGboost4J源代碼和編譯的jar中刪除同步,但它在1-2分鐘內崩潰。堆轉儲顯示在下面一行的崩潰而做本地調用XgboostJNI

ErrorHandle.checkCall(XgboostJNI.XGBoosterPredict(this.handle, data.getHandle(), optionMask, treeLimit, rawPredicts)); 

任何人都知道實施Xgboost4J爲高度可擴展的Web服務方法使用Java的更好的辦法?

回答