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 TensorFlow library to Pyret #287

Open
wants to merge 4 commits into
base: horizon
Choose a base branch
from

Conversation

ZacharyEspiritu
Copy link

@ZacharyEspiritu ZacharyEspiritu commented Aug 17, 2018

This pull request adds a TensorFlow library to Pyret for the development of programs with machine learning capabilities. It's a transfer of the code from this pyret-lang branch to CPO.

The library code is located at src/js/trove/tensorflow.js and (mostly complete) documentation is located in a separate pull request at brownplt/pyret-docs#44. The old branch contains example programs at examples/tensorflow, but I'll extract those out to a separate repo in the meantime.

Most of the functions in the Pyret library are ported from the existing TensorFlow.js project. Two important questions:

  • I have test cases for about 50-60% of the library functions in the old branch, but couldn't find a good place to put them in CPO—is there a good place to put them?
  • In order to create Roughnums, I exposed the num_to_roughnum function in the Pyret runtime at this pull request: Expose num_to_roughnum from runtime pyret-lang#1393. There might be a better way to do this—let me know!

And a few interesting notes:

  • TensorFlow.js stores all values in Float32Arrays. As such, retrieving data from a Pyret Tensor always returns a List<Roughnum>. Unavoidably, this also means that all Pyret numbers need to be converted to JavaScript fixnums before passing them to a TensorFlow.js function, so we’ll lose some precision and won’t be able to handle arbitrary large numbers. However, this should be fine, since this is a limitation of the official TensorFlow.js library too.
  • Errors are handled as separate checks within the Pyret library before passing the data along to the TensorFlow.js equivalents. The rationale for this is that the Pyret library can then improve on the TensorFlow.js error messages (which, IMO, are not clear in some cases), and I think it’s preferable to figure out what the edge cases for each function are so we can document those in pyret-docs rather than relying on try/catch blocks to handle unknown edge cases.
  • There are some TensorFlow.js functions that have many optional arguments and they consume a single JS object containing argument names and values. This is simulated in the Pyret library by using Pyret objects, though in order to preserve the Pyret style of kebob-case naming, the Pyret library maps kebob-case keys to their camelCase equivalents and also maps each key to a type check / Pyret-to-JS converter function. This allows users to use native Pyret interfaces such as Lists and PyretTensors in their argument objects while still allowing TensorFlow.js to understand the arguments correctly.

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.

1 participant