[optimization] for the FTRL and SGD optimizers, use the new *_array_sum_sq function to do L2 regularization, vs. the L2 norm which will use the linear algebra meaning
This commit is contained in:
@@ -208,7 +208,7 @@ double ftrl_reg_cost(ftrl_trainer_t *self, double_matrix_t *theta, uint32_array
|
|||||||
if (row_idx >= i_start) {
|
if (row_idx >= i_start) {
|
||||||
double *theta_i = double_matrix_get_row(theta, i);
|
double *theta_i = double_matrix_get_row(theta, i);
|
||||||
|
|
||||||
l2_cost += double_array_l2_norm(theta_i, n);
|
l2_cost += double_array_sum_sq(theta_i, n);
|
||||||
l1_cost += double_array_l1_norm(theta_i, n);
|
l1_cost += double_array_l1_norm(theta_i, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ double stochastic_gradient_descent_reg_cost(sgd_trainer_t *self, uint32_array *u
|
|||||||
double *theta_i = double_matrix_get_row(theta, row);
|
double *theta_i = double_matrix_get_row(theta, row);
|
||||||
|
|
||||||
if (reg_type == REGULARIZATION_L2 && row >= i_start) {
|
if (reg_type == REGULARIZATION_L2 && row >= i_start) {
|
||||||
cost += double_array_l2_norm(theta_i, n);
|
cost += double_array_sum_sq(theta_i, n);
|
||||||
} else if (reg_type == REGULARIZATION_L1 && row >= i_start) {
|
} else if (reg_type == REGULARIZATION_L1 && row >= i_start) {
|
||||||
cost += double_array_l1_norm(theta_i, n);
|
cost += double_array_l1_norm(theta_i, n);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,6 +210,14 @@
|
|||||||
return sqrt((double)result); \
|
return sqrt((double)result); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
static inline unsigned_type name##_sum_sq(type *array, size_t n) { \
|
||||||
|
unsigned_type result = 0; \
|
||||||
|
for (size_t i = 0; i < n; i++) { \
|
||||||
|
result += array[i] * array[i]; \
|
||||||
|
} \
|
||||||
|
return result; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
static inline double name##_mean(type *array, size_t n) { \
|
static inline double name##_mean(type *array, size_t n) { \
|
||||||
unsigned_type sum = name##_sum(array, n); \
|
unsigned_type sum = name##_sum(array, n); \
|
||||||
return (double)sum / n; \
|
return (double)sum / n; \
|
||||||
|
|||||||
Reference in New Issue
Block a user