https://github.com/pratyushtiwary/toynn
A toy NN library in JS to play and learn NN concepts.
https://github.com/pratyushtiwary/toynn
javascript neural-network nn toynn typescript
Last synced: 4 months ago
JSON representation
A toy NN library in JS to play and learn NN concepts.
- Host: GitHub
- URL: https://github.com/pratyushtiwary/toynn
- Owner: pratyushtiwary
- License: mit
- Created: 2023-07-08T07:27:38.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-12-13T21:06:52.000Z (6 months ago)
- Last Synced: 2025-12-15T16:35:52.130Z (6 months ago)
- Topics: javascript, neural-network, nn, toynn, typescript
- Language: TypeScript
- Homepage: https://toynn.vercel.app/
- Size: 2.81 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 32
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
A toy NN library for JS to play and learn NN concepts.
Contribution is much appreciated, if you feel like something is not right or you've found some bug feel free to raise an issue or create a PR
## Requirements
[NodeJS](https://nodejs.org/) v18.0.0 or higher must be installed to use `toynn`.
## Installation
| **Source** | **Info** |
| ---------- | ------------------- |
| npm | `npm install toynn` |
| yarn | `yarn add toynn` |
## Usage
```js
import toynn from 'toynn';
const X = [
new toynn.NArray([0, 0]).reshape(1, 2),
new toynn.NArray([0, 1]).reshape(1, 2),
new toynn.NArray([1, 0]).reshape(1, 2),
new toynn.NArray([1, 1]).reshape(1, 2),
];
const y = [
new toynn.NArray([0]),
new toynn.NArray([0]),
new toynn.NArray([0]),
new toynn.NArray([1]),
];
const model = new toynn.NN('and');
const layer1 = new toynn.Layer(2, 3);
layer1.use(toynn.functions.linear);
const layer2 = new toynn.Layer(3, 1);
layer2.use(toynn.functions.sigmoid);
model.add(layer1);
model.add(layer2);
model.train({
x: X,
y,
epochs: 500,
alpha: 0.001,
loss: toynn.errors.MSE,
verbose: true,
});
let newData = new toynn.NArray([1, 0]).reshape(1, 2);
// make prediction
console.log(model.forward(newData).flatten());
```
**Note: The above code only supports v2.0.0 or above**
**You can use the library with typescript also. The code remains the same.**
## Docs
Docs can be found [here](https://toynn.vercel.app/).