Net-MachineLearning-Sample

 view release on metacpan or  search on metacpan

lib/Net/MachineLearning/Sample.pm  view on Meta::CPAN


模块每次随机读取一个图片,将每个像素的值进行变换后,与到目标数字相似的10项权重分别相乘。

最后将每一项相乘的结果加权汇总,得到图片与目标数字的相似概率,数字越大相似度越高。

权重参数是一个JSON文件,位于库文件同一目录下的weights.json,通过get_weights.pl这个脚本产生。

正常来说,权重参数是通过大量的带标注图片,训练出来的,而不是手工调参的结果。

运行方法:

1. 首先通过cpanm安装该模块:

    $ sudo cpanm Net::MachineLearning::Sample

2. 然后通过命令行运行即可:

    $ perl -MNet::MachineLearning::Sample -e 'Net::MachineLearning::Sample->new->run'
	input numeric image: 8
	my best guess: 8

3. 或者在perl程序里调用:

    use Net::MachineLearning::Sample;
    my $ml = Net::MachineLearning::Sample->new;
    $ml->run;

=head1 SUBROUTINES/METHODS

=head2 new

new the object.

=cut

sub new {
    my $class = shift;
    bless {},$class;
}

=head2 run

run the model.

=cut

sub run {

    my $module_dir = $INC{'Net/MachineLearning/Sample.pm'};
    $module_dir =~ s/Sample\.pm$//;

    my $ix = int rand(10);
    my %scores;

    open my $fd,"$module_dir/weights.json" or die $!;
    my $json = <$fd>;
    close $fd;

    my $wht = from_json($json);

    my $myImage = newFromPng GD::Image("$module_dir/gray-$ix.png",0);
    my $pointer = 0;

    for my $column (0..9) {
        for my $row (0..9) {
            my $index = $myImage->getPixel($row,$column);

            for my $num (0..9) {
                my $weight = $wht->{$num}->[$pointer];
                $scores{$num}  += $weight * (255 - $index);
            }

            $pointer ++;
        }
    }

	my $bestValue;

	for (sort {$scores{$b} <=> $scores{$a} } keys %scores) {
		$bestValue = $_;
		last;
	}

    print "input numeric image: $ix\n";
	print "my best guess: $bestValue\n";
}


=head1 AUTHOR

Ken Peng, C<< <yhpeng at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-net-machinelearning-sample at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-MachineLearning-Sample>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Net::MachineLearning::Sample


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-MachineLearning-Sample>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Net-MachineLearning-Sample>

=item * CPAN Ratings



( run in 1.413 second using v1.01-cache-2.11-cpan-5b529ec07f3 )