@sourceacademy/torch
v0.1.0
Published
machine-learning libraries for Source Academy
Readme
torch
machine-learning libraries for Source Academy
The primary objective of this project is to create a reimplementation of PyTorch in TypeScript, with an educational focus. This project is developed with Source Academy integration in mind.
This project reimplements core parts of PyTorch while trying to keep the codebase simple, and the API as close to PyTorch as possible.
Using Pyodide, we can run Python code in the browser. Using pyodide_bridge.py in a way similar to examples/pyodide/ we can run PyTorch-like code in the browser.
Notable differences with PyTorch
- This library exposes extra information for debuggers and visualizers to catch, as seen in
eventsinsrc/util.ts. It is similar to hooks in PyTorch. - This library does not differentiate between LongTensors and FloatTensors. It uses
numberfor all tensor elements. - This library does not currently support devices, such as GPUs.
Getting Started
Install yarn:
corepack enable
# or
npm install -g yarnInstall dependencies:
yarn installDemo Usage
First, build it:
yarn buildSee examples/ for examples.
Node
See examples/basic_backpropagation.js.
node examples/basic_backpropagation.jsBrowser
You can run http-server and load examples/browser/index.html to see how it works.
yarn serve
# and navigate to http://localhost:8080/examples/browser/index.html to run torch in js
# or http://localhost:8080/examples/pyodide/index.html to run in python
# or http://localhost:8080/test/ to run the testsContributing
Contributions are welcome. The short version:
- Run
yarn testto verify everything passes. - Add tests for new ops or behaviour changes.
- Follow the existing patterns — new ops go in src/functions/ops.ts.
For full details on the codebase, how to add operations, and the testing setup, see CONTRIBUTING.md.
