Skip to content

Commit

Permalink
MXNET-776 [Perl] Better documentation/bug fixes. (apache#12038)
Browse files Browse the repository at this point in the history
* MXNET-776
1) Several new metric classes.
2) Improved documentation.
3) Bugfixes.

* added links and fixed a typo.
  • Loading branch information
sergeykolychev committed Aug 8, 2018
1 parent 31abf34 commit 09a417f
Show file tree
Hide file tree
Showing 59 changed files with 1,211 additions and 382 deletions.
11 changes: 11 additions & 0 deletions docs/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ Select API: 

<hr>

## Perl Tutorials

* Getting Started
* [Machine learning in Perl](http://blogs.perl.org/users/sergey_kolychev/2017/02/machine-learning-in-perl.html)
* [Calculator and Robo-Shakespeare](http://blogs.perl.org/users/sergey_kolychev/2017/04/machine-learning-in-perl-part2-a-calculator-handwritten-digits-and-roboshakespeare.html)
* Gluon
* [DCGAN](http://blogs.perl.org/users/sergey_kolychev/2017/10/machine-learning-in-perl-part3-deep-convolutional-generative-adversarial-network.html)
* [Image classification and Style transfer](http://blogs.perl.org/users/sergey_kolychev/2018/07/machine-learning-in-perl-kyuubi-goes-to-a-modelzoo-during-the-starry-night.html)

<hr>

## Contributing Tutorials

We really appreciate contributions, and tutorials are a great way to share your knowledge and help the community. After you have followed [these steps](https://github.com/apache/incubator-mxnet/tree/master/example#contributing), please submit a pull request on Github.
Expand Down
6 changes: 6 additions & 0 deletions perl-package/AI-MXNet-Gluon-Contrib/Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Revision history for Perl extension AI::MXNet::Gluon::Contrib

1.32 Sun Jul 15 12:12:15 PDT 2018
- Missing POD fixes.

1.31 Sat Jul 14 08:33:21 PDT 2018
- Fixed CPAN indexing issue.

1.3 Tue Jul 10 21:19:13 PDT 2018
- Initial release

2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-Contrib/META.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
}
},
"release_status" : "stable",
"version" : "1.3"
"version" : "1.32"
}
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-Contrib/META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ no_index:
- inc
requires:
AI::MXNet: '1.31'
version: '1.3'
version: '1.32'
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-Contrib/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ my %WriteMakefileArgs = (
"AI::MXNet" => "1.31",
},
"TEST_REQUIRES" => {},
"VERSION" => "1.3",
"VERSION" => "1.32",
"test" => {
"TESTS" => "t/*.t"
}
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-Contrib/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This archive contains the distribution AI-MXNet-Gluon-Contrib,
version 1.3:
version 1.32:

Perl interface to MXNet Gluon Contib modules, a collection of supplemental Gluon blocks.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
# specific language governing permissions and limitations
# under the License.

package AI::MXNet::Gluon::Contrib;
use strict;
use warnings;
use AI::MXNet;
use AI::MXNet::Gluon::Contrib::NN::BasicLayers;
our $VERSION = '1.3';
our $VERSION = '1.32';
=head1 NAME
AI::MXNet::Gluon::Contrib - A collection of supplemental Gluon blocks.
=cut

1;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

use strict;
use warnings;
package AI::MXNet::Gluon::Contrib::NN::BasicLayers;

=head1 NAME
AI::MXNet::Gluon::Contrib::NN::BasicLayers - An additional collection of Gluon's building blocks.
=cut

use AI::MXNet::Function::Parameters;
package AI::MXNet::Gluon::NN::Concurrent;
use AI::MXNet::Gluon::Mouse;
Expand Down
3 changes: 3 additions & 0 deletions perl-package/AI-MXNet-Gluon-ModelZoo/Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Perl extension AI::MXNet::Gluon::ModelZoo

1.32 Sun Aug 5 14:25:31 PDT 2018
- Updated vgg16/19 models

1.3 Tue Jul 10 21:19:13 PDT 2018
- Initial release

2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-ModelZoo/META.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
}
},
"release_status" : "stable",
"version" : "1.3"
"version" : "1.32"
}
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-ModelZoo/META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ no_index:
requires:
AI::MXNet: '1.31'
AI::MXNet::Gluon::Contrib: '1.3'
version: '1.3'
version: '1.32'
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-ModelZoo/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ my %WriteMakefileArgs = (
"AI::MXNet::Gluon::Contrib" => "1.3"
},
"TEST_REQUIRES" => {},
"VERSION" => "1.3",
"VERSION" => "1.32",
"test" => {
"TESTS" => "t/*.t"
}
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet-Gluon-ModelZoo/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This archive contains the distribution AI-MXNet-Gluon-ModelZoo,
version 1.3:
version 1.32:

Perl interface to MXNet Gluon ModelZoo, a collection of pretrained machine learning models for computer vision.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use AI::MXNet::Gluon::ModelZoo::Vision;
use Exporter;
use base qw(Exporter);
@AI::MXNet::Gluon::ModelZoo::EXPORT_OK = qw(get_model);
our $VERSION = '1.3';
our $VERSION = '1.32';

=head1 NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ my %_model_sha1 = map { $_->[1] => $_->[0] } (
['ee79a8098a91fbe05b7a973fed2017a6117723a8', 'vgg11_bn'],
['6bc5de58a05a5e2e7f493e2d75a580d83efde38c', 'vgg13'],
['7d97a06c3c7a1aecc88b6e7385c2b373a249e95e', 'vgg13_bn'],
['649467530119c0f78c4859999e264e7bf14471a9', 'vgg16'],
['6b9dbe6194e5bfed30fd7a7c9a71f7e5a276cb14', 'vgg16_bn'],
['f713436691eee9a20d70a145ce0d53ed24bf7399', 'vgg19'],
['9730961c9cea43fd7eeefb00d792e386c45847d6', 'vgg19_bn']
['e660d4569ccb679ec68f1fd3cce07a387252a90a', 'vgg16'],
['7f01cf050d357127a73826045c245041b0df7363', 'vgg16_bn'],
['ad2f660d101905472b83590b59708b71ea22b2e5', 'vgg19'],
['f360b758e856f1074a85abd5fd873ed1d98297c3', 'vgg19_bn']
);

my $apache_repo_url = 'http://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/';
Expand Down
6 changes: 6 additions & 0 deletions perl-package/AI-MXNet/Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
Revision history for Perl extension AI::MXNet

1.32 Sun Aug 5 14:25:31 PDT 2018
- Several new metric classes
- Expanded documentation
- Bugfixes.

1.31 Tue Jul 10 21:19:13 PDT 2018
- Memory leak fix for Gluon API
- Added summary function for Gluon models
Expand Down
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/META.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"runtime" : {
"requires" : {
"AI::MXNetCAPI" : "1.3",
"AI::MXNetCAPI" : "1.32",
"AI::NNVMCAPI" : "1.3",
"Function::Parameters" : "1.0705",
"Hash::Ordered" : "0.012",
Expand All @@ -45,5 +45,5 @@
}
},
"release_status" : "stable",
"version" : "1.31"
"version" : "1.32"
}
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ no_index:
- t
- inc
requires:
AI::MXNetCAPI: '1.3'
AI::MXNetCAPI: '1.32'
AI::NNVMCAPI: '1.3'
Function::Parameters: '1.0705'
Hash::Ordered: '0.012'
GraphViz: '2.14'
Mouse: v2.1.0
PDL: '2.007'
PDL::CCS: '1.23.4'
version: '1.31'
version: '1.32'
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ my %WriteMakefileArgs = (
"GraphViz" => "2.14"
},
"TEST_REQUIRES" => {},
"VERSION" => "1.31",
"VERSION" => "1.32",
"test" => {
"TESTS" => "t/*.t"
}
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This archive contains the distribution AI-MXNet,
version 1.31:
version 1.32:

Perl interface to MXNet machine learning library

Expand Down
107 changes: 101 additions & 6 deletions perl-package/AI-MXNet/lib/AI/MXNet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use AI::MXNet::Gluon;
use AI::MXNet::NDArray::Sparse;
use AI::MXNet::Symbol::Sparse;
use AI::MXNet::Engine;
our $VERSION = '1.31';
our $VERSION = '1.32';

sub import
{
Expand Down Expand Up @@ -132,7 +132,7 @@ AI::MXNet - Perl interface to MXNet machine learning library
## Convolutional NN for recognizing hand-written digits in MNIST dataset
## It's considered "Hello, World" for Neural Networks
## For more info about the MNIST problem please refer to http://neuralnetworksanddeeplearning.com/chap1.html
## For more info about the MNIST problem please refer to L<http://neuralnetworksanddeeplearning.com/chap1.html>
use strict;
use warnings;
Expand Down Expand Up @@ -187,9 +187,104 @@ AI::MXNet - Perl interface to MXNet machine learning library
my $res = $mod->score($val_dataiter, mx->metric->create('acc'));
ok($res->{accuracy} > 0.8);
## Gluon MNIST example
my $net = nn->Sequential();
$net->name_scope(sub {
$net->add(nn->Dense(128, activation=>'relu'));
$net->add(nn->Dense(64, activation=>'relu'));
$net->add(nn->Dense(10));
});
$net->hybridize;
# data
sub transformer
{
my ($data, $label) = @_;
$data = $data->reshape([-1])->astype('float32')/255;
return ($data, $label);
}
my $train_data = gluon->data->DataLoader(
gluon->data->vision->MNIST('./data', train=>1, transform => \&transformer),
batch_size=>$batch_size, shuffle=>1, last_batch=>'discard'
);
## training
sub train
{
my ($epochs, $ctx) = @_;
# Collect all parameters from net and its children, then initialize them.
$net->initialize(mx->init->Xavier(magnitude=>2.24), ctx=>$ctx);
# Trainer is for updating parameters with gradient.
my $trainer = gluon->Trainer($net->collect_params(), 'sgd', { learning_rate => $lr, momentum => $momentum });
my $metric = mx->metric->Accuracy();
my $loss = gluon->loss->SoftmaxCrossEntropyLoss();
for my $epoch (0..$epochs-1)
{
# reset data iterator and metric at begining of epoch.
$metric->reset();
enumerate(sub {
my ($i, $d) = @_;
my ($data, $label) = @$d;
$data = $data->as_in_context($ctx);
$label = $label->as_in_context($ctx);
# Start recording computation graph with record() section.
# Recorded graphs can then be differentiated with backward.
my $output;
autograd->record(sub {
$output = $net->($data);
my $L = $loss->($output, $label);
$L->backward;
});
# take a gradient step with batch_size equal to data.shape[0]
$trainer->step($data->shape->[0]);
# update metric at last.
$metric->update([$label], [$output]);
if($i % $log_interval == 0 and $i > 0)
{
my ($name, $acc) = $metric->get();
print "[Epoch $epoch Batch $i] Training: $name=$acc\n";
}
}, \@{ $train_data });
my ($name, $acc) = $metric->get();
print "[Epoch $epoch] Training: $name=$acc\n";
my ($val_name, $val_acc) = test($ctx);
print "[Epoch $epoch] Validation: $val_name=$val_acc\n"
}
$net->save_parameters('mnist.params');
}
train($epochs, $cuda ? mx->gpu(0) : mx->cpu);
=head1 DESCRIPTION
Perl interface to MXNet machine learning library.
MXNet supports the Perl programming language.
The MXNet Perl package brings flexible and efficient GPU computing and
state-of-art deep learning to Perl.
It enables you to write seamless tensor/matrix computation with multiple GPUs in Perl.
It also lets you construct and customize the state-of-art deep learning models in Perl,
and apply them to tasks, such as image classification and data science challenges.
One important thing to internalize is that Perl interface is written to be as close as possible to the Python’s API,
so most, if not all of Python’s documentation and examples should just work in Perl after making few changes
in order to make the code a bit more Perlish. In nutshell just add $ sigils and replace . = \n with -> => ;
and in 99% of cases that’s all that is needed there.
In addition please refer to very detailed L<MXNet Python API Documentation|http://mxnet.io/api/python/index.html>.
AI::MXNet supports new imperative PyTorch like Gluon MXNet interface.
Please get acquainted with this new interface at L<Deep Learning - The Straight Dope|https://gluon.mxnet.io/>.
For specific Perl Gluon usage please refer to Perl examples and tests directories on github,
but be assured that the Python and Perl usage are extremely close in order to make the use
of the Python Gluon docs and examples as easy as possible.
AI::MXNet is seamlessly glued with L<PDL|https://metacpan.org/pod/PDL>, the C++ level state can be easily initialized from PDL
and the results can be transferred to PDL objects in order to allow you to use all the glory and power of the PDL!
=head1 BUGS AND INCOMPATIBILITIES
Expand All @@ -198,16 +293,16 @@ AI::MXNet - Perl interface to MXNet machine learning library
=head1 SEE ALSO
http://mxnet.io/
https://github.com/dmlc/mxnet/tree/master/perl-package
Function::Parameters, Mouse
L<http://mxnet.io/>
L<https://github.com/dmlc/mxnet/tree/master/perl-package>
L<Function::Parameters|https://metacpan.org/pod/Function::Parameters>, L<Mouse|https://metacpan.org/pod/Mouse>
=head1 AUTHOR
Sergey Kolychev, <sergeykolychev.github@gmail.com>
=head1 COPYRIGHT & LICENSE
This library is licensed under Apache 2.0 license https://www.apache.org/licenses/LICENSE-2.0
This library is licensed under Apache 2.0 license L<https://www.apache.org/licenses/LICENSE-2.0>
=cut
Loading

0 comments on commit 09a417f

Please sign in to comment.