Algorithm-LibLinear

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


0.24    Mon 4 Apr 2022
        - Bundle LIBLINEAR 2.44.

0.23    Wed 18 Nov 2020
        - Bundle LIBLINEAR 2.42, featuring automatic switching from dual CD
          solver to primal Newton solver when convergence is slow.

0.22    Fri 28 Aug 2020
        - Algorithm::LibLinear: Make find_parameters() / find_cost_parameter()
          throw an error when the given solver type is unsupported.
        - Fix documentation errors.
        - Fix a broken test case.

0.21    Sun 23 Aug 2020
        - Bundle LIBILNEAR 2.41.
        - Bump minimum required Perl version from 5.14 to 5.16 for using
          T_AVREF_REFCOUNT_FIXED typemap.
        - Fix memory leaks in several methods which return ArrayRefs.
        - Switch from Smart::Args to Smart::Args::TypeTiny for less
          dependencies.

README.pod  view on Meta::CPAN

=head2 find_cost_parameter(data_set => $data_set, num_folds => $num_folds [, initial => -1.0] [, update => 0])

Deprecated. Use C<find_parameters> instead.

Shorthand alias for C<find_parameters> only works on C<cost> parameter.
Notice that C<loss_sensitivity> is affected too when C<update> is set.

=head2 find_parameters(data_set => $data_set, num_folds => $num_folds [, initial_cost => -1.0] [, initial_loss_sensitivity => -1.0] [, update => 0])

Finds the best parameters by N-fold cross validation. If C<initial_cost> or C<initial_loss_sensitivity> is a negative, the value is automatically calculated.
Works only for 3 solvers: C<'L2R_LR'>, C<'L2R_L2LOSS_SVC'> and C<'L2R_L2LOSS_SVR'>. Error will be thrown for otherwise.

When C<update> is set true, the instance is updated to use the found parameters. This behaviour is disabled by default.

Return value is an ArrayRef containing 3 values: found C<cost>, found C<loss_sensitivity> (only if solver is C<'L2R_L2LOSS_SVR'>) and mean accuracy of cross validation with the found parameters.

=head2 train(data_set => $data_set)

Executes training and returns a trained L<Algorithm::LibLinear::Model> instance.
C<data_set> is same as the C<cross_validation>'s.

lib/Algorithm/LibLinear.pm  view on Meta::CPAN

=head2 find_cost_parameter(data_set => $data_set, num_folds => $num_folds [, initial => -1.0] [, update => 0])

Deprecated. Use C<find_parameters> instead.

Shorthand alias for C<find_parameters> only works on C<cost> parameter.
Notice that C<loss_sensitivity> is affected too when C<update> is set.

=head2 find_parameters(data_set => $data_set, num_folds => $num_folds [, initial_cost => -1.0] [, initial_loss_sensitivity => -1.0] [, update => 0])

Finds the best parameters by N-fold cross validation. If C<initial_cost> or C<initial_loss_sensitivity> is a negative, the value is automatically calculated.
Works only for 3 solvers: C<'L2R_LR'>, C<'L2R_L2LOSS_SVC'> and C<'L2R_L2LOSS_SVR'>. Error will be thrown for otherwise.

When C<update> is set true, the instance is updated to use the found parameters. This behaviour is disabled by default.

Return value is an ArrayRef containing 3 values: found C<cost>, found C<loss_sensitivity> (only if solver is C<'L2R_L2LOSS_SVR'>) and mean accuracy of cross validation with the found parameters.

=head2 train(data_set => $data_set)

Executes training and returns a trained L<Algorithm::LibLinear::Model> instance.
C<data_set> is same as the C<cross_validation>'s.

t/Algorithm/LibLinear.t  view on Meta::CPAN

    my ($found_cost2) = @{
        $learner->find_cost_parameter(
            data_set => $data_set,
            initial => 1,
            num_folds => 5,
        )
    };
    is $found_cost, $found_cost2;
    is $found_loss_sensitivity, undef;

    throws_ok {
        my $learner = Algorithm::LibLinear->new(
            solver => 'L2R_L2LOSS_SVC_DUAL',
        );
        $learner->find_parameters(data_set => $data_set, num_folds => 5);
    } qr/unsupported/;

    my $classifier = $learner->train(data_set => $data_set);
    isa_ok $classifier, 'Algorithm::LibLinear::Model';

    my @labels = sort { $a <=> $b } @{ $classifier->class_labels };

t/Algorithm/LibLinear.t  view on Meta::CPAN

        $temp_file = $guard->filename;
        $classifier->save(filename => $temp_file);
        ok +(-s $temp_file > 0), 'Model object can be written in a file.';

        lives_ok {
            Algorithm::LibLinear::Model->load(filename => $guard->filename);
        } 'Model can be resumed from a file.';
    }

    # $temp_file is no longer exists here.
    throws_ok {
        Algorithm::LibLinear::Model->load(filename => $temp_file);
    } qr/Failed to load a model/i;
}

no_leaks_ok {
    my $learner = Algorithm::LibLinear->new(
        cost => 1,
        epsilon => 0.1,
        solver => 'L2R_L2LOSS_SVC_DUAL',
        weights => [

t/Algorithm/LibLinear/TrainingParameter.t  view on Meta::CPAN

    +{
      constructor_params => [
        loss_sensitivity => -1,
        solver => 'L2R_L2LOSS_SVR',
      ],
      error_pattern => qr/p < 0/,
    },
);
for my $case (@cases) {
    my $learner = Algorithm::LibLinear->new(@{ $case->{constructor_params} });
    throws_ok {
        $learner->train(data_set => $data_set);
    } $case->{error_pattern};
    throws_ok {
        $learner->cross_validation(data_set => $data_set, num_folds => 5);
    } $case->{error_pattern};
}

done_testing;



( run in 0.380 second using v1.01-cache-2.11-cpan-496ff517765 )