@@ -21,14 +21,16 @@ np.import_array()
21
21
def _update_gradients_least_squares (
22
22
G_H_DTYPE_C [::1] gradients , # OUT
23
23
const Y_DTYPE_C [::1] y_true , # IN
24
- const Y_DTYPE_C [::1] raw_predictions ): # IN
24
+ const Y_DTYPE_C [::1] raw_predictions , # IN
25
+ int n_threads , # IN
26
+ ):
25
27
26
28
cdef:
27
29
int n_samples
28
30
int i
29
31
30
32
n_samples = raw_predictions.shape[0 ]
31
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
33
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
32
34
# Note: a more correct expression is 2 * (raw_predictions - y_true)
33
35
# but since we use 1 for the constant hessian value (and not 2) this
34
36
# is strictly equivalent for the leaves values.
@@ -40,14 +42,16 @@ def _update_gradients_hessians_least_squares(
40
42
G_H_DTYPE_C [::1] hessians , # OUT
41
43
const Y_DTYPE_C [::1] y_true , # IN
42
44
const Y_DTYPE_C [::1] raw_predictions , # IN
43
- const Y_DTYPE_C [::1] sample_weight ): # IN
45
+ const Y_DTYPE_C [::1] sample_weight , # IN
46
+ int n_threads , # IN
47
+ ):
44
48
45
49
cdef:
46
50
int n_samples
47
51
int i
48
52
49
53
n_samples = raw_predictions.shape[0 ]
50
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
54
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
51
55
# Note: a more correct exp is 2 * (raw_predictions - y_true) * sample_weight
52
56
# but since we use 1 for the constant hessian value (and not 2) this
53
57
# is strictly equivalent for the leaves values.
@@ -60,14 +64,15 @@ def _update_gradients_hessians_least_absolute_deviation(
60
64
G_H_DTYPE_C [::1] hessians , # OUT
61
65
const Y_DTYPE_C [::1] y_true , # IN
62
66
const Y_DTYPE_C [::1] raw_predictions , # IN
63
- const Y_DTYPE_C [::1] sample_weight ): # IN
64
-
67
+ const Y_DTYPE_C [::1] sample_weight , # IN
68
+ int n_threads , # IN
69
+ ):
65
70
cdef:
66
71
int n_samples
67
72
int i
68
73
69
74
n_samples = raw_predictions.shape[0 ]
70
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
75
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
71
76
# gradient = sign(raw_predicition - y_pred) * sample_weight
72
77
gradients[i] = sample_weight[i] * (2 *
73
78
(y_true[i] - raw_predictions[i] < 0 ) - 1 )
@@ -77,14 +82,15 @@ def _update_gradients_hessians_least_absolute_deviation(
77
82
def _update_gradients_least_absolute_deviation (
78
83
G_H_DTYPE_C [::1] gradients , # OUT
79
84
const Y_DTYPE_C [::1] y_true , # IN
80
- const Y_DTYPE_C [::1] raw_predictions ): # IN
81
-
85
+ const Y_DTYPE_C [::1] raw_predictions , # IN
86
+ int n_threads , # IN
87
+ ):
82
88
cdef:
83
89
int n_samples
84
90
int i
85
91
86
92
n_samples = raw_predictions.shape[0 ]
87
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
93
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
88
94
# gradient = sign(raw_predicition - y_pred)
89
95
gradients[i] = 2 * (y_true[i] - raw_predictions[i] < 0 ) - 1
90
96
@@ -94,23 +100,24 @@ def _update_gradients_hessians_poisson(
94
100
G_H_DTYPE_C [::1] hessians , # OUT
95
101
const Y_DTYPE_C [::1] y_true , # IN
96
102
const Y_DTYPE_C [::1] raw_predictions , # IN
97
- const Y_DTYPE_C [::1] sample_weight ): # IN
98
-
103
+ const Y_DTYPE_C [::1] sample_weight , # IN
104
+ int n_threads , # IN
105
+ ):
99
106
cdef:
100
107
int n_samples
101
108
int i
102
109
Y_DTYPE_C y_pred
103
110
104
111
n_samples = raw_predictions.shape[0 ]
105
112
if sample_weight is None :
106
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
113
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
107
114
# Note: We use only half of the deviance loss. Therefore, there is
108
115
# no factor of 2.
109
116
y_pred = exp(raw_predictions[i])
110
117
gradients[i] = (y_pred - y_true[i])
111
118
hessians[i] = y_pred
112
119
else :
113
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
120
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
114
121
# Note: We use only half of the deviance loss. Therefore, there is
115
122
# no factor of 2.
116
123
y_pred = exp(raw_predictions[i])
@@ -123,20 +130,22 @@ def _update_gradients_hessians_binary_crossentropy(
123
130
G_H_DTYPE_C [::1] hessians , # OUT
124
131
const Y_DTYPE_C [::1] y_true , # IN
125
132
const Y_DTYPE_C [::1] raw_predictions , # IN
126
- const Y_DTYPE_C [::1] sample_weight ): # IN
133
+ const Y_DTYPE_C [::1] sample_weight , # IN
134
+ int n_threads , # IN
135
+ ):
127
136
cdef:
128
137
int n_samples
129
138
Y_DTYPE_C p_i # proba that ith sample belongs to positive class
130
139
int i
131
140
132
141
n_samples = raw_predictions.shape[0 ]
133
142
if sample_weight is None :
134
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
143
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
135
144
p_i = _cexpit(raw_predictions[i])
136
145
gradients[i] = p_i - y_true[i]
137
146
hessians[i] = p_i * (1. - p_i)
138
147
else :
139
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
148
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
140
149
p_i = _cexpit(raw_predictions[i])
141
150
gradients[i] = (p_i - y_true[i]) * sample_weight[i]
142
151
hessians[i] = p_i * (1. - p_i) * sample_weight[i]
@@ -147,7 +156,9 @@ def _update_gradients_hessians_categorical_crossentropy(
147
156
G_H_DTYPE_C [:, ::1] hessians , # OUT
148
157
const Y_DTYPE_C [::1] y_true , # IN
149
158
const Y_DTYPE_C [:, ::1] raw_predictions , # IN
150
- const Y_DTYPE_C [::1] sample_weight ): # IN
159
+ const Y_DTYPE_C [::1] sample_weight , # IN
160
+ int n_threads , # IN
161
+ ):
151
162
cdef:
152
163
int prediction_dim = raw_predictions.shape[0 ]
153
164
int n_samples = raw_predictions.shape[1 ]
@@ -160,7 +171,7 @@ def _update_gradients_hessians_categorical_crossentropy(
160
171
Y_DTYPE_C p_i_k
161
172
162
173
if sample_weight is None :
163
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
174
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
164
175
# first compute softmaxes of sample i for each class
165
176
for k in range (prediction_dim):
166
177
p[i, k] = raw_predictions[k, i] # prepare softmax
@@ -171,7 +182,7 @@ def _update_gradients_hessians_categorical_crossentropy(
171
182
gradients[k, i] = p_i_k - (y_true[i] == k)
172
183
hessians[k, i] = p_i_k * (1. - p_i_k)
173
184
else :
174
- for i in prange(n_samples, schedule = ' static' , nogil = True ):
185
+ for i in prange(n_samples, schedule = ' static' , nogil = True , num_threads = n_threads ):
175
186
# first compute softmaxes of sample i for each class
176
187
for k in range (prediction_dim):
177
188
p[i, k] = raw_predictions[k, i] # prepare softmax
0 commit comments