akurennoy / moltr

Multi-Objective Learning to Rank with LightGBM
MIT License
11 stars 2 forks source link

ERROR: buffer source array is read-only #1

Closed burakisiklidh closed 2 years ago

burakisiklidh commented 2 years ago

Hi, I'm running the moltr code in UNIX. It throws this error: "buffer source array is read-only". Do you know what can cause this?

ValueError                                Traceback (most recent call last)
<ipython-input-30-81cd0e3846ab> in <module>
----> 1 custom_model = lgb.train(
      2     lgb_params,
      3     lgb_train_set,
      4     valid_sets=[lgb_valid_set],
      5     valid_names=["val"],

~/.local/lib/python3.8/site-packages/lightgbm/engine.py in train(params, train_set, num_boost_round, valid_sets, valid_names, fobj, feval, init_model, feature_name, categorical_feature, early_stopping_rounds, evals_result, verbose_eval, learning_rates, keep_training_booster, callbacks)
    290                                     evaluation_result_list=None))
    291 
--> 292         booster.update(fobj=fobj)
    293 
    294         evaluation_result_list = []

~/.local/lib/python3.8/site-packages/lightgbm/basic.py in update(self, train_set, fobj)
   3027             if not self.__set_objective_to_none:
   3028                 self.reset_parameter({"objective": "none"}).__set_objective_to_none = True
-> 3029             grad, hess = fobj(self.__inner_predict(0), self.train_set)
   3030             return self.__boost(grad, hess)
   3031 

<ipython-input-18-39714e540c99> in lambdamart_objective(preds, dataset)
      6         grad = np.zeros(len(preds))
      7         hess = np.zeros(len(preds))
----> 8         get_gradients(np.ascontiguousarray(dataset.label, dtype=np.double), 
      9                       np.ascontiguousarray(preds),
     10                       len(preds),

lambdaobj.pyx in lambdaobj.get_gradients()

/tmp/moltr/moltr/lambdaobj.cpython-38-x86_64-linux-gnu.so in View.MemoryView.memoryview_cwrapper()

/tmp/moltr/moltr/lambdaobj.cpython-38-x86_64-linux-gnu.so in View.MemoryView.memoryview.__cinit__()

ValueError: buffer source array is read-only
akurennoy commented 2 years ago

Hi @burakisiklidh,

Thanks for your message. Can you check that all of the group lengths are correct? Does it run through when LightGBM is invoked with the standard ranking objective instead of the custom one?

burakisiklidh commented 2 years ago

Hi, I fixed it by using this function:

def _memoryview_safe(x):
    if not x.flags.writeable:
        if not x.flags.owndata:
            x = x.copy(order='C')
        x.setflags(write=True)
    return x