Skip to content

Commit d8bbd39

Browse files
committed
Ensure to initialize |w_recalc| parameter.
1 parent 387a213 commit d8bbd39

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

lib/Algorithm/LibLinear.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ sub new {
6262
my $epsilon => +{ isa => Num, optional => 1, },
6363
my $loss_sensitivity => +{ isa => Num, default => 0.1, },
6464
my $nu => +{ isa => Num, default => 0.5, },
65+
my $recalculate_weights => +{ isa => Bool, default => 0, },
6566
my $regularize_bias => +{ isa => Bool, default => 1, },
6667
my $solver => +{
6768
isa => $SolverDescriptor,
@@ -82,6 +83,7 @@ sub new {
8283
$loss_sensitivity,
8384
$nu,
8485
$regularize_bias,
86+
$recalculate_weights,
8587
);
8688
bless +{
8789
bias => $bias,
@@ -170,6 +172,8 @@ sub is_regression_solver { $_[0]->training_parameter->is_regression_solver }
170172

171173
sub loss_sensitivity { $_[0]->training_parameter->loss_sensitivity }
172174

175+
sub recalculate_weights { $_[0]->training_parameter->recalculate_weights }
176+
173177
sub training_parameter { $_[0]->{training_parameter} }
174178

175179
sub train {
@@ -242,7 +246,7 @@ Current version is based on LIBLINEAR 2.48, released on January 5, 2025.
242246
243247
=head1 METHODS
244248
245-
=head2 new([bias => -1.0] [, cost => 1] [, epsilon => 0.1] [, loss_sensitivity => 0.1] [, nu => 0.5] [, regularize_bias => 1] [, solver => 'L2R_L2LOSS_SVC_DUAL'] [, weights => []])
249+
=head2 new([bias => -1.0] [, cost => 1] [, epsilon => undef] [, loss_sensitivity => 0.1] [, nu => 0.5] [, recalculate_weights => 0] [, regularize_bias => 1] [, solver => 'L2R_L2LOSS_SVC_DUAL'] [, weights => []])
246250
247251
Constructor. You can set several named parameters:
248252
@@ -272,6 +276,10 @@ Epsilon in loss function of SVR (C<-p>.)
272276
273277
Nu parameter of one-class SVM (C<-n>.)
274278
279+
=item recalculate_weights
280+
281+
Whether to recalculate weights. This option is only valid for dual solvers for L2-regularized L1/L2-loss SVM (L2R_L1LOSS_SVC_DUAL / L2R_L2LOSS_SVC_DUAL).
282+
275283
=item regularize_bias
276284
277285
Whether to regularize the bias term (C<-R>, negated.)

src/liblinear.xs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ alloc_parameter(pTHX_ int num_weights) {
2525
}
2626
parameter_->init_sol = NULL;
2727
parameter_->nr_weight = num_weights;
28+
parameter_->w_recalc = false;
2829
return parameter_;
2930
}
3031

@@ -394,7 +395,7 @@ MODULE = Algorithm::LibLinear PACKAGE = Algorithm::LibLinear::TrainingParameter
394395
PROTOTYPES: DISABLE
395396

396397
struct parameter *
397-
ll_new(klass, solver_type, epsilon, cost, weight_labels, weights, loss_sensitivity, nu, regularize_bias)
398+
ll_new(klass, solver_type, epsilon, cost, weight_labels, weights, loss_sensitivity, nu, regularize_bias, recalculate_weights = false)
398399
int solver_type;
399400
double epsilon;
400401
double cost;
@@ -403,6 +404,7 @@ ll_new(klass, solver_type, epsilon, cost, weight_labels, weights, loss_sensitivi
403404
double loss_sensitivity;
404405
double nu;
405406
bool regularize_bias;
407+
bool recalculate_weights;
406408
CODE:
407409
int num_weights = av_len(weight_labels) + 1;
408410
if (av_len(weights) + 1 != num_weights) {
@@ -419,6 +421,7 @@ CODE:
419421
RETVAL->p = loss_sensitivity;
420422
RETVAL->nu = nu;
421423
RETVAL->regularize_bias = regularize_bias ? 1 : 0;
424+
RETVAL->w_recalc = recalculate_weights;
422425
dXCPT;
423426
XCPT_TRY_START {
424427
int *weight_labels_ = RETVAL->weight_label;
@@ -518,6 +521,14 @@ CODE:
518521
OUTPUT:
519522
RETVAL
520523

524+
bool
525+
ll_recalculate_weights(self)
526+
struct parameter *self;
527+
CODE:
528+
RETVAL = self->w_recalc;
529+
OUTPUT:
530+
RETVAL
531+
521532
int
522533
ll_solver_type(self)
523534
struct parameter *self;

t/Algorithm/LibLinear/TrainingParameter.t

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ my $tolerance = 10e-15;
1616
cost => num(1, $tolerance),
1717
epsilon => num(0.1, $tolerance),
1818
is_regression_solver => bool(0),
19+
recalculate_weights => bool(0),
1920
weights => [],
2021
],
2122
);
@@ -47,6 +48,22 @@ my $tolerance = 10e-15;
4748
);
4849
}
4950

51+
{
52+
my $learner = new_ok 'Algorithm::LibLinear' => [
53+
recalculate_weights => 1,
54+
];
55+
cmp_methods(
56+
$learner,
57+
[
58+
cost => num(1, $tolerance),
59+
epsilon => num(0.1, $tolerance),
60+
is_regression_solver => bool(0),
61+
recalculate_weights => bool(1),
62+
weights => [],
63+
],
64+
);
65+
}
66+
5067
my $data_set = Algorithm::LibLinear::DataSet->new(data_set => [
5168
+{ feature => +{}, label => 1 },
5269
]);
@@ -61,6 +78,14 @@ my @cases = (
6178
],
6279
error_pattern => qr/p < 0/,
6380
},
81+
+{
82+
constructor_params => [
83+
recalculate_weights => 1,
84+
# Arbitrary value except L2R_L2LOSS_SVC_DUAL / L2R_L1LOSS_SVC_DUAL.
85+
solver => 'L2R_L2LOSS_SVR',
86+
],
87+
error_pattern => qr!Recalculating w in the end is only for dual solvers for L2-regularized L1/L2-loss SVM!,
88+
},
6489
);
6590
for my $case (@cases) {
6691
my $learner = Algorithm::LibLinear->new(@{ $case->{constructor_params} });

0 commit comments

Comments
 (0)