mini-jstorch 1.4.2 → 1.4.3
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 +23 -3
- package/package.json +1 -1
- package/src/MainEngine.js +77 -13
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ A lightweight JavaScript neural network library for rapid frontend AI experiment
|
|
|
6
6
|
|
|
7
7
|
Mini-JSTorch is a high-performance, minimalist JavaScript library for building neural networks. It runs efficiently in both frontend and backend environments, including low-end devices. The library enables quick experimentation and learning in AI without compromising stability, accuracy, or training reliability.
|
|
8
8
|
|
|
9
|
-
This release, **version 1.4.3**,
|
|
9
|
+
This release, **version 1.4.3**, We make `Matrix Utils` now can be used in others Files.
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
@@ -33,7 +33,7 @@ This release, **version 1.4.3**, we introduces **learning rate schedulers**, imp
|
|
|
33
33
|
- **Activations:** ReLU, Sigmoid, Tanh, LeakyReLU, GELU, Mish, SiLU, ELU
|
|
34
34
|
- **Loss Functions:** MSELoss, CrossEntropyLoss
|
|
35
35
|
- **Optimizers:** Adam, SGD
|
|
36
|
-
- **Schedulers:** StepLR, LambdaLR
|
|
36
|
+
- **Schedulers:** StepLR, LambdaLR , ReduceLROnPlateau
|
|
37
37
|
- **Regularization:** Dropout, BatchNorm2D
|
|
38
38
|
- **Utilities:** zeros, randomMatrix, softmax, crossEntropy, dot, addMatrices, reshape, stack, flatten, eye, concat
|
|
39
39
|
- **Model Container:** Sequential (for stacking layers with forward/backward passes)
|
|
@@ -133,4 +133,24 @@ node tests/scheduler.js
|
|
|
133
133
|
|
|
134
134
|
# License
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
`MIT License`
|
|
137
|
+
|
|
138
|
+
**Copyright (c) 2025 rizal-editors**
|
|
139
|
+
|
|
140
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
141
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
142
|
+
in the Software without restriction, including without limitation the rights
|
|
143
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
144
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
145
|
+
furnished to do so, subject to the following conditions:
|
|
146
|
+
|
|
147
|
+
The above copyright notice and this permission notice shall be included in all
|
|
148
|
+
copies or substantial portions of the Software.
|
|
149
|
+
|
|
150
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
151
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
152
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
153
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
154
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
155
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
156
|
+
SOFTWARE.
|
package/package.json
CHANGED
package/src/MainEngine.js
CHANGED
|
@@ -1,17 +1,77 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Project: mini-jstorch
|
|
3
|
+
* File: MainEngine.js
|
|
4
|
+
* Author: M. Rizal H. (Actual Author Name)
|
|
5
|
+
* License: MIT
|
|
6
|
+
* Copyright (C) 2025 M. Rizal H.
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*/
|
|
1
26
|
|
|
2
|
-
// OFFICIAL MINI-JSTORCH ENGINE
|
|
3
|
-
// LICENSED UNDER MIT LICENSE
|
|
4
|
-
// MIT (C) Rizal 2025
|
|
5
27
|
|
|
6
28
|
// ---------------------- Utilities ----------------------
|
|
7
|
-
function zeros(rows, cols) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
29
|
+
export function zeros(rows, cols) {
|
|
30
|
+
return Array.from({length:rows},()=>Array(cols).fill(0));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function ones(rows, cols) {
|
|
34
|
+
return Array.from({length:rows},()=>Array(cols).fill(1));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function randomMatrix(rows, cols, scale=0.1){
|
|
38
|
+
return Array.from({length:rows},()=>
|
|
39
|
+
Array.from({length:cols},()=> (Math.random()*2-1)*scale)
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function transpose(matrix){
|
|
44
|
+
return matrix[0].map((_,i)=>matrix.map(row=>row[i]));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function addMatrices(a,b){
|
|
48
|
+
return a.map((row,i)=>
|
|
49
|
+
row.map((v,j)=>v+(b[i] && b[i][j]!==undefined?b[i][j]:0))
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function dot(a,b){
|
|
54
|
+
const res=zeros(a.length,b[0].length);
|
|
55
|
+
for(let i=0;i<a.length;i++)
|
|
56
|
+
for(let j=0;j<b[0].length;j++)
|
|
57
|
+
for(let k=0;k<a[0].length;k++)
|
|
58
|
+
res[i][j]+=a[i][k]*b[k][j];
|
|
59
|
+
return res;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function softmax(x){
|
|
63
|
+
const m=Math.max(...x);
|
|
64
|
+
const exps=x.map(v=>Math.exp(v-m));
|
|
65
|
+
const s=exps.reduce((a,b)=>a+b,0);
|
|
66
|
+
return exps.map(v=>v/s);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function crossEntropy(pred,target){
|
|
70
|
+
const eps=1e-12;
|
|
71
|
+
return -target.reduce((sum,t,i)=>sum+t*Math.log(pred[i]+eps),0);
|
|
72
|
+
}
|
|
73
|
+
/* Not Added more Utils yet. (this patch not MINOR)
|
|
74
|
+
Just make it Public to other files. */
|
|
15
75
|
|
|
16
76
|
// ---------------------- Tensor ----------------------
|
|
17
77
|
export class Tensor {
|
|
@@ -243,6 +303,7 @@ export class StepLR {
|
|
|
243
303
|
|
|
244
304
|
get_lr() {
|
|
245
305
|
return this.optimizer.lr;
|
|
306
|
+
/* Do nothing else */
|
|
246
307
|
}
|
|
247
308
|
}
|
|
248
309
|
|
|
@@ -261,6 +322,7 @@ export class LambdaLR {
|
|
|
261
322
|
|
|
262
323
|
get_lr() {
|
|
263
324
|
return this.optimizer.lr;
|
|
325
|
+
/* Do nothing else */
|
|
264
326
|
}
|
|
265
327
|
}
|
|
266
328
|
|
|
@@ -573,6 +635,7 @@ export function saveModel(model){
|
|
|
573
635
|
if(!(model instanceof Sequential)) throw new Error("saveModel supports only Sequential");
|
|
574
636
|
const weights=model.layers.map(layer=>({weights:layer.W||null,biases:layer.b||null}));
|
|
575
637
|
return JSON.stringify(weights);
|
|
638
|
+
/* Didn't expect this to work */
|
|
576
639
|
}
|
|
577
640
|
|
|
578
641
|
export function loadModel(model,json){
|
|
@@ -582,6 +645,7 @@ export function loadModel(model,json){
|
|
|
582
645
|
if(layer.W && weights[i].weights) layer.W=weights[i].weights;
|
|
583
646
|
if(layer.b && weights[i].biases) layer.b=weights[i].biases;
|
|
584
647
|
});
|
|
648
|
+
/* Didn't expect this to work */
|
|
585
649
|
}
|
|
586
650
|
|
|
587
651
|
// ---------------------- Advanced Utils ----------------------
|
|
@@ -590,10 +654,10 @@ export function stack(tensors){ return tensors.map(t=>t.data); }
|
|
|
590
654
|
export function eye(n){ return Array.from({length:n},(_,i)=>Array.from({length:n},(_,j)=>i===j?1:0)); }
|
|
591
655
|
export function concat(a,b,axis=0){ /* concat along axis */ if(axis===0) return [...a,...b]; if(axis===1) return a.map((row,i)=>[...row,...b[i]]); }
|
|
592
656
|
export function reshape(tensor, rows, cols) {
|
|
593
|
-
let flat = tensor.data.flat(); // flatten
|
|
657
|
+
let flat = tensor.data.flat(); // flatten first
|
|
594
658
|
if(flat.length < rows*cols) throw new Error("reshape size mismatch");
|
|
595
659
|
const out = Array.from({length: rows}, (_, i) =>
|
|
596
660
|
flat.slice(i*cols, i*cols + cols)
|
|
597
661
|
);
|
|
598
662
|
return out;
|
|
599
|
-
}
|
|
663
|
+
}
|