Boost Booster for one iteration with customized gradient statistics. .. note:: For multi-class task, the score is group by class_id first, then group by row_id. If you want to get i-th row score in j-th class, the access way is score[j * num_data + i] an
(self, grad, hess, grad2 = None, hess2 = None)
| 2007 | return self.__boost(grad, hess) |
| 2008 | |
| 2009 | def __boost(self, grad, hess, grad2 = None, hess2 = None): |
| 2010 | """Boost Booster for one iteration with customized gradient statistics. |
| 2011 | |
| 2012 | .. note:: |
| 2013 | |
| 2014 | For multi-class task, the score is group by class_id first, then group by row_id. |
| 2015 | If you want to get i-th row score in j-th class, the access way is score[j * num_data + i] |
| 2016 | and you should group grad and hess in this way as well. |
| 2017 | |
| 2018 | Parameters |
| 2019 | ---------- |
| 2020 | grad : list or numpy 1-D array |
| 2021 | The first order derivative (gradient). |
| 2022 | hess : list or numpy 1-D array |
| 2023 | The second order derivative (Hessian). |
| 2024 | |
| 2025 | Returns |
| 2026 | ------- |
| 2027 | is_finished : bool |
| 2028 | Whether the boost was successfully finished. |
| 2029 | """ |
| 2030 | grad = list_to_1d_numpy(grad, name='gradient') |
| 2031 | hess = list_to_1d_numpy(hess, name='hessian') |
| 2032 | |
| 2033 | assert grad.flags.c_contiguous |
| 2034 | assert hess.flags.c_contiguous |
| 2035 | if len(grad) != len(hess): |
| 2036 | raise ValueError("Lengths of gradient({}) and hessian({}) don't match" |
| 2037 | .format(len(grad), len(hess))) |
| 2038 | is_finished = ctypes.c_int(0) |
| 2039 | if 'num_labels' in self.params and self.params['num_labels'] > 1: |
| 2040 | grad2 = list_to_1d_numpy(grad2, name='gradient') |
| 2041 | hess2 = list_to_1d_numpy(hess2, name='hessian') |
| 2042 | assert grad2.flags.c_contiguous |
| 2043 | assert hess2.flags.c_contiguous |
| 2044 | _safe_call(_LIB.LGBM_BoosterUpdateOneIterCustom2( |
| 2045 | self.handle, |
| 2046 | grad.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), |
| 2047 | hess.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), |
| 2048 | grad2.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), |
| 2049 | hess2.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), |
| 2050 | ctypes.byref(is_finished))) |
| 2051 | else: |
| 2052 | _safe_call(_LIB.LGBM_BoosterUpdateOneIterCustom( |
| 2053 | self.handle, |
| 2054 | grad.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), |
| 2055 | hess.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), |
| 2056 | ctypes.byref(is_finished))) |
| 2057 | self.__is_predicted_cur_iter = [False for _ in range_(self.__num_dataset)] |
| 2058 | return is_finished.value == 1 |
| 2059 | |
| 2060 | def rollback_one_iter(self): |
| 2061 | """Rollback one iteration. |
no test coverage detected