mini-jstorch 1.8.1 → 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 CHANGED
@@ -1,83 +1,84 @@
1
- # Mini-JSTorch — Technical Information
2
-
3
- ---
4
-
5
- ## General Information
6
-
7
- - **Project Name:** mini-jstorch
8
- - **Internal Name:** JST (JST-orch)
9
-
10
- > Note:
11
- > Early versions of JST do not strictly follow semantic versioning conventions
12
- > (e.g. `0.0.1` for patches, `0.1.0` for minor releases, `1.0.0` for major releases).
13
- > This inconsistency reflects the early learning and experimental phase of the project.
14
-
15
- ---
16
-
17
- ## 1. Engine Architecture Limitations (JST Core)
18
-
19
- This section outlines the known structural weaknesses of the JST engine.
20
- Although the architecture may appear complex, it is currently sensitive and tightly coupled.
21
-
22
- ### Identified Limitations
23
-
24
- - **High dependency on Utilities**
25
- Every core class depends directly on the Utilities module, which is defined at the top of the `jstorch.js` file. This creates strong coupling across the engine.
26
-
27
- - **Limited Tensor dimensionality**
28
- Tensor implementations currently support only two dimensions.
29
- Extending support to higher-dimensional tensors would require significant architectural changes due to the existing complexity.
30
-
31
- - **Uneven class complexity**
32
- New or recently modified classes often become significantly more complex than others, leading to inconsistency in maintainability and internal design balance.
33
-
34
- ---
35
-
36
- ## 2. Rationale Behind the `fu_` Utilities
37
-
38
- This section explains why the `fu_` utilities were introduced despite the existence of internal Utilities.
39
-
40
- ### Issues with Internal Utilities
41
-
42
- - The Utilities defined at the beginning of `jstorch.js` are **internal engine helpers**, not intended for direct user interaction.
43
-
44
- - These Utilities are heavily reused across multiple core classes.
45
- Any modification to a utility function may trigger **cascading (domino) errors** throughout the engine due to tight dependencies.
46
-
47
- - Some utility functions intentionally diverge from standard or expected formulas.
48
- For example:
49
- - Expected formula:
50
- `Param1 - Param4 * Param3`
51
- - Internal Utilities implementation:
52
- `Param1 - Param2 * Param3 + Param4`
53
-
54
- This behavior exists because internal Utilities are optimized for class-level computations, not for user-facing correctness or predictability.
55
-
56
- ### Purpose of `fu_` Utilities
57
-
58
- The `fu_` utilities were designed to improve the **user experience** by providing:
59
-
60
- - Predictable and correct computational behavior
61
- - User-friendly and stable helper functions
62
- - Isolation from internal engine changes
63
- - Reduced risk of incorrect outputs and dependency-based cascading errors
64
-
65
- In short, `fu_` exists to ensure safety, clarity, and consistency for end users of Mini-JSTorch.
66
-
67
- ---
68
-
69
- ## 3. SJK (Shortcut JST Keywords) Reference
70
-
71
- This section lists commonly used abbreviations and keywords within the mini-jstorch ecosystem.
72
-
73
- **Format:** `"KEYWORD" : "Full Name / Meaning"`
74
-
75
- - `"JST"` : JSTorch
76
- - `"fu"` : For User / User-Friendly
77
- - `"fun"` : Function
78
- - `"Dummy"` : Experimental
79
- - `"Exp"` : Restricted experimental entity
80
- - `"msg"` : Message, comment, warning, announcement
81
- - `"donot"` : Do not / Don't
82
-
83
- ---
1
+ # Mini-JSTorch — Technical Information
2
+
3
+ ---
4
+
5
+ ## General Information
6
+
7
+ - **Project Name:** mini-jstorch
8
+ - **Internal Name:** JST (JST-orch)
9
+
10
+ > Note:
11
+ > Early versions of JST do not strictly follow semantic versioning conventions
12
+ > (e.g. `0.0.1` for patches, `0.1.0` for minor releases, `1.0.0` for major releases).
13
+ > This inconsistency reflects the early learning and experimental phase of the project.
14
+
15
+ ---
16
+
17
+ ## 1. Engine Architecture Limitations (JST Core)
18
+
19
+ This section outlines the known structural weaknesses of the JST engine.
20
+ Although the architecture may appear complex, it is currently sensitive and tightly coupled.
21
+
22
+ ### Identified Limitations
23
+
24
+ - **High dependency on Utilities**
25
+ Every core class depends directly on the Utilities module, which is defined at the top of the `jstorch.js` file. This creates strong coupling across the engine.
26
+
27
+ - **Limited Tensor dimensionality**
28
+ Tensor implementations currently support only two dimensions.
29
+ Extending support to higher-dimensional tensors would require significant architectural changes due to the existing complexity.
30
+
31
+ - **Uneven class complexity**
32
+ New or recently modified classes often become significantly more complex than others, leading to inconsistency in maintainability and internal design balance.
33
+
34
+ ---
35
+
36
+ ## 2. Rationale Behind the `fu_` Utilities
37
+
38
+ This section explains why the `fu_` utilities were introduced despite the existence of internal Utilities.
39
+
40
+ ### Issues with Internal Utilities
41
+
42
+ - The Utilities defined at the beginning of `jstorch.js` are **internal engine helpers**, not intended for direct user interaction.
43
+
44
+ - These Utilities are heavily reused across multiple core classes.
45
+ Any modification to a utility function may trigger **cascading (domino) errors** throughout the engine due to tight dependencies.
46
+
47
+ - Some utility functions intentionally diverge from standard or expected formulas.
48
+ For example:
49
+ - Expected formula:
50
+ `Param1 - Param4 * Param3`
51
+ - Internal Utilities implementation:
52
+ `Param1 - Param2 * Param3 + Param4`
53
+
54
+ This behavior exists because internal Utilities are optimized for class-level computations, not for user-facing correctness or predictability.
55
+
56
+ ### Purpose of `fu_` Utilities
57
+
58
+ The `fu_` utilities were designed to improve the **user experience** by providing:
59
+
60
+ - Predictable and correct computational behavior
61
+ - User-friendly and stable helper functions
62
+ - Isolation from internal engine changes
63
+ - Reduced risk of incorrect outputs and dependency-based cascading errors
64
+
65
+ In short, `fu_` exists to ensure safety, clarity, and consistency for end users of Mini-JSTorch.
66
+
67
+ ---
68
+
69
+ ## 3. SJK (Shortcut JST Keywords) Reference
70
+
71
+ This section lists commonly used abbreviations and keywords within the mini-jstorch ecosystem.
72
+
73
+ **Format:** `"KEYWORD" : "Full Name / Meaning"`
74
+
75
+ - `"JST"` : JSTorch
76
+ - `"fu"` : For User / User-Friendly
77
+ - `"fun"` : Function
78
+ - `"Dummy"` : Experimental
79
+ - `"Exp"` : Restricted experimental entity
80
+ - `"msg"` : Message, comment, warning, announcement
81
+ - `"donot"` : Do not / Don't
82
+ // add more.
83
+
84
+ ---
package/Docs/Structure.md CHANGED
@@ -1,129 +1,116 @@
1
- # Project File Structure #
2
-
3
- This document describes the directory and file structure of the **mini-JSTorch** package.
4
- It provides an overview of how the project is organized and the purpose of each major component.
5
-
6
- ---
7
-
8
- ## Repository Overview
9
-
10
- ```text
11
- mini-jstorch/
12
- ├── demo/
13
- │ ├── fu_fun.js
14
- │ ├── MakeModel.js
15
- │ └── scheduler.js
16
- ├── Docs/
17
- │ ├── About.md
18
- │ └── Structure.md
19
- ├── src/
20
- ├── jstorch.js
21
- │ └── Dummy/
22
- └── msg/
23
- ├── index.js
24
- ├── package.json
25
- └── README.md
26
- ```
27
-
28
- ---
29
-
30
- ## Directory Descriptions
31
-
32
- `/demo`
33
-
34
- - Contains demonstration and testing files.
35
-
36
- - Used for unit testing, quick system checks, and example usage
37
- - Intended for users who prefer practical examples over reading full API documentation
38
- - Allows testing features without writing extensive manual code
39
-
40
- `/Docs`
41
-
42
- - Contains detailed documentation related to the mini-JSTorch package.
43
-
44
- - Provides deeper explanations of internal design and usage
45
- - Intended for contributors and advanced users
46
-
47
- `/src`
48
-
49
- - Contains the source code of the JSTorch engine.
50
-
51
- - Houses all core logic and internal implementations
52
- - Modifications in this directory directly affect engine behavior
53
-
54
- `/src/Dummy`
55
-
56
- - Experimental and restricted directory.
57
-
58
- - Used for experimental purposes and future development
59
- - Files inside this directory may be unstable or incomplete
60
- - Not intended for public or production use
61
-
62
- `/src/Dummy/msg`
63
-
64
- - Contains warning or message files.
65
-
66
- - Indicates that files within the `Dummy` directory are restricted
67
- - Serves as a notification mechanism for experimental or future-update-related content
68
-
69
- ---
70
-
71
- ## File Descriptions
72
-
73
- `/demo/fu_fun.js`
74
-
75
- - Purpose: Tests all user-facing (`fu_`) functions
76
- - Notes: Focuses on friendly and predictable helper utilities
77
-
78
- `/demo/MakeModel.js`
79
-
80
- - Purpose: Demonstrates creation of a simple model
81
- - Notes: Uses the `StepLR` scheduler as part of the example workflow
82
-
83
- `/demo/scheduler.js`
84
-
85
- - Purpose: Tests scheduler-related functionality
86
- - Notes: Intended to validate learning rate scheduling behavior
87
-
88
- `/Docs/About.md`
89
-
90
- - Purpose: Contains additional information about the mini-JSTorch package
91
- - Notes: May include background, design decisions, or non-API-related explanations
92
-
93
- `/Docs/Structure.md`
94
-
95
- - Purpose: Documents the repository file and folder structure
96
- - Notes: This file
97
-
98
- `/src/jstorch.js`
99
-
100
- - Purpose: Core engine implementation
101
-
102
- - Notes:
103
-
104
- - Contains all JSTorch engine logic and functions
105
- - Central file of the entire package
106
- - Changes here have wide-ranging effects
107
-
108
- `index.js`
109
-
110
- - Purpose: Package entry point
111
- - Notes: Exposes public APIs and connects internal modules
112
-
113
- `package.json`
114
-
115
- - Purpose: Project configuration and metadata
116
- - Notes: Defines dependencies, scripts, and package information
117
-
118
- `README.md`
119
-
120
- - Purpose: Main documentation entry
121
- - Notes: Provides overview, installation instructions, and basic usage
122
-
123
- **Notes**
124
-
125
- - Experimental files may change or be restricted without notice
126
- - Users are encouraged to rely on public APIs and documented utilities
127
- - Internal structures are subject to refactoring as the project evolves
128
-
1
+ # Project File Structure #
2
+
3
+ This document describes the directory and file structure of the **mini-JSTorch** package.
4
+ It provides an overview of how the project is organized and the purpose of each major component.
5
+
6
+ ---
7
+
8
+ ## Repository Overview
9
+
10
+ ```text
11
+ mini-jstorch/
12
+ ├── demo/
13
+ │ ├── fu_fun.js
14
+ │ ├── MakeModel.js
15
+ │ └── scheduler.js
16
+ xor_classification.js
17
+ linear_regression.js
18
+ ├── Docs/
19
+ ├── About.md
20
+ └── Structure.md
21
+ ├── src/
22
+ ├── jstorch.js
23
+ │ └── Dummy/
24
+ │ └── msg/
25
+ ├── index.js
26
+ ├── package.json
27
+ └── README.md
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Directory Descriptions
33
+
34
+ `/demo`
35
+
36
+ - Contains demonstration and testing files.
37
+
38
+ - Used for unit testing, quick system checks, and example usage
39
+ - Intended for users who prefer practical examples over reading full API documentation
40
+ - Allows testing features without writing extensive manual code
41
+
42
+ `/Docs`
43
+
44
+ - Contains detailed documentation related to the mini-JSTorch package.
45
+
46
+ - Provides deeper explanations of internal design and usage
47
+ - Intended for contributors and advanced users
48
+
49
+ `/src`
50
+
51
+ - Contains the source code of the JSTorch engine.
52
+
53
+ - Houses all core logic and internal implementations
54
+ - Modifications in this directory directly affect engine behavior
55
+
56
+ `/src/Dummy`
57
+
58
+ - Experimental and restricted directory.
59
+
60
+ - Used for experimental purposes and future development
61
+ - Files inside this directory may be unstable or incomplete
62
+ - Not intended for public or production use
63
+
64
+ `/src/Dummy/msg`
65
+
66
+ - Contains warning or message files.
67
+
68
+ - Indicates that files within the `Dummy` directory are restricted
69
+ - Serves as a notification mechanism for experimental or future-update-related content
70
+
71
+ ---
72
+
73
+ ## File Descriptions
74
+
75
+ `/Docs/About.md`
76
+
77
+ - Purpose: Contains additional information about the mini-JSTorch package
78
+ - Notes: May include background, design decisions, or non-API-related explanations
79
+
80
+ `/Docs/Structure.md`
81
+
82
+ - Purpose: Documents the repository file and folder structure
83
+ - Notes: This file
84
+
85
+ `/src/jstorch.js`
86
+
87
+ - Purpose: Core engine implementation
88
+
89
+ - Notes:
90
+
91
+ - Contains all JSTorch engine logic and functions
92
+ - Central file of the entire package
93
+ - Changes here have wide-ranging effects
94
+
95
+ `index.js`
96
+
97
+ - Purpose: Package entry point
98
+ - Notes: Exposes public APIs and connects internal modules
99
+
100
+ `package.json`
101
+
102
+ - Purpose: Project configuration and metadata
103
+ - Notes: Defines dependencies, scripts, and package information
104
+
105
+ `README.md`
106
+
107
+ - Purpose: Main documentation entry
108
+ - Notes: Provides overview, installation instructions, and basic usage
109
+
110
+ **Notes**
111
+
112
+ - Experimental files may change or be restricted without notice
113
+ - Users are encouraged to rely on public APIs and documented utilities
114
+ - Internal structures are subject to refactoring as the project evolves
115
+
129
116
  ---
package/README.md CHANGED
@@ -1,12 +1,26 @@
1
- ## Mini-JSTorch
1
+ ## Mini-JSTorch (MAJOR UPDATE)
2
2
 
3
+ ---
3
4
 
4
5
  Mini-JSTorch is a lightweight, `dependency-free` JavaScript neural network library designed for `education`, `experimentation`, and `small-scale models`.
5
6
  It runs in Node.js and modern browsers, with a simple API inspired by PyTorch-style workflows.
6
7
 
7
8
  This project prioritizes `clarity`, `numerical correctness`, and `accessibility` over performance or large-scale production use.
8
9
 
9
- In this version `1.8.0`, we Introduce the **SoftmaxCrossEntropyLoss**, and **BCEWithLogitsLoss**
10
+ In this version `2.0.0`, we introduce:
11
+ - **Fixed Linear layer cache** (critical bug fix for training)
12
+ - **Fixed GELU gradient calculation**
13
+ - **Fixed MSELoss gradient scaling**
14
+ - **Optimized Softmax gradient** (O(n²) → O(n))
15
+ - **Improved Tokenizer** with proper PAD/UNK separation
16
+ - **Added Sequential.zeroGrad(), train(), eval(), stateDict() methods**
17
+
18
+ ---
19
+
20
+ **⚠️ BREAKING CHANGES in v2.0.0:**
21
+ - Tokenizer API: `tokenizeBatch()` → `transform()`, `detokenizeBatch()` → `inverseTransform()`
22
+ - Tokenizer now uses `<PAD>` at index 0 and `<UNK>` at index 1
23
+ - MSELoss gradient scale now matches PyTorch behavior
10
24
 
11
25
  ---
12
26
 
@@ -21,16 +35,13 @@ In this version `1.8.0`, we Introduce the **SoftmaxCrossEntropyLoss**, and **BCE
21
35
  - running simple training loops in the browser
22
36
  - environments where large frameworks are unnecessary or unavailable
23
37
 
24
- `Mini-JSTorch is NOT a replacement for PyTorch, TensorFlow, or TensorFlow.js.`
25
-
26
- `It is intentionally scoped to remain small, readable, and easy to debug.`
38
+ `mini-jstorch is intentionally designed to be small, readable, and easy to debug.`
27
39
 
28
40
  ---
29
41
 
30
42
  # Key Characteristics
31
43
 
32
44
  - Zero dependencies
33
- - ESM-first (`type: module`)
34
45
  - Works in Node.js or others enviornments and browser environments
35
46
  - Explicit, manual forward and backward passes
36
47
  - Focused on 2D training logic (`[batch][features]`)
@@ -135,7 +146,7 @@ In Browser/Website:
135
146
  # Loss Functions
136
147
 
137
148
  - MSELoss
138
- - CrossEntropyLoss (*legacy*)
149
+ - CrossEntropyLoss (*legacy*, use **SoftmaxCrossEntropy** instead)
139
150
  - SoftmaxCrossEntropyLoss (**recommended**)
140
151
  - BCEWithLogitsLoss (**recommended**)
141
152
 
@@ -152,7 +163,7 @@ In Browser/Website:
152
163
  - LambdaLR
153
164
  - ReduceLROnPlateau
154
165
  - Regularization
155
- - Dropout (*basic*, *educational*)
166
+ - Dropout
156
167
  - BatchNorm2D (*experimental*)
157
168
 
158
169
  # Utilities
@@ -203,9 +214,9 @@ import {
203
214
  } from "./src/jstorch.js";
204
215
 
205
216
  const model = new Sequential([
206
- new Linear(2, 4),
217
+ new Linear(2, 8),
207
218
  new ReLU(),
208
- new Linear(4, 2) // logits output
219
+ new Linear(8, 2) // logits output
209
220
  ]);
210
221
 
211
222
  const X = [
@@ -217,7 +228,7 @@ const Y = [
217
228
  ];
218
229
 
219
230
  const lossFn = new SoftmaxCrossEntropyLoss();
220
- const optimizer = new Adam(model.parameters(), 0.1);
231
+ const optimizer = new Adam(model.parameters(), {lr: 0.1});
221
232
 
222
233
  for (let epoch = 1; epoch <= 300; epoch++) {
223
234
  const logits = model.forward(X);
@@ -225,13 +236,16 @@ for (let epoch = 1; epoch <= 300; epoch++) {
225
236
  const grad = lossFn.backward();
226
237
  model.backward(grad);
227
238
  optimizer.step();
239
+
240
+ // Zero gradients for next iteration
241
+ model.zeroGrad();
228
242
 
229
243
  if (epoch % 50 === 0) {
230
- console.log(`Epoch ${epoch}, Loss: ${loss.toFixed(4)}`);
244
+ console.log(`Epoch ${epoch}, Loss: ${loss.toFixed(6)}`);
231
245
  }
232
246
  }
233
247
  ```
234
- Do not combine `SoftmaxCrossEntropyLoss` with a `Softmax` layer.
248
+ `Important:` Do not combine `SoftmaxCrossEntropyLoss` with a `Softmax` layer.
235
249
 
236
250
  # Binary Classifiaction (BCEWithLogitsLoss)
237
251
 
@@ -245,21 +259,20 @@ import {
245
259
  } from "./src/jstorch.js";
246
260
 
247
261
  const model = new Sequential([
248
- new Linear(2, 4),
262
+ new Linear(2, 8),
249
263
  new ReLU(),
250
- new Linear(4, 1) // logit
264
+ new Linear(8, 1) // logit output
251
265
  ]);
252
266
 
253
267
  const X = [
254
268
  [0,0], [0,1], [1,0], [1,1]
255
269
  ];
256
-
257
270
  const Y = [
258
271
  [0], [1], [1], [0]
259
272
  ];
260
273
 
261
274
  const lossFn = new BCEWithLogitsLoss();
262
- const optimizer = new Adam(model.parameters(), 0.1);
275
+ const optimizer = new Adam(model.parameters(), {lr: 0.1});
263
276
 
264
277
  for (let epoch = 1; epoch <= 300; epoch++) {
265
278
  const logits = model.forward(X);
@@ -267,7 +280,37 @@ for (let epoch = 1; epoch <= 300; epoch++) {
267
280
  const grad = lossFn.backward();
268
281
  model.backward(grad);
269
282
  optimizer.step();
283
+ model.zeroGrad();
284
+
285
+ // Print progress every 50 epochs
286
+ if (epoch % 50 === 0) {
287
+ const probs = logits.map(p => 1 / (1 + Math.exp(-p[0])));
288
+ console.log(`Epoch ${epoch} | Loss: ${loss.toFixed(6)}`);
289
+ probs.forEach((prob, i) => {
290
+ const pred = prob > 0.5 ? 1 : 0;
291
+ console.log(` [${X[i]}] → prob: ${prob.toFixed(4)} (${pred}) | target: ${Y[i][0]}`);
292
+ });
293
+ console.log('');
294
+ }
270
295
  }
296
+
297
+ // Final evaluation
298
+ console.log("\nTraining Complete\n");
299
+ model.eval();
300
+
301
+ const finalLogits = model.forward(X);
302
+ const finalProbs = finalLogits.map(p => 1 / (1 + Math.exp(-p[0])));
303
+
304
+ console.log("Final Results:");
305
+ let correct = 0;
306
+ finalProbs.forEach((prob, i) => {
307
+ const pred = prob > 0.5 ? 1 : 0;
308
+ const target = Y[i][0];
309
+ const isCorrect = pred === target;
310
+ if (isCorrect) correct++;
311
+ console.log(` [${X[i]}] → ${prob.toFixed(4)} (${pred}) | target: ${target} ${isCorrect ? '✓' : '✗'}`);
312
+ });
313
+ console.log(`\nAccuracy: ${(correct / X.length * 100).toFixed(2)}%`);
271
314
  ```
272
315
  Do not combine `BCEWithLogitsLoss` with a `Sigmoid` layer.
273
316
 
@@ -276,7 +319,8 @@ Do not combine `BCEWithLogitsLoss` with a `Sigmoid` layer.
276
319
  # Save & Load Models
277
320
 
278
321
  ```javascript
279
- import { saveModel, loadModel, Sequential } from "mini-jstorch";
322
+ // WARN: Error/Bug may be expected for this time!
323
+ import { saveModel, loadModel, Sequential } from "./src/jstorch.js";
280
324
 
281
325
  const json = saveModel(model);
282
326
  const model2 = new Sequential([...]); // same architecture
@@ -287,16 +331,18 @@ loadModel(model2, json);
287
331
 
288
332
  # Demos
289
333
 
290
- See the `demo/` directory for runnable examples:
291
- - `demo/MakeModel.js` – simple training loop
292
- - `demo/scheduler.js` – learning rate schedulers
293
- - `demo/fu_fun.js` – utility functions
334
+ See the `demo/` directory for runnable examples!
335
+ - `demo/fu_fun.js`
336
+ - `demo/MakeModel.js`
337
+ - `demo/scheduler.js`
338
+ - `demo/xor_classification.js`
339
+ - `demo/linear_regression.js`
340
+
294
341
 
295
342
  ```bash
296
- node demo/MakeModel.js
297
- node demo/scheduler.js
298
- node demo/fu_fun.js
343
+ node demo/<fileNameInDemo>.js
299
344
  ```
345
+ **Make sure your directory while run this at root folder!**
300
346
 
301
347
  ---
302
348
 
@@ -310,15 +356,6 @@ node demo/fu_fun.js
310
356
 
311
357
  ---
312
358
 
313
- # Intended Use Cases
314
-
315
- - Learning how neural networks work internally
316
- - Teaching ML fundamentals
317
- - Small experiments in Node.js or the browser
318
- - Lightweight AI demos without GPU or large frameworks
319
-
320
- ---
321
-
322
359
  # License
323
360
 
324
361
  MIT License