AI-MaxEntropy
view release on metacpan or search on metacpan
t/02-learn_by_lbfgs.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 5;
use Test::Number::Delta within => 1e-5;
my $__;
sub NAME { $__ = shift };
###
NAME 'Load the module';
BEGIN { use_ok 'AI::MaxEntropy' }
my $me = AI::MaxEntropy->new(smoother => {});
$me->see(['round', 'smooth', 'red'] => 'apple' => 2);
$me->see(['long', 'smooth', 'yellow'] => 'banana' => 3);
$me->cut(0);
$me->_cache;
###
NAME 'Negative log likelihood calculation (lambda = all 0)';
my ($f, $g) = AI::MaxEntropy::_neg_log_likelihood(
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], undef, $me
);
delta_ok
[
$f,
$g
],
[
- (2 * log(0.5) + 3 * log(0.5)),
[
- ((1 - 0.5) * 2 + 0 * 3),
- ((1 - 0.5) * 2 + (0 - 0.5) * 3),
- ((1 - 0.5) * 2 + 0 * 3),
- (0 * 2 + (0 - 0.5) * 3),
- (0 * 2 + (0 - 0.5) * 3),
- ((0 - 0.5) * 2 + 0 * 3),
- ((0 - 0.5) * 2 + (1 -0.5) * 3),
- ((0 - 0.5) * 2 + 0 * 3),
- (0 * 2 + (1 - 0.5) * 3),
- (0 * 2 + (1 - 0.5) * 3)
]
],
$__;
###
NAME 'Negative log likelihood calculation (lambda = random .1 and 0)';
($f, $g) = AI::MaxEntropy::_neg_log_likelihood(
[.1, .1, 0, 0, 0, .1, .1, 0, 0, .1], undef, $me
);
delta_ok
[
$f,
$g
],
[
- (log(exp(.1) / (2 * exp(.1))) * 2 +
log(exp(.2) / (exp(.1) + exp(.2))) * 3),
[
- ((1 - exp(.1) / (2 * exp(.1))) * 2 + 0 * 3),
- ((1 - exp(.1) / (2 * exp(.1))) * 2 +
(0 - exp(.1) / (exp(.1) + exp(.2))) * 3),
- ((1 - exp(.1) / (2 * exp(.1))) * 2 + 0 * 3),
( run in 2.156 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )