File Coverage

blib/lib/Net/MachineLearning/Sample.pm
Criterion Covered Total %
statement 15 17 88.2
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 23 91.3


line stmt bran cond sub pod time code
1             package Net::MachineLearning::Sample;
2              
3 1     1   13232 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         15  
5 1     1   3 use warnings;
  1         4  
  1         25  
6 1     1   452 use utf8;
  1         7  
  1         3  
7 1     1   516 use JSON;
  1         9735  
  1         4  
8 1     1   370 use GD;
  0            
  0            
9              
10             =encoding utf8
11              
12             =head1 NAME
13              
14             Net::MachineLearning::Sample - how machine learning works by demo
15              
16             =head1 VERSION
17              
18             Version 0.01
19              
20             =cut
21              
22             our $VERSION = '0.01';
23              
24              
25             =head1 SYNOPSIS
26              
27             该模块是一个非常粗浅的案例,仅仅展示机器学习如何运作。
28              
29             在库文件的同一目录,有0-9共10张数字图片,每张都是10x10像素的灰度PNG格式。
30              
31             模块每次随机读取一个图片,将每个像素的值进行变换后,与到目标数字相似的10项权重分别相乘。
32              
33             最后将每一项相乘的结果加权汇总,得到图片与目标数字的相似概率,数字越大相似度越高。
34              
35             权重参数是一个JSON文件,位于库文件同一目录下的weights.json,通过get_weights.pl这个脚本产生。
36              
37             正常来说,权重参数是通过大量的带标注图片,训练出来的,而不是手工调参的结果。
38              
39             运行方法:
40              
41             1. 首先通过cpanm安装该模块:
42              
43             $ sudo cpanm Net::MachineLearning::Sample
44              
45             2. 然后通过命令行运行即可:
46              
47             $ perl -MNet::MachineLearning::Sample -e 'Net::MachineLearning::Sample->new->run'
48             input numeric image: 8
49             my best guess: 8
50              
51             3. 或者在perl程序里调用:
52              
53             use Net::MachineLearning::Sample;
54             my $ml = Net::MachineLearning::Sample->new;
55             $ml->run;
56              
57             =head1 SUBROUTINES/METHODS
58              
59             =head2 new
60              
61             new the object.
62              
63             =cut
64              
65             sub new {
66             my $class = shift;
67             bless {},$class;
68             }
69              
70             =head2 run
71              
72             run the model.
73              
74             =cut
75              
76             sub run {
77              
78             my $module_dir = $INC{'Net/MachineLearning/Sample.pm'};
79             $module_dir =~ s/Sample\.pm$//;
80              
81             my $ix = int rand(10);
82             my %scores;
83              
84             open my $fd,"$module_dir/weights.json" or die $!;
85             my $json = <$fd>;
86             close $fd;
87              
88             my $wht = from_json($json);
89              
90             my $myImage = newFromPng GD::Image("$module_dir/gray-$ix.png",0);
91             my $pointer = 0;
92              
93             for my $column (0..9) {
94             for my $row (0..9) {
95             my $index = $myImage->getPixel($row,$column);
96              
97             for my $num (0..9) {
98             my $weight = $wht->{$num}->[$pointer];
99             $scores{$num} += $weight * (255 - $index);
100             }
101              
102             $pointer ++;
103             }
104             }
105              
106             my $bestValue;
107              
108             for (sort {$scores{$b} <=> $scores{$a} } keys %scores) {
109             $bestValue = $_;
110             last;
111             }
112              
113             print "input numeric image: $ix\n";
114             print "my best guess: $bestValue\n";
115             }
116              
117              
118             =head1 AUTHOR
119              
120             Ken Peng, C<< >>
121              
122             =head1 BUGS
123              
124             Please report any bugs or feature requests to C, or through
125             the web interface at L. I will be notified, and then you'll
126             automatically be notified of progress on your bug as I make changes.
127              
128              
129              
130              
131             =head1 SUPPORT
132              
133             You can find documentation for this module with the perldoc command.
134              
135             perldoc Net::MachineLearning::Sample
136              
137              
138             You can also look for information at:
139              
140             =over 4
141              
142             =item * RT: CPAN's request tracker (report bugs here)
143              
144             L
145              
146             =item * AnnoCPAN: Annotated CPAN documentation
147              
148             L
149              
150             =item * CPAN Ratings
151              
152             L
153              
154             =item * Search CPAN
155              
156             L
157              
158             =back
159              
160              
161             =head1 ACKNOWLEDGEMENTS
162              
163              
164             =head1 LICENSE AND COPYRIGHT
165              
166             Copyright 2017 Ken Peng.
167              
168             This program is free software; you can redistribute it and/or modify it
169             under the terms of the the Artistic License (2.0). You may obtain a
170             copy of the full license at:
171              
172             L
173              
174             Any use, modification, and distribution of the Standard or Modified
175             Versions is governed by this Artistic License. By using, modifying or
176             distributing the Package, you accept this license. Do not use, modify,
177             or distribute the Package, if you do not accept this license.
178              
179             If your Modified Version has been derived from a Modified Version made
180             by someone other than you, you are nevertheless required to ensure that
181             your Modified Version complies with the requirements of this license.
182              
183             This license does not grant you the right to use any trademark, service
184             mark, tradename, or logo of the Copyright Holder.
185              
186             This license includes the non-exclusive, worldwide, free-of-charge
187             patent license to make, have made, use, offer to sell, sell, import and
188             otherwise transfer the Package with respect to any patent claims
189             licensable by the Copyright Holder that are necessarily infringed by the
190             Package. If you institute patent litigation (including a cross-claim or
191             counterclaim) against any party alleging that the Package constitutes
192             direct or contributory patent infringement, then this Artistic License
193             to you shall terminate on the date that such litigation is filed.
194              
195             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
196             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
197             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
198             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
199             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
200             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
201             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
202             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203              
204              
205             =cut
206              
207             1; # End of Net::MachineLearning::Sample