mini-jstorch 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/activations/relu.js +7 -0
- package/index.js +5 -0
- package/layers/Dense.js +17 -0
- package/models/Sequential.js +20 -0
- package/package.json +17 -8
- package/utils/io.js +25 -0
- package/utils/math.js +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# mini-jstorch
|
|
2
|
+
*NOTICE!*:
|
|
3
|
+
This versions is still on a **BETA** Versions of **mini-jstorch**!
|
|
4
|
+
So maybe the features is not many and completely *SO* BE PATIENCE!!
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
**Version: 1.0.2**
|
|
9
|
+
A lightweight and browser-compatible deep learning framework inspired by **PyTorch** — written in pure JavaScript.
|
|
10
|
+
Perfect for CodePen, web demos, and fast prototyping.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- # `Sequential` model API
|
|
17
|
+
- # Fully modular layers (e.g., `Dense`)
|
|
18
|
+
- # Activation functions: `ReLU`, `Sigmoid`
|
|
19
|
+
- # Model serialization (`saveModel`, `loadModel`)
|
|
20
|
+
- # Works on both **Node.js** and **browser**
|
|
21
|
+
- # No native bindings, 100% JavaScript
|
|
22
|
+
- # Inspired by PyTorch, but mini-sized
|
|
23
|
+
|
|
24
|
+
---
|
package/index.js
ADDED
package/layers/Dense.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { dot, add } from '../utils/math.js';
|
|
2
|
+
|
|
3
|
+
export class Dense {
|
|
4
|
+
constructor(inputSize, outputSize, activation = x => x) {
|
|
5
|
+
this.weights = Array.from({ length: outputSize }, () =>
|
|
6
|
+
Array.from({ length: inputSize }, () => Math.random() * 0.1)
|
|
7
|
+
);
|
|
8
|
+
this.bias = Array(outputSize).fill(0);
|
|
9
|
+
this.activation = activation;
|
|
10
|
+
this.name = "Dense";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
forward(input) {
|
|
14
|
+
const z = add(dot(this.weights, input), this.bias);
|
|
15
|
+
return z.map(this.activation);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export class Sequential {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.layers = [];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
add(layer) {
|
|
7
|
+
this.layers.push(layer);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
predict(input) {
|
|
11
|
+
return this.layers.reduce((out, layer) => layer.forward(out), input);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
summary() {
|
|
15
|
+
console.log("Sequential model:");
|
|
16
|
+
this.layers.forEach((layer, i) => {
|
|
17
|
+
console.log(` Layer ${i + 1}: ${layer.name || "unnamed"}`);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mini-jstorch",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "A lightweight JavaScript neural network framework for browser & Node.js, inspired by PyTorch.",
|
|
4
5
|
"main": "index.js",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
"keywords": [
|
|
7
|
+
"neural-network",
|
|
8
|
+
"javascript",
|
|
9
|
+
"lightweight",
|
|
10
|
+
"ai",
|
|
11
|
+
"machine-learning",
|
|
12
|
+
"browser",
|
|
13
|
+
"mini"
|
|
14
|
+
],
|
|
15
|
+
"author": "Rizal",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/rizal-editors/mini-jstorch.git"
|
|
20
|
+
}
|
|
12
21
|
}
|
package/utils/io.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function saveModel(model) {
|
|
2
|
+
const modelData = {
|
|
3
|
+
layers: model.layers.map(layer => ({
|
|
4
|
+
name: layer.name,
|
|
5
|
+
weights: layer.weights,
|
|
6
|
+
bias: layer.bias
|
|
7
|
+
}))
|
|
8
|
+
};
|
|
9
|
+
return JSON.stringify(modelData);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function loadModel(json, ModelClass, LayerClass) {
|
|
13
|
+
const data = typeof json === 'string' ? JSON.parse(json) : json;
|
|
14
|
+
const model = new ModelClass();
|
|
15
|
+
|
|
16
|
+
data.layers.forEach(layer => {
|
|
17
|
+
const dense = new LayerClass(0, 0); // dummy init
|
|
18
|
+
dense.weights = layer.weights;
|
|
19
|
+
dense.bias = layer.bias;
|
|
20
|
+
dense.name = layer.name;
|
|
21
|
+
model.add(dense);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return model;
|
|
25
|
+
}
|