Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-haibin-lin committed Aug 9, 2017
2 parents bc33101 + f674bc4 commit 8040953
Show file tree
Hide file tree
Showing 28 changed files with 618 additions and 42 deletions.
58 changes: 58 additions & 0 deletions docs/api/python/autograd.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,64 @@
.. warning:: This package is currently experimental and may change in the near future.
```

## Overview

The `autograd` package enables automatic
differentiation of NDArray operations.
In machine learning applications,
`autograd` is often used to calculate the gradients
of loss functions with respect to parameters.


### Record vs Pause

`autograd` records computation history on the fly to calculate gradients later.
This is only enabled inside a `with autograd.record():` block.
A `with auto_grad.pause()` block can be used inside a `record()` block
to temporarily disable recording.

To compute gradient with respect to an `NDArray` `x`, first call `x.attach_grad()`
to allocate space for the gradient. Then, start a `with autograd.record()` block,
and do some computation. Finally, call `backward()` on the result:

```python
>>> x = mx.nd.array([1,2,3,4])
>>> x.attach_grad()
>>> with mx.autograd.record():
... y = x * x + 1
>>> print(x.grad)
[ 2. 4. 6. 8.]
<NDArray 4 @cpu(0)>
```


## Train mode and Predict Mode

Some operators (Dropout, BatchNorm, etc) behave differently in
when training and when making predictions.
This can be controled with `train_mode` and `predict_mode` scope.

By default, MXNet is in `predict_mode`.
A `with autograd.record()` block by default turns on `train_mode`
(equivalent to ``with autograd.record(train_mode=True)``).
To compute a gradient in prediction mode (as when generating adversarial examples),
call record with `train_mode=False` and then call `backward(train_mode=False)`

Although training usually coincides with recording,
this isn't always the case.
To control *training* vs *predict_mode* without changing
*recording* vs *not recording*,
Use a `with autograd.train_mode():`
or `with autograd.predict_mode():` block.

Detailed tutorials are available in Part 1 of
[the MXNet gluon book](http://gluon.mxnet.io/).






<script type="text/javascript" src='../../_static/js/auto_module_index.js'></script>

## Autograd
Expand Down
3 changes: 3 additions & 0 deletions perl-package/AI-MXNet/Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Perl extension AI::MXNet

1.0102 Sun Aug 6 16:55:08 PDT 2017
- bugfixes in Image.pm, updated tests, added PearsonCorrelation metric, added Convolutional RNN modules.

1.0101 Sun Jul 2 17:16:01 PDT 2017
- reworked CachedOp, two new optimizers, auto module reshape, using strings to index the kvstore.

Expand Down
1 change: 0 additions & 1 deletion perl-package/AI-MXNet/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ examples/cudnn_lstm_bucketing.pl
Makefile.PL
Changes
META.json
t/test_autograd.t
t/test_recordio.t
t/test_random.t
t/test_init.t
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.0101",
"AI::MXNetCAPI" : "1.0102",
"AI::NNVMCAPI" : "1.01",
"Function::Parameters" : "1.0705",
"GraphViz" : "2.14",
Expand All @@ -43,5 +43,5 @@
}
},
"release_status" : "stable",
"version" : "1.0101"
"version" : "1.0102"
}
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,10 +17,10 @@ no_index:
- t
- inc
requires:
AI::MXNetCAPI: '1.0101'
AI::MXNetCAPI: '1.0102'
AI::NNVMCAPI: '1.01'
Function::Parameters: '1.0705'
GraphViz: '2.14'
Mouse: v2.1.0
PDL: '2.007'
version: '1.0101'
version: '1.0102'
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ my %WriteMakefileArgs = (
"LICENSE" => "apache_2_0",
"NAME" => "AI::MXNet",
"PREREQ_PM" => {
"AI::MXNetCAPI" => "1.0101",
"AI::MXNetCAPI" => "1.0102",
"AI::NNVMCAPI" => "1.01",
"Function::Parameters" => "1.0705",
"Mouse" => "v2.1.0",
Expand All @@ -35,7 +35,7 @@ my %WriteMakefileArgs = (


my %FallbackPrereqs = (
"AI::MXNetCAPI" => "1.0101",
"AI::MXNetCAPI" => "1.0102",
"AI::NNVMCAPI" => "1.01",
"Function::Parameters" => "1.0705",
"Mouse" => "v2.1.0",
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.0101:
version 1.0102:

Perl interface to MXNet machine learning library

Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use AI::MXNet::Image;
use AI::MXNet::Contrib;
use AI::MXNet::Contrib::AutoGrad;
use AI::MXNet::CachedOp;
our $VERSION = '1.0101';
our $VERSION = '1.0102';

sub import
{
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use strict;
use warnings;
use PDL;
use PDL::Types qw();
use AI::MXNetCAPI 1.0101;
use AI::MXNetCAPI 1.0102;
use AI::NNVMCAPI 1.01;
use AI::MXNet::Types;
use Time::HiRes;
Expand Down
8 changes: 6 additions & 2 deletions perl-package/AI-MXNet/lib/AI/MXNet/Image.pm
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ sub BUILD
{
chomp($line);
my @line = split(/\t/, $line);
my $label = AI::MXNet::NDArray->array([@line[1..@line-1]]);
my $label = AI::MXNet::NDArray->array([@line[1..@line-2]]);
my $key = $line[0];
$imglist{$key} = [$label, $line[-1]];
push @imgkeys, $key;
Expand Down Expand Up @@ -838,6 +838,10 @@ sub BUILD
{
$self->aug_list(AI::MXNet::Image->CreateAugmenter(data_shape => $self->data_shape, %{ $self->kwargs//{} }));
}
else
{
$self->aug_list([]);
}
$self->cur(0);
$self->reset();
}
Expand Down Expand Up @@ -877,7 +881,7 @@ method next_sample()
}
else
{
my ($label, $fname) = $self->imglist->{$idx};
my ($label, $fname) = @{ $self->imglist->{$idx} };
if(not defined $self->imgrec)
{
open(F, $self->path_root . "/$fname") or confess("can't open $fname $!");
Expand Down
50 changes: 50 additions & 0 deletions perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,55 @@ method update(ArrayRef[AI::MXNet::NDArray] $labels, ArrayRef[AI::MXNet::NDArray]
}, $labels, $preds);
}

package AI::MXNet::PearsonCorrelation;
use Mouse;
use AI::MXNet::Base;
extends 'AI::MXNet::EvalMetric';
has '+name' => (default => 'pearson-correlation');

=head1 NAME
AI::MXNet::PearsonCorrelation
=cut

=head1 DESCRIPTION
Computes Pearson correlation.
Parameters
----------
name : str
Name of this metric instance for display.
Examples
--------
>>> $predicts = [mx->nd->array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])]
>>> $labels = [mx->nd->array([[1, 0], [0, 1], [0, 1]])]
>>> $pr = mx->metric->PearsonCorrelation()
>>> $pr->update($labels, $predicts)
>>> print pr->get()
('pearson-correlation', '0.421637061887229')
=cut

method update(ArrayRef[AI::MXNet::NDArray] $labels, ArrayRef[AI::MXNet::NDArray] $preds)
{
AI::MXNet::Metric::check_label_shapes($labels, $preds);
zip(sub {
my ($label, $pred) = @_;
AI::MXNet::Metric::check_label_shapes($label, $pred);
$label = $label->aspdl->flat;
$pred = $pred->aspdl->flat;
my ($label_mean, $label_stdv) = ($label->stats)[0, 6];
my ($pred_mean, $pred_stdv) = ($pred->stats)[0, 6];
$self->sum_metric(
$self->sum_metric
+
((($label-$label_mean)*($pred-$pred_mean))->sum/$label->nelem)/(($label_stdv*$pred_stdv)->at(0))
);
$self->num_inst($self->num_inst + 1);
}, $labels, $preds);
}

=head1 DESCRIPTION
Custom evaluation metric that takes a sub ref.
Expand Down Expand Up @@ -574,6 +623,7 @@ my %metrics = qw/
top_k_accuracy AI::MXNet::TopKAccuracy
Perplexity AI::MXNet::Perplexity
perplexity AI::MXNet::Perplexity
pearsonr AI::MXNet::PearsonCorrelation
/;

method create(Metric|ArrayRef[Metric] $metric, %kwargs)
Expand Down
8 changes: 0 additions & 8 deletions perl-package/AI-MXNet/lib/AI/MXNet/Module.pm
Original file line number Diff line number Diff line change
Expand Up @@ -796,14 +796,6 @@ method forward(
)
{
assert($self->binded and $self->params_initialized);
# If starting to do the inference, force rebind the module.
if($self->label_shapes and not $data_batch->label)
{
confess(
"If you are trying to do inference, rebind module ".
"with 'force_rebind=True' and 'for_training=False'"
);
}

my @curr_data_shapes = map { $_->shape } @{ $self->data_shapes };
my @new_data_shapes = map { $_->shape } @{ $data_batch->data };
Expand Down
3 changes: 3 additions & 0 deletions perl-package/AI-MXNet/lib/AI/MXNet/RNN.pm
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ method SequentialRNNCell(@args) { AI::MXNet::RNN::SequentialCell->new(@args) }
method BidirectionalCell(@args) { AI::MXNet::RNN::BidirectionalCell->new(@args) }
method DropoutCell(@args) { AI::MXNet::RNN::DropoutCell->new(@args) }
method ZoneoutCell(@args) { AI::MXNet::RNN::ZoneoutCell->new(@args) }
method ConvRNNCell(@args) { AI::MXNet::RNN::ConvCell->new(@args) }
method ConvLSTMCell(@args) { AI::MXNet::RNN::ConvLSTMCell->new(@args) }
method ConvGRUCell(@args) { AI::MXNet::RNN::ConvGRUCell->new(@args) }
method ResidualCell(@args) { AI::MXNet::RNN::ResidualCell->new(@args) }
method encode_sentences(@args) { AI::MXNet::RNN::IO->encode_sentences(@args) }
method BucketSentenceIter(@args)
Expand Down
Loading

0 comments on commit 8040953

Please sign in to comment.