mini-jstorch 1.8.2 → 2.0.0
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/Docs/About.md +84 -83
- package/Docs/Structure.md +115 -128
- package/README.md +70 -31
- package/demo/fu_fun.js +71 -71
- package/demo/linear_regression.js +42 -0
- package/demo/xor_classification.js +47 -0
- package/package.json +23 -23
- package/src/jstorch.js +838 -166
package/demo/fu_fun.js
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
fu_tensor, fu_add, fu_mul, fu_matmul, fu_sum, fu_mean,
|
|
4
|
-
fu_relu, fu_sigmoid, fu_tanh, fu_softmax, fu_flatten, fu_reshape
|
|
5
|
-
} from '../src/jstorch.js';
|
|
6
|
-
|
|
7
|
-
function testAllFuFunctions() {
|
|
8
|
-
console.log("TESTING ALL FU_FUNCTIONS\n");
|
|
9
|
-
|
|
10
|
-
// Test 1: fu_tensor
|
|
11
|
-
console.log("1. fu_tensor");
|
|
12
|
-
const t1 = fu_tensor([[1, 2], [3, 4]]);
|
|
13
|
-
console.log("", t1.data);
|
|
14
|
-
|
|
15
|
-
// Test 2: fu_add
|
|
16
|
-
console.log("\n2. fu_add");
|
|
17
|
-
const a = fu_tensor([[1, 2]]);
|
|
18
|
-
const b = fu_tensor([[3, 4]]);
|
|
19
|
-
const c = fu_add(a, b);
|
|
20
|
-
console.log("", a.data, "+", b.data, "=", c.data);
|
|
21
|
-
|
|
22
|
-
// Test 3: fu_mul
|
|
23
|
-
console.log("\n3. fu_mul");
|
|
24
|
-
const d = fu_mul(a, b);
|
|
25
|
-
console.log("", a.data, "*", b.data, "=", d.data);
|
|
26
|
-
|
|
27
|
-
// Test 4: fu_matmul
|
|
28
|
-
console.log("\n4. fu_matmul");
|
|
29
|
-
const e = fu_tensor([[1, 2]]);
|
|
30
|
-
const f = fu_tensor([[3], [4]]);
|
|
31
|
-
const g = fu_matmul(e, f);
|
|
32
|
-
console.log("matmul =", g.data);
|
|
33
|
-
|
|
34
|
-
// Test 5: fu_sum & fu_mean
|
|
35
|
-
console.log("\n5. fu_sum & fu_mean");
|
|
36
|
-
const h = fu_tensor([[1, 2], [3, 4]]);
|
|
37
|
-
const sum = fu_sum(h);
|
|
38
|
-
const mean = fu_mean(h);
|
|
39
|
-
console.log("sum =", sum.data, "mean =", mean.data);
|
|
40
|
-
|
|
41
|
-
// Test 6: fu_relu
|
|
42
|
-
console.log("\n6. fu_relu");
|
|
43
|
-
const i = fu_tensor([[-1, 0], [1, 2]]);
|
|
44
|
-
const relu = fu_relu(i);
|
|
45
|
-
console.log("relu =", relu.data);
|
|
46
|
-
|
|
47
|
-
// Test 7: fu_sigmoid
|
|
48
|
-
console.log("\n7. fu_sigmoid");
|
|
49
|
-
const sigmoid = fu_sigmoid(i);
|
|
50
|
-
console.log("sigmoid =", sigmoid.data);
|
|
51
|
-
|
|
52
|
-
// Test 8: fu_tanh
|
|
53
|
-
console.log("\n8. fu_tanh");
|
|
54
|
-
const tanh = fu_tanh(i);
|
|
55
|
-
console.log("tanh =", tanh.data);
|
|
56
|
-
|
|
57
|
-
// Test 9: fu_softmax
|
|
58
|
-
console.log("\n9. fu_softmax");
|
|
59
|
-
const j = fu_tensor([[1, 2, 3]]);
|
|
60
|
-
const softmax = fu_softmax(j);
|
|
61
|
-
console.log("softmax =", softmax.data);
|
|
62
|
-
|
|
63
|
-
// Test 10: fu_flatten & fu_reshape
|
|
64
|
-
console.log("\n10. fu_flatten & fu_reshape");
|
|
65
|
-
const k = fu_tensor([[1, 2], [3, 4]]);
|
|
66
|
-
const flat = fu_flatten(k);
|
|
67
|
-
const reshaped = fu_reshape(flat, 1, 4);
|
|
68
|
-
console.log("flatten =", flat.data);
|
|
69
|
-
console.log("reshape =", reshaped.data);
|
|
70
|
-
}
|
|
71
|
-
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
fu_tensor, fu_add, fu_mul, fu_matmul, fu_sum, fu_mean,
|
|
4
|
+
fu_relu, fu_sigmoid, fu_tanh, fu_softmax, fu_flatten, fu_reshape
|
|
5
|
+
} from '../src/jstorch.js';
|
|
6
|
+
|
|
7
|
+
function testAllFuFunctions() {
|
|
8
|
+
console.log("TESTING ALL FU_FUNCTIONS\n");
|
|
9
|
+
|
|
10
|
+
// Test 1: fu_tensor
|
|
11
|
+
console.log("1. fu_tensor");
|
|
12
|
+
const t1 = fu_tensor([[1, 2], [3, 4]]);
|
|
13
|
+
console.log("", t1.data);
|
|
14
|
+
|
|
15
|
+
// Test 2: fu_add
|
|
16
|
+
console.log("\n2. fu_add");
|
|
17
|
+
const a = fu_tensor([[1, 2]]);
|
|
18
|
+
const b = fu_tensor([[3, 4]]);
|
|
19
|
+
const c = fu_add(a, b);
|
|
20
|
+
console.log("", a.data, "+", b.data, "=", c.data);
|
|
21
|
+
|
|
22
|
+
// Test 3: fu_mul
|
|
23
|
+
console.log("\n3. fu_mul");
|
|
24
|
+
const d = fu_mul(a, b);
|
|
25
|
+
console.log("", a.data, "*", b.data, "=", d.data);
|
|
26
|
+
|
|
27
|
+
// Test 4: fu_matmul
|
|
28
|
+
console.log("\n4. fu_matmul");
|
|
29
|
+
const e = fu_tensor([[1, 2]]);
|
|
30
|
+
const f = fu_tensor([[3], [4]]);
|
|
31
|
+
const g = fu_matmul(e, f);
|
|
32
|
+
console.log("matmul =", g.data);
|
|
33
|
+
|
|
34
|
+
// Test 5: fu_sum & fu_mean
|
|
35
|
+
console.log("\n5. fu_sum & fu_mean");
|
|
36
|
+
const h = fu_tensor([[1, 2], [3, 4]]);
|
|
37
|
+
const sum = fu_sum(h);
|
|
38
|
+
const mean = fu_mean(h);
|
|
39
|
+
console.log("sum =", sum.data, "mean =", mean.data);
|
|
40
|
+
|
|
41
|
+
// Test 6: fu_relu
|
|
42
|
+
console.log("\n6. fu_relu");
|
|
43
|
+
const i = fu_tensor([[-1, 0], [1, 2]]);
|
|
44
|
+
const relu = fu_relu(i);
|
|
45
|
+
console.log("relu =", relu.data);
|
|
46
|
+
|
|
47
|
+
// Test 7: fu_sigmoid
|
|
48
|
+
console.log("\n7. fu_sigmoid");
|
|
49
|
+
const sigmoid = fu_sigmoid(i);
|
|
50
|
+
console.log("sigmoid =", sigmoid.data);
|
|
51
|
+
|
|
52
|
+
// Test 8: fu_tanh
|
|
53
|
+
console.log("\n8. fu_tanh");
|
|
54
|
+
const tanh = fu_tanh(i);
|
|
55
|
+
console.log("tanh =", tanh.data);
|
|
56
|
+
|
|
57
|
+
// Test 9: fu_softmax
|
|
58
|
+
console.log("\n9. fu_softmax");
|
|
59
|
+
const j = fu_tensor([[1, 2, 3]]);
|
|
60
|
+
const softmax = fu_softmax(j);
|
|
61
|
+
console.log("softmax =", softmax.data);
|
|
62
|
+
|
|
63
|
+
// Test 10: fu_flatten & fu_reshape
|
|
64
|
+
console.log("\n10. fu_flatten & fu_reshape");
|
|
65
|
+
const k = fu_tensor([[1, 2], [3, 4]]);
|
|
66
|
+
const flat = fu_flatten(k);
|
|
67
|
+
const reshaped = fu_reshape(flat, 1, 4);
|
|
68
|
+
console.log("flatten =", flat.data);
|
|
69
|
+
console.log("reshape =", reshaped.data);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
72
|
testAllFuFunctions();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Sequential, Linear, MSELoss, Adam
|
|
2
|
+
} from '../src/jstorch.js';
|
|
3
|
+
|
|
4
|
+
// Dataset: y = 2x
|
|
5
|
+
const X = [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]];
|
|
6
|
+
const Y = [[2], [4], [6], [8], [10], [12], [14], [16], [18], [20]];
|
|
7
|
+
|
|
8
|
+
const model = new Sequential([
|
|
9
|
+
new Linear(1, 1)
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
const lossFn = new MSELoss();
|
|
13
|
+
const optimizer = new Adam(model.parameters(), {lr: 0.1}); // ← LR 0.1
|
|
14
|
+
|
|
15
|
+
console.log("=== Linear Regression (y = 2x) ===\n");
|
|
16
|
+
|
|
17
|
+
for (let epoch = 1; epoch <= 500; epoch++) {
|
|
18
|
+
const pred = model.forward(X);
|
|
19
|
+
const loss = lossFn.forward(pred, Y);
|
|
20
|
+
const grad = lossFn.backward();
|
|
21
|
+
model.backward(grad);
|
|
22
|
+
optimizer.step();
|
|
23
|
+
model.zeroGrad();
|
|
24
|
+
|
|
25
|
+
if (epoch % 100 === 0) {
|
|
26
|
+
console.log(`Epoch ${epoch} | Loss: ${loss.toFixed(6)}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
model.eval();
|
|
31
|
+
const finalPred = model.forward(X);
|
|
32
|
+
|
|
33
|
+
console.log('\nResults:');
|
|
34
|
+
let totalError = 0;
|
|
35
|
+
finalPred.forEach((p, i) => {
|
|
36
|
+
const error = Math.abs(p[0] - Y[i][0]);
|
|
37
|
+
totalError += error;
|
|
38
|
+
console.log(` x=${X[i][0]} → pred: ${p[0].toFixed(2)} | actual: ${Y[i][0]} | error: ${error.toFixed(2)}`);
|
|
39
|
+
});
|
|
40
|
+
console.log(`\nAverage Error: ${(totalError / X.length).toFixed(2)}`);
|
|
41
|
+
console.log(`Weight (slope): ${model.layers[0].W[0][0].toFixed(4)} (expected: 2.0)`);
|
|
42
|
+
console.log(`Bias: ${model.layers[0].b[0].toFixed(4)} (expected: 0.0)`);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Sequential, Linear, ReLU, BCEWithLogitsLoss, Adam
|
|
2
|
+
} from '../src/jstorch.js';
|
|
3
|
+
|
|
4
|
+
// Dataset XOR
|
|
5
|
+
const X = [[0,0], [0,1], [1,0], [1,1]];
|
|
6
|
+
const Y = [[0], [1], [1], [0]];
|
|
7
|
+
|
|
8
|
+
// Model
|
|
9
|
+
const model = new Sequential([
|
|
10
|
+
new Linear(2, 8),
|
|
11
|
+
new ReLU(),
|
|
12
|
+
new Linear(8, 1)
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
const lossFn = new BCEWithLogitsLoss();
|
|
16
|
+
const optimizer = new Adam(model.parameters(), {lr: 0.1});
|
|
17
|
+
|
|
18
|
+
console.log("=== XOR Binary Classification ===\n");
|
|
19
|
+
|
|
20
|
+
for (let epoch = 1; epoch <= 200; epoch++) {
|
|
21
|
+
const logits = model.forward(X);
|
|
22
|
+
const loss = lossFn.forward(logits, Y);
|
|
23
|
+
const grad = lossFn.backward();
|
|
24
|
+
model.backward(grad);
|
|
25
|
+
optimizer.step();
|
|
26
|
+
model.zeroGrad();
|
|
27
|
+
|
|
28
|
+
if (epoch % 50 === 0) {
|
|
29
|
+
const probs = logits.map(p => 1 / (1 + Math.exp(-p[0])));
|
|
30
|
+
console.log(`Epoch ${epoch} | Loss: ${loss.toFixed(6)}`);
|
|
31
|
+
probs.forEach((p, i) => console.log(` [${X[i]}] → ${p.toFixed(4)} (target: ${Y[i][0]})`));
|
|
32
|
+
console.log('');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Evaluation
|
|
37
|
+
model.eval();
|
|
38
|
+
const finalLogits = model.forward(X);
|
|
39
|
+
const finalProbs = finalLogits.map(p => 1 / (1 + Math.exp(-p[0])));
|
|
40
|
+
|
|
41
|
+
let correct = 0;
|
|
42
|
+
finalProbs.forEach((p, i) => {
|
|
43
|
+
const pred = p > 0.5 ? 1 : 0;
|
|
44
|
+
if (pred === Y[i][0]) correct++;
|
|
45
|
+
console.log(`[${X[i]}] → pred: ${pred} (${p.toFixed(4)}) | target: ${Y[i][0]} ${pred === Y[i][0] ? '✓' : '✗'}`);
|
|
46
|
+
});
|
|
47
|
+
console.log(`\nAccuracy: ${(correct / X.length * 100).toFixed(0)}%`);
|
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mini-jstorch",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "A lightweight JavaScript neural network library for learning AI concepts and rapid Frontend experimentation. PyTorch-inspired, zero dependencies, perfect for educational use.",
|
|
6
|
-
"main": "index.js",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/Rizal-HID11/mini-jstorch-github.git"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"lightweight-ml",
|
|
13
|
-
"javascript-torch",
|
|
14
|
-
"front-end-torch",
|
|
15
|
-
"tiny-ml",
|
|
16
|
-
"mini-neural-network",
|
|
17
|
-
"mini-ml-library",
|
|
18
|
-
"mini-js-ml",
|
|
19
|
-
"educational-ml"
|
|
20
|
-
],
|
|
21
|
-
"author": "Rizal",
|
|
22
|
-
"license": "MIT"
|
|
23
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "mini-jstorch",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A lightweight JavaScript neural network library for learning AI concepts and rapid Frontend experimentation. PyTorch-inspired, zero dependencies, perfect for educational use.",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Rizal-HID11/mini-jstorch-github.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"lightweight-ml",
|
|
13
|
+
"javascript-torch",
|
|
14
|
+
"front-end-torch",
|
|
15
|
+
"tiny-ml",
|
|
16
|
+
"mini-neural-network",
|
|
17
|
+
"mini-ml-library",
|
|
18
|
+
"mini-js-ml",
|
|
19
|
+
"educational-ml"
|
|
20
|
+
],
|
|
21
|
+
"author": "Rizal",
|
|
22
|
+
"license": "MIT"
|
|
23
|
+
}
|