Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MXNet backend #83

Merged
merged 10 commits into from Feb 12, 2018
Merged

Add MXNet backend #83

merged 10 commits into from Feb 12, 2018

Conversation

matthewfeickert
Copy link
Member

@matthewfeickert matthewfeickert commented Feb 10, 2018

Add in a backend for MXNet as described in Issue #80.

TODO:

  • Finish the mxnet_backend.py methods
  • Get all tests passing
  • Refactor methods to use as few dependencies beyond mxnet as possible
  • Add Sphinx compliant doc strings
  • Add an example Jupyter Notebook
  • Add Binder support

This was made easier by the "PyTorch to MXNet" README from the online book "Deep Learning - The Straight Dope"

Add a placeholder file for the MXNet backend and partially fill it in.

In addition also add mxnet to packages to pip install and add
mxnet_backend to the list of backends to test.
@matthewfeickert matthewfeickert self-assigned this Feb 10, 2018
"""Backend for MXNet"""

def __init__(self, **kwargs):
self.session = kwargs.get('session')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like MXNet does not have a concept of a session (at least I can't find a reference to it. So I think this can be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was the result of some sloppy copypasta and me not checking things late at night. I'll fix that up when I get back to this after finishing work tonight.

@matthewfeickert matthewfeickert changed the title [WIP] Add placeholder MXNet backend [WIP] Add MXNet backend Feb 10, 2018
Other methods are added as well, but these are the two that were the
most important
@matthewfeickert
Copy link
Member Author

matthewfeickert commented Feb 11, 2018

Sorry this is taking a bit of time. I have things mostly done but I was getting a bit confused with the way that MXNet handles things like this: https://discuss.mxnet.io/t/ndarray-conversion-from-other-array-types/258

I can probably push a working version later tonight once I finish some analysis work, but I canceled all the builds for the last push.


def poisson(self, n, lam):
pass
return self.normal(n, lam, self.sqrt(lam))

def normal(self, x, mu, sigma):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but for poisson, let's stick for now to a gaussian approximation with mu = lambda , sigma = sqrt(lambda) since we need continuous poissons

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe I'm misunderstanding what you mean, but we want the pdf of the distribution evaluated at x, yes? The MXNet distribution generators return sampling of the distributions given the parameters you pass, so mx.nd.random.normal() is akin to np.random.normal(), and we want something like scipy.stats.norm.pdf(x), right? I might have missed another API though.

Yeah, I follow you RE: the Poissons.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, i didn't look to closely at the api seems like it more close to np.random stan scipy.stats.. sorry about that!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! Just wanted to make sure I wasn't being dumb. :)

The current outer() function is highly suboptimal. However, until a
better solution can be implimented in a more MXNet-centric style it will
at least pass the tests. The Pull Request in which this commit exists
should _NOT_ be merged in until outer() is updated with a better
solution.
@coveralls
Copy link

coveralls commented Feb 11, 2018

Pull Request Test Coverage Report for Build 256

  • 86 of 89 (96.63%) changed or added relevant lines in 2 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.2%) to 98.064%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pyhf/tensor/mxnet_backend.py 83 84 98.81%
pyhf/init.py 3 5 60.0%
Files with Coverage Reduction New Missed Lines %
pyhf/init.py 2 97.21%
Totals Coverage Status
Change from base Build 237: -0.2%
Covered Lines: 709
Relevant Lines: 723

💛 - Coveralls

@matthewfeickert
Copy link
Member Author

@lukasheinrich Please don't accept this yet even though it passes. There are some things I want to fixup in it.

Use the linear algebra functions of MXNet to provide a more clean
implimenation of the outer product method, outer()
Extended iterable unpacking wasn't added until PEP 3132 and so isn't in
Python 2, which is causing the Python 2.7 tests to fail. While this is
annoying, it is probably worth still supporting Python 2.7 until the
official Python 2 end of life date.

c.f. https://www.python.org/dev/peps/pep-3132/
To support the more complex systems of multiple backends a Conda
environment.yml file is used instead of a pip requirements.txt file for
use with Binder.

c.f.
https://mybinder.readthedocs.io/en/latest/sample_repos.html#conda-environment-with-environment-yml
Additionally make a correction to outer().
nd.broadcast_mul() is the correct function to use in this method.
Add Google Style docstrings that should be compatible with Sphinx under
certain themes, such as the Sphinx theme created by Read The Docs. For
an example of what this would look like, see PyTorch's docs.

c.f.:
  http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html#example-google
  http://pytorch.org/docs/master/_modules/torch/distributions/bernoulli.html#Bernoulli
@matthewfeickert
Copy link
Member Author

@lukasheinrich I think this is ready for review, and if no comments then to be merged in.

@matthewfeickert matthewfeickert changed the title [WIP] Add MXNet backend Add MXNet backend Feb 12, 2018
@lukasheinrich
Copy link
Contributor

LGTM! thanks a lot!

@matthewfeickert
Copy link
Member Author

Closes Issue #80

@matthewfeickert matthewfeickert deleted the add-MXNet-backend branch February 12, 2018 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants