scalar-autograd 0.1.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/Value.ts ADDED
@@ -0,0 +1,456 @@
1
+ type BackwardFn = () => void;
2
+
3
+ const EPS = 1e-12;
4
+
5
+ import { ValueTrig } from './ValueTrig';
6
+ import { ValueActivation } from './ValueActivation';
7
+ import { ValueArithmetic } from './ValueArithmetic';
8
+ import { ValueComparison } from './ValueComparison';
9
+
10
+ export class Value {
11
+ static no_grad_mode = false;
12
+ data: number;
13
+ grad: number = 0;
14
+ requiresGrad: boolean;
15
+ private backwardFn: BackwardFn = () => {};
16
+ private prev: Value[] = [];
17
+ public label: string;
18
+
19
+ constructor(data: number, label = "", requiresGrad = false) {
20
+ if (typeof data !== 'number' || Number.isNaN(data) || !Number.isFinite(data)) {
21
+ throw new Error(`Invalid number passed to Value: ${data}`);
22
+ }
23
+ this.data = data;
24
+ this.label = label;
25
+ this.requiresGrad = requiresGrad;
26
+ }
27
+
28
+ private static ensureValue(x: Value | number): Value {
29
+ return typeof x === 'number' ? new Value(x) : x;
30
+ }
31
+
32
+ /**
33
+ * Returns sin(this).
34
+ * @returns New Value with sin.
35
+ */
36
+ sin(): Value {
37
+ return ValueTrig.sin(this);
38
+ }
39
+
40
+ /**
41
+ * Returns cos(this).
42
+ * @returns New Value with cos.
43
+ */
44
+ cos(): Value {
45
+ return ValueTrig.cos(this);
46
+ }
47
+
48
+ /**
49
+ * Returns tan(this).
50
+ * @returns New Value with tan.
51
+ */
52
+ tan(): Value {
53
+ return ValueTrig.tan(this);
54
+ }
55
+
56
+ /**
57
+ * Returns asin(this).
58
+ * @returns New Value with asin.
59
+ */
60
+ asin(): Value {
61
+ return ValueTrig.asin(this);
62
+ }
63
+
64
+ /**
65
+ * Returns acos(this).
66
+ * @returns New Value with acos.
67
+ */
68
+ acos(): Value {
69
+ return ValueTrig.acos(this);
70
+ }
71
+
72
+ /**
73
+ * Returns atan(this).
74
+ * @returns New Value with atan.
75
+ */
76
+ atan(): Value {
77
+ return ValueTrig.atan(this);
78
+ }
79
+
80
+ /**
81
+ * Returns relu(this).
82
+ * @returns New Value with relu.
83
+ */
84
+ relu(): Value {
85
+ return ValueActivation.relu(this);
86
+ }
87
+
88
+ /**
89
+ * Returns abs(this).
90
+ * @returns New Value with abs.
91
+ */
92
+ abs(): Value {
93
+ return ValueArithmetic.abs(this);
94
+ }
95
+
96
+ /**
97
+ * Returns exp(this).
98
+ * @returns New Value with exp.
99
+ */
100
+ exp(): Value {
101
+ return ValueArithmetic.exp(this);
102
+ }
103
+
104
+ /**
105
+ * Returns log(this).
106
+ * @returns New Value with log.
107
+ */
108
+ log(): Value {
109
+ return ValueArithmetic.log(this, EPS);
110
+ }
111
+
112
+ /**
113
+ * Returns min(this, other).
114
+ * @param other Value to compare
115
+ * @returns New Value with min.
116
+ */
117
+ min(other: Value): Value {
118
+ return ValueArithmetic.min(this, other);
119
+ }
120
+
121
+ /**
122
+ * Returns max(this, other).
123
+ * @param other Value to compare
124
+ * @returns New Value with max.
125
+ */
126
+ max(other: Value): Value {
127
+ return ValueArithmetic.max(this, other);
128
+ }
129
+
130
+ /**
131
+ * Adds this and other.
132
+ * @param other Value or number to add
133
+ * @returns New Value with sum.
134
+ */
135
+ add(other: Value | number): Value {
136
+ return ValueArithmetic.add(this, Value.ensureValue(other));
137
+ }
138
+ /**
139
+ * Multiplies this and other.
140
+ * @param other Value or number to multiply
141
+ * @returns New Value with product.
142
+ */
143
+ mul(other: Value | number): Value {
144
+ return ValueArithmetic.mul(this, Value.ensureValue(other));
145
+ }
146
+
147
+ /**
148
+ * Subtracts other from this.
149
+ * @param other Value or number to subtract
150
+ * @returns New Value with difference.
151
+ */
152
+ sub(other: Value | number): Value {
153
+ return ValueArithmetic.sub(this, Value.ensureValue(other));
154
+ }
155
+
156
+ /**
157
+ * Divides this by other.
158
+ * @param other Value or number divisor
159
+ * @returns New Value with quotient.
160
+ */
161
+ div(other: Value | number): Value {
162
+ return ValueArithmetic.div(this, Value.ensureValue(other), EPS);
163
+ }
164
+
165
+ /**
166
+ * Raises this to the power exp.
167
+ * @param exp Exponent
168
+ * @returns New Value with pow(this, exp)
169
+ */
170
+ pow(exp: number): Value {
171
+ return ValueArithmetic.pow(this, exp);
172
+ }
173
+
174
+ /**
175
+ * Raises this to a dynamic Value (other).
176
+ * @param other Exponent Value or number
177
+ * @returns New Value with pow(this, other)
178
+ */
179
+ powValue(other: Value | number): Value {
180
+ return ValueArithmetic.powValue(this, Value.ensureValue(other), EPS);
181
+ }
182
+
183
+ /**
184
+ * Returns this modulo other.
185
+ * @param other Divisor Value
186
+ * @returns New Value with modulo.
187
+ */
188
+ mod(other: Value): Value {
189
+ return ValueArithmetic.mod(this, other);
190
+ }
191
+
192
+ /**
193
+ * Returns Value indicating if this equals other.
194
+ * @param other Value to compare
195
+ * @returns New Value (1 if equal, else 0)
196
+ */
197
+ eq(other: Value): Value {
198
+ return ValueComparison.eq(this, other);
199
+ }
200
+ /**
201
+ * Returns Value indicating if this not equals other.
202
+ * @param other Value to compare
203
+ * @returns New Value (1 if not equal, else 0)
204
+ */
205
+ neq(other: Value): Value {
206
+ return ValueComparison.neq(this, other);
207
+ }
208
+ /**
209
+ * Returns Value indicating if this greater than other.
210
+ * @param other Value to compare
211
+ * @returns New Value (1 if true, else 0)
212
+ */
213
+ gt(other: Value): Value {
214
+ return ValueComparison.gt(this, other);
215
+ }
216
+ /**
217
+ * Returns Value indicating if this less than other.
218
+ * @param other Value to compare
219
+ * @returns New Value (1 if true, else 0)
220
+ */
221
+ lt(other: Value): Value {
222
+ return ValueComparison.lt(this, other);
223
+ }
224
+ /**
225
+ * Returns Value indicating if this greater than or equal to other.
226
+ * @param other Value to compare
227
+ * @returns New Value (1 if true, else 0)
228
+ */
229
+ gte(other: Value): Value {
230
+ return ValueComparison.gte(this, other);
231
+ }
232
+ /**
233
+ * Returns Value indicating if this less than or equal to other.
234
+ * @param other Value to compare
235
+ * @returns New Value (1 if true, else 0)
236
+ */
237
+ lte(other: Value): Value {
238
+ return ValueComparison.lte(this, other);
239
+ }
240
+
241
+ /**
242
+ * Returns softplus(this).
243
+ * @returns New Value with softplus.
244
+ */
245
+ softplus(): Value {
246
+ return ValueActivation.softplus(this);
247
+ }
248
+
249
+ /**
250
+ * Returns the floor of this Value.
251
+ * @returns New Value with floor(data).
252
+ */
253
+ floor(): Value {
254
+ return ValueArithmetic.floor(this);
255
+ }
256
+ /**
257
+ * Returns the ceiling of this Value.
258
+ * @returns New Value with ceil(data).
259
+ */
260
+ ceil(): Value {
261
+ return ValueArithmetic.ceil(this);
262
+ }
263
+ /**
264
+ * Returns the rounded value of this Value.
265
+ * @returns New Value with rounded data.
266
+ */
267
+ round(): Value {
268
+ return ValueArithmetic.round(this);
269
+ }
270
+ /**
271
+ * Returns the square of this Value.
272
+ * @returns New Value with squared data.
273
+ */
274
+ square(): Value {
275
+ return ValueArithmetic.square(this);
276
+ }
277
+ /**
278
+ * Returns the cube of this Value.
279
+ * @returns New Value with cubed data.
280
+ */
281
+ cube(): Value {
282
+ return ValueArithmetic.cube(this);
283
+ }
284
+ /**
285
+ * Returns the reciprocal (1/x) of this Value.
286
+ * @returns New Value with reciprocal.
287
+ */
288
+ reciprocal(): Value {
289
+ return ValueArithmetic.reciprocal(this, EPS);
290
+ }
291
+
292
+ /**
293
+ * Clamps this between min and max.
294
+ * @param min Minimum value
295
+ * @param max Maximum value
296
+ * @returns New clamped Value
297
+ */
298
+ clamp(min: number, max: number): Value {
299
+ return ValueArithmetic.clamp(this, min, max);
300
+ }
301
+
302
+ /**
303
+ * Returns the negation (-this) Value.
304
+ * @returns New Value which is the negation.
305
+ */
306
+ neg(): Value {
307
+ return ValueArithmetic.neg(this);
308
+ }
309
+
310
+ /**
311
+ * Returns the sum of the given Values.
312
+ * @param vals Array of Value objects
313
+ * @returns New Value holding their sum.
314
+ */
315
+ static sum(vals: Value[]): Value {
316
+ return ValueArithmetic.sum(vals);
317
+ }
318
+
319
+ /**
320
+ * Returns the mean of the given Values.
321
+ * @param vals Array of Value objects
322
+ * @returns New Value holding their mean.
323
+ */
324
+ static mean(vals: Value[]): Value {
325
+ return ValueArithmetic.mean(vals);
326
+ }
327
+
328
+ /**
329
+ * Returns tanh(this).
330
+ * @returns New Value with tanh.
331
+ */
332
+ tanh(): Value {
333
+ return ValueActivation.tanh(this);
334
+ }
335
+
336
+ /**
337
+ * Returns sigmoid(this).
338
+ * @returns New Value with sigmoid.
339
+ */
340
+ sigmoid(): Value {
341
+ return ValueActivation.sigmoid(this);
342
+ }
343
+
344
+ /**
345
+ * Performs a reverse-mode autodiff backward pass from this Value.
346
+ * @param zeroGrad If true, zeroes all grads in the graph before backward
347
+ */
348
+ backward(zeroGrad = false): void {
349
+ // Only allow backward on scalars (not arrays), i.e. single value outputs
350
+ // (output shape check is redundant for this codebase, but keep to scalar-by-convention)
351
+ if (zeroGrad) Value.zeroGradTree(this);
352
+
353
+ const topo: Value[] = [];
354
+ const visited = new Set<Value>();
355
+
356
+ const buildTopo = (v: Value) => {
357
+ if (!visited.has(v)) {
358
+ visited.add(v);
359
+ for (const child of v.prev) {
360
+ buildTopo(child);
361
+ }
362
+ topo.push(v);
363
+ }
364
+ };
365
+
366
+ buildTopo(this);
367
+ this.grad = 1;
368
+
369
+ for (let i = topo.length - 1; i >= 0; i--) {
370
+ if (topo[i].requiresGrad) {
371
+ topo[i].backwardFn();
372
+ }
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Sets all grad fields in the computation tree (from root) to 0.
378
+ * @param root Value to zero tree from
379
+ */
380
+ static zeroGradTree(root: Value): void {
381
+ const visited = new Set<Value>();
382
+ const visit = (v: Value) => {
383
+ if (!visited.has(v)) {
384
+ visited.add(v);
385
+ v.grad = 0;
386
+ for (const child of v.prev) visit(child);
387
+ }
388
+ };
389
+ visit(root);
390
+ }
391
+
392
+ /**
393
+ * Sets all grad fields in all supplied trees to 0.
394
+ * @param vals Values whose trees to zero
395
+ */
396
+ static zeroGradAll(vals: Value[]): void {
397
+ const visited = new Set<Value>();
398
+ for (const v of vals) {
399
+ const visit = (u: Value) => {
400
+ if (!visited.has(u)) {
401
+ visited.add(u);
402
+ u.grad = 0;
403
+ for (const child of u.prev) visit(child);
404
+ }
405
+ };
406
+ visit(v);
407
+ }
408
+ }
409
+
410
+ /**
411
+ * Internal helper to construct a Value with correct backward fn and grads.
412
+ * @param data Output value data
413
+ * @param left Left operand Value
414
+ * @param right Right operand Value or null
415
+ * @param backwardFnBuilder Function to create backward closure
416
+ * @param label Node label for debugging
417
+ * @returns New Value node
418
+ */
419
+ static make(
420
+ data: number,
421
+ left: Value,
422
+ right: Value | null,
423
+ backwardFnBuilder: (out: Value) => BackwardFn,
424
+ label: string
425
+ ): Value {
426
+ const requiresGrad = !Value.no_grad_mode && [left, right].filter(Boolean).some(v => v!.requiresGrad);
427
+ const out = new Value(data, label, requiresGrad);
428
+ out.prev = Value.no_grad_mode ? [] : ([left, right].filter(Boolean) as Value[]);
429
+ if (requiresGrad) {
430
+ out.backwardFn = backwardFnBuilder(out);
431
+ }
432
+ return out;
433
+ }
434
+
435
+ /**
436
+ * Returns string representation for debugging.
437
+ * @returns String summary of Value
438
+ */
439
+ toString(): string {
440
+ return `Value(data=${this.data.toFixed(4)}, grad=${this.grad.toFixed(4)}, label=${this.label})`;
441
+ }
442
+
443
+ /**
444
+ * Temporarily disables gradient tracking within the callback scope, like torch.no_grad().
445
+ * Restores the previous state after running fn.
446
+ */
447
+ static withNoGrad<T>(fn: () => T): T {
448
+ const prev = Value.no_grad_mode;
449
+ Value.no_grad_mode = true;
450
+ try {
451
+ return fn();
452
+ } finally {
453
+ Value.no_grad_mode = prev;
454
+ }
455
+ }
456
+ }
@@ -0,0 +1,51 @@
1
+ import { Value } from './Value';
2
+
3
+ export class ValueActivation {
4
+ static relu(x: Value): Value {
5
+ const r = Math.max(0, x.data);
6
+ return Value.make(
7
+ r,
8
+ x, null,
9
+ (out) => () => {
10
+ if (x.requiresGrad) x.grad += (x.data > 0 ? 1 : 0) * out.grad;
11
+ },
12
+ `relu(${x.label})`
13
+ );
14
+ }
15
+
16
+ static softplus(x: Value): Value {
17
+ const s = Math.log(1 + Math.exp(x.data));
18
+ return Value.make(
19
+ s,
20
+ x, null,
21
+ (out) => () => {
22
+ x.grad += 1 / (1 + Math.exp(-x.data)) * out.grad;
23
+ },
24
+ `softplus(${x.label})`
25
+ );
26
+ }
27
+
28
+ static tanh(x: Value): Value {
29
+ const t = Math.tanh(x.data);
30
+ return Value.make(
31
+ t,
32
+ x, null,
33
+ (out) => () => {
34
+ if (x.requiresGrad) x.grad += (1 - t ** 2) * out.grad;
35
+ },
36
+ `tanh(${x.label})`
37
+ );
38
+ }
39
+
40
+ static sigmoid(x: Value): Value {
41
+ const s = 1 / (1 + Math.exp(-x.data));
42
+ return Value.make(
43
+ s,
44
+ x, null,
45
+ (out) => () => {
46
+ if (x.requiresGrad) x.grad += s * (1 - s) * out.grad;
47
+ },
48
+ `sigmoid(${x.label})`
49
+ );
50
+ }
51
+ }