Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Python API for Myelin #302

Merged
merged 2 commits into from
Dec 4, 2018
Merged

Python API for Myelin #302

merged 2 commits into from
Dec 4, 2018

Conversation

ringgaard
Copy link
Contributor

You can now run Myelin computations from Python using pysling. I have updated flow.py to support flow version 5 format. You can now compile these flows to a network to run computations. I have updated the documentation with examples of running Myelin in Python.

The Myelin tensors support the Python buffer interface, so these can be shared with other Python modules with buffer support, like numpy.

@ringgaard ringgaard self-assigned this Nov 30, 2018
@ringgaard ringgaard requested a review from rahul1980 November 30, 2018 13:53
// Get single-dimensional index.
int idx = PyInt_AsLong(index);
if (idx == -1 && PyErr_Occurred()) return nullptr;
if (idx < 0 || idx >= format->dim(0)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: you can support the Python convention here:
if (idx < 0) idx += format->dim(0);

def neg_grad(op, g):
x = op.inputs[0]
y = op.outputs[0]
g.add(x, g.expr.mul(g.d(y), g.expr.neg(g.v(x))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this just be:
g.add(x, g.expr.neg(g.d(y))) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This bug has already been fixed in the C++ version.

def reciprocal_grad(op, g):
x = op.inputs[0]
y = op.outputs[0]
g.add(x, g.expr.mul(g.d(y), g.expr.neg(g.expr.rcp(g.expr.square(g.v(x))))))
Copy link
Contributor

Choose a reason for hiding this comment

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

g.expr.neg(g.expr.square(g.v(y))) might be more efficient than g.expr.neg(g.expr.rcp(g.expr.square(g.v(x))))))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. This is my autograd experiment in python which was included in this PR by mistake.

However, the optimization is still good since you save a division, so I have changed this in the C++ version. Thanks for spotting this.


![input flow](flowin.svg)

The graph only shows the input and output variables (gren and blue), and the
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: gren -> green

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


The weights in W and b can be initialized from NumPy arrays or any other
objects that support the
[Python bufffer protocol](https://docs.python.org/2/c-api/buffer.html):
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: bufffer -> buffer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor Author

@ringgaard ringgaard left a comment

Choose a reason for hiding this comment

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

Thanks for the review.

def reciprocal_grad(op, g):
x = op.inputs[0]
y = op.outputs[0]
g.add(x, g.expr.mul(g.d(y), g.expr.neg(g.expr.rcp(g.expr.square(g.v(x))))))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. This is my autograd experiment in python which was included in this PR by mistake.

However, the optimization is still good since you save a division, so I have changed this in the C++ version. Thanks for spotting this.

def neg_grad(op, g):
x = op.inputs[0]
y = op.outputs[0]
g.add(x, g.expr.mul(g.d(y), g.expr.neg(g.v(x))))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This bug has already been fixed in the C++ version.


![input flow](flowin.svg)

The graph only shows the input and output variables (gren and blue), and the
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


The weights in W and b can be initialized from NumPy arrays or any other
objects that support the
[Python bufffer protocol](https://docs.python.org/2/c-api/buffer.html):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@ringgaard ringgaard merged commit 523f73f into google:master Dec 4, 2018
@ringgaard ringgaard deleted the pymyelin branch December 4, 2018 12:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants