blib/lib/Algorithm/LibLinear.pm

filter: Statementstmt 96.2% 77 / 80 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 22 / 22 Podpod 83.3% 10 / 12 totaltotal 92.6% 114 / 123 CC 5 SCAR 3.3
CC
5
Cov
94%
new
3.0
cross_validation
2.1
find_parameters
2.0
CRAP 1.4
« blib/lib/Algorithm/LibLinear/Model.pm ↑ Summary src/liblinear/linear.cpp »
1 package Algorithm::LibLinear;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Algorithm::LibLinear::DataSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Algorithm::LibLinear::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use List::MoreUtils qw/none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Smart::Args::TypeTiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Types::Standard qw/ArrayRef Bool ClassName Dict Enum InstanceOf Int Num/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '0.26';
14  
15 4 XSLoader::load(__PACKAGE__, $VERSION);
16  
17 4 my $ClassWeight = Dict[ label => Int, weight => Num ];
18  
19 4 my $InstanceOfPackage = InstanceOf[__PACKAGE__];
20  
21 4 my $SolverDescriptor = Enum[
22 qw/L2R_LR L2R_L2LOSS_SVC_DUAL L2R_L2LOSS_SVC L2R_L1LOSS_SVC_DUAL MCSVM_CS
23 L1R_L2LOSS_SVC L1R_LR L2R_LR_DUAL L2R_L2LOSS_SVR L2R_L2LOSS_SVR_DUAL
24 L2R_L1LOSS_SVR_DUAL ONECLASS_SVM/,
25 ];
26  
27 4 my %default_eps = (
28 L2R_LR => 0.01,
29 L2R_L2LOSS_SVC_DUAL => 0.1,
30 L2R_L2LOSS_SVC => 0.01,
31 L2R_L1LOSS_SVC_DUAL => 0.1,
32 MCSVM_CS => 0.1,
33 L1R_L2LOSS_SVC => 0.01,
34 L1R_LR => 0.01,
35 L2R_LR_DUAL => 0.1,
36 L2R_L2LOSS_SVR => 0.0001,
37 L2R_L2LOSS_SVR_DUAL => 0.1,
38 L2R_L1LOSS_SVR_DUAL => 0.1,
39 ONECLASS_SVM => 0.01,
40 );
41  
42 4 my %solvers = (
43 L2R_LR => 0,
44 L2R_L2LOSS_SVC_DUAL => 1,
45 L2R_L2LOSS_SVC => 2,
46 L2R_L1LOSS_SVC_DUAL => 3,
47 MCSVM_CS => 4,
48 L1R_L2LOSS_SVC => 5,
49 L1R_LR => 6,
50 L2R_LR_DUAL => 7,
51 L2R_L2LOSS_SVR => 11,
52 L2R_L2LOSS_SVR_DUAL => 12,
53 L2R_L1LOSS_SVR_DUAL => 13,
54 ONECLASS_SVM => 21,
55 );
56  
57 9 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
58 9 args
59 my $class => ClassName,
60 my $bias => +{ isa => Num, default => -1.0, },
61 my $cost => +{ isa => Num, default => 1, },
62 my $epsilon => +{ isa => Num, optional => 1, },
63 my $loss_sensitivity => +{ isa => Num, default => 0.1, },
64 my $nu => +{ isa => Num, default => 0.5, },
65 my $regularize_bias => +{ isa => Bool, default => 1, },
66 my $solver => +{
67 isa => $SolverDescriptor,
68 default => 'L2R_L2LOSS_SVC_DUAL',
69 },
70 my $weights => +{ isa => ArrayRef[$ClassWeight], default => [], };
71  
72 9 ▶ $epsilon //= $default_eps{$solver};
Condition66%2/3
$epsilon //= $default_eps{$solver}
l!l&&r!l&&!r
540
MC/DC0%0/2
$epsilon //= $default_eps{$solver}
$epsilon $default_eps{$solver}
Truth table66%2/3
A //= B
A= $epsilonB= $default_eps{$solver}
ABresult
1X1
011
000
73 9 my (@weight_labels, @weights);
74 9 for my $weight (@$weights) {
75 8 push @weight_labels, $weight->{label};
76 8 push @weights, $weight->{weight};
77 }
78 my $training_parameter = Algorithm::LibLinear::TrainingParameter->new(
79 9 $solvers{$solver},
80 $epsilon,
81 $cost,
82 \@weight_labels,
83 \@weights,
84 $loss_sensitivity,
85 $nu,
86 $regularize_bias,
87 );
88 9 bless +{
89 bias => $bias,
90 training_parameter => $training_parameter,
91 } => $class;
92 }
93  
94 12 ▶sub bias { $_[0]->{bias} }
Subroutine bias: called CC=1 SCAR=0.0
Pod documented
95  
96 2 ▶sub cost { $_[0]->training_parameter->cost }
Subroutine cost: called CC=1 SCAR=0.0
Pod documented
97  
98 4 ▶sub cross_validation {
Subroutine cross_validation: called CC=2 SCAR=7.4
Pod documented
99 4 args
100 my $self => $InstanceOfPackage,
101 my $data_set => InstanceOf['Algorithm::LibLinear::DataSet'],
102 my $num_folds => Int;
103  
104 4 my $targets = $self->training_parameter->cross_validation(
105 $data_set->as_problem(bias => $self->bias),
106 $num_folds,
107 );
108 1 my @labels = map { $_->{label} } @{ $data_set->as_arrayref };
109 1 ▶ if ($self->is_regression_solver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_regression_solver) { } 0 1 1
110 my $total_square_error = sum map {
111 0 ($targets->[$_] - $labels[$_]) ** 2;
112 } (0 .. $data_set->size - 1);
113 # Returns mean squared error.
114 # TODO: Squared correlation coefficient (see train.c in LIBLINEAR.)
115 0 return $total_square_error / $data_set->size;
116 } else {
117 my $num_corrects =
118 1 grep { $targets->[$_] == $labels[$_] } (0 .. $data_set->size - 1);
119 1 return $num_corrects / $data_set->size;
120 }
121 }
122  
123 2 ▶sub epsilon { $_[0]->training_parameter->epsilon }
Subroutine epsilon: called CC=1 SCAR=0.0
Pod documented
124  
125 1 ▶sub find_cost_parameter {
Subroutine find_cost_parameter: called CC=1 SCAR=0.0
Pod documented
126 1 args
127 my $self => $InstanceOfPackage,
128 my $data_set => InstanceOf['Algorithm::LibLinear::DataSet'],
129 my $initial => +{ isa => Num, default => -1.0 },
130 my $num_folds => Int,
131 my $update => +{ isa => Bool, default => 0, };
132  
133 my ($cost, undef, $accuracy) = @{
134 1 $self->find_parameters(
135 data_set => $data_set,
136 initial_cost => $initial,
137 initial_loss_sensitivity => -1.0,
138 num_folds => $num_folds,
139 update => $update,
140 )
141 };
142 1 return [ $cost, $accuracy ];
143 }
144  
145 3 ▶sub find_parameters {
Subroutine find_parameters: called CC=2 SCAR=6.9
Pod documented
146 3 args
147 my $self => $InstanceOfPackage,
148 my $data_set => InstanceOf['Algorithm::LibLinear::DataSet'],
149 my $initial_cost => +{ isa => Num, default => -1.0, },
150 my $initial_loss_sensitivity => +{ isa => Num, default => -1.0, },
151 my $num_folds => Int,
152 my $update => +{ isa => Bool, default => 0, };
153  
154 3 my $solver_type = $self->training_parameter->solver_type;
155 3 ▶ if (none { $solver_type == $_ } @solvers{
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $solver_type == $_; } , @solvers{"L2R_LR", "L2R_L2LOSS_SVC", "L2R_L2LOSS_SVR"})) 1 2 3
156 qw/L2R_LR L2R_L2LOSS_SVC L2R_L2LOSS_SVR/,
157 }) {
158 1 my %solver_types = reverse %solvers;
159 1 my $solver = $solver_types{$solver_type};
160 1 Carp::croak('Operation is unsupported with this solver: ', $solver);
161 }
162 $self->training_parameter->find_parameters(
163 2 $data_set->as_problem(bias => $self->bias),
164 $num_folds,
165 $initial_cost,
166 $initial_loss_sensitivity,
167 $update,
168 );
169 }
170  
171 3 ▶sub is_regression_solver { $_[0]->training_parameter->is_regression_solver }
Subroutine is_regression_solver: called CC=1 SCAR=0.0
Pod undocumented
172  
173 1 ▶sub loss_sensitivity { $_[0]->training_parameter->loss_sensitivity }
Subroutine loss_sensitivity: called CC=1 SCAR=0.0
Pod documented
174  
175 27 ▶sub training_parameter { $_[0]->{training_parameter} }
Subroutine training_parameter: called CC=1 SCAR=0.0
Pod undocumented
176  
177 6 ▶sub train {
Subroutine train: called CC=1 SCAR=0.0
Pod documented
178 6 args
179 my $self => $InstanceOfPackage,
180 my $data_set => InstanceOf['Algorithm::LibLinear::DataSet'];
181  
182 6 my $raw_model = Algorithm::LibLinear::Model::Raw->train(
183 $data_set->as_problem(bias => $self->bias),
184 $self->training_parameter,
185 );
186 3 Algorithm::LibLinear::Model->new(raw_model => $raw_model);
187 }
188  
189 2 ▶sub weights {
Subroutine weights: called CC=1 SCAR=0.0
Pod documented
190 2 args
191 my $self => $InstanceOfPackage;
192  
193 2 my $labels = $self->training_parameter->weight_labels;
194 2 my $weights = $self->training_parameter->weights;
195 [ map {
196 2 +{ label => $labels->[$_], weight => $weights->[$_], }
197 } 0 .. $#$labels ];
198 }
199  
200 4 1;
201 __END__
« blib/lib/Algorithm/LibLinear/Model.pm ↑ Summary src/liblinear/linear.cpp »