@tangent.to/opt 0.2.0 → 0.2.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/src/numdiff.js CHANGED
@@ -1,15 +1,17 @@
1
- /**
1
+ /*
2
2
  * Numerical differentiation utilities
3
3
  */
4
4
 
5
5
  /**
6
6
  * Approximate the gradient of a scalar function by central finite differences.
7
7
  *
8
- * @param {Function} f - Scalar function (x: Array<number>) => number
9
- * @param {Array<number>} x - Point at which to evaluate the gradient
8
+ * @param {(x: number[]) => number} f - Scalar function
9
+ * @param {number[]} x - Point at which to evaluate the gradient
10
10
  * @param {Object} [options]
11
- * @param {number} [options.h=1e-6] - Step size
12
- * @returns {Array<number>} Gradient approximation
11
+ * @param {number} [options.h=1e-6] - Absolute step size
12
+ * @returns {number[]} Gradient approximation
13
+ * @example
14
+ * numericalGradient(([a, b]) => a * a + 3 * b, [1, 2]); // ≈ [2, 3]
13
15
  */
14
16
  export function numericalGradient(f, x, options = {}) {
15
17
  const h = options.h || 1e-6;
@@ -30,14 +32,15 @@ export function numericalGradient(f, x, options = {}) {
30
32
  }
31
33
 
32
34
  /**
33
- * Central finite-difference Jacobian of a vector-valued function.
35
+ * Approximate the Jacobian of a vector-valued function by central finite differences.
36
+ *
34
37
  * Per-component step h_j = step * max(1, |x_j|).
35
38
  *
36
- * @param {Function} f - (x: Array<number>) => Array<number> of length m
37
- * @param {Array<number>} x - Point of length n
39
+ * @param {(x: number[]) => ArrayLike<number>} f - Function returning m values
40
+ * @param {number[]} x - Point of length n
38
41
  * @param {Object} [options]
39
42
  * @param {number} [options.step=1e-6] - Relative step size
40
- * @returns {Array<Array<number>>} m-by-n Jacobian
43
+ * @returns {number[][]} m-by-n Jacobian, `J[i][j] = d f_i / d x_j`
41
44
  */
42
45
  export function numericalJacobian(f, x, options = {}) {
43
46
  const step = options.step || 1e-6;
@@ -67,13 +70,15 @@ export function numericalJacobian(f, x, options = {}) {
67
70
  }
68
71
 
69
72
  /**
70
- * Central finite-difference Hessian of a scalar function.
73
+ * Approximate the Hessian of a scalar function by central finite differences.
74
+ *
75
+ * Per-component step h_j = step * max(1, |x_j|).
71
76
  *
72
- * @param {Function} f - (x: Array<number>) => number
73
- * @param {Array<number>} x - Point of length n
77
+ * @param {(x: number[]) => number} f - Scalar function
78
+ * @param {number[]} x - Point of length n
74
79
  * @param {Object} [options]
75
80
  * @param {number} [options.step=1e-4] - Relative step size
76
- * @returns {Array<Array<number>>} n-by-n symmetric Hessian
81
+ * @returns {number[][]} n-by-n symmetric Hessian
77
82
  */
78
83
  export function numericalHessian(f, x, options = {}) {
79
84
  const h = options.step || 1e-4;
package/src/scalar.js CHANGED
@@ -324,14 +324,19 @@ function goldenMinimize(fn, xa, xb, xc, fxb, xTol, maxIter) {
324
324
  /**
325
325
  * Minimize a univariate function.
326
326
  *
327
- * @param {Function} f - Objective: (x: number) => number
327
+ * @param {(x: number) => number} f - Objective
328
328
  * @param {Object} [options]
329
- * @param {string} [options.method='brent'] - 'brent' or 'golden'
330
- * @param {Array<number>} [options.bracket=[0, 1]] - [a, b] to auto-bracket
331
- * from, or a full bracketing triple [a, b, c] with f(b) <= f(a), f(b) <= f(c)
329
+ * @param {'brent'|'golden'} [options.method='brent'] - Brent's method or
330
+ * golden-section search; case-insensitive
331
+ * @param {number[]} [options.bracket=[0, 1]] - [a, b] to auto-bracket
332
+ * from (downhill), or a full bracketing triple [a, b, c] with f(b) <= f(a), f(b) <= f(c)
332
333
  * @param {number} [options.xTol=1e-8] - Relative tolerance on x
333
334
  * @param {number} [options.maxIter=500] - Maximum iterations
334
- * @returns {Object} {x, fx, iterations, fevals, converged}
335
+ * @returns {{x: number, fx: number, iterations: number, nfev: number, fevals: number, converged: boolean, success: boolean}}
336
+ * `fevals` is a deprecated alias of `nfev`; `success` equals `converged`
337
+ * @throws {Error} if no minimum can be bracketed (f monotonic or unbounded below)
338
+ * @example
339
+ * minimizeScalar(Math.cos, { bracket: [2, 4] }).x; // 3.14159265441603
335
340
  */
336
341
  export function minimizeScalar(f, options = {}) {
337
342
  if (typeof f !== 'function') {
@@ -552,15 +557,20 @@ function bisectRoot(fn, a0, b0, fa0, xTol, rTol, maxIter) {
552
557
  /**
553
558
  * Find a root of a univariate function inside a sign-changing bracket.
554
559
  *
555
- * @param {Function} f - Function: (x: number) => number
556
- * @param {Object} [options]
557
- * @param {string} [options.method='brent'] - 'brent' or 'bisect'
558
- * @param {Array<number>} options.bracket - [a, b] with f(a) and f(b) of
559
- * opposite signs (required)
560
+ * @param {(x: number) => number} f - Function
561
+ * @param {Object} options
562
+ * @param {number[]} options.bracket - [a, b] with f(a) and f(b) of opposite
563
+ * signs (required)
564
+ * @param {'brent'|'bisect'} [options.method='brent'] - Brent-Dekker or
565
+ * bisection; case-insensitive
560
566
  * @param {number} [options.xTol=1e-12] - Absolute tolerance on x
561
567
  * @param {number} [options.rTol=4 * Number.EPSILON] - Relative tolerance on x
562
568
  * @param {number} [options.maxIter=100] - Maximum iterations
563
- * @returns {Object} {x, fx, iterations, fevals, converged}
569
+ * @returns {{x: number, fx: number, iterations: number, nfev: number, fevals: number, converged: boolean, success: boolean}}
570
+ * `fevals` is a deprecated alias of `nfev`; `success` equals `converged`
571
+ * @throws {Error} if f(a) and f(b) have the same sign, or f is not finite
572
+ * @example
573
+ * rootScalar(Math.cos, { bracket: [1, 2] }).x; // 1.5707963267948966
564
574
  */
565
575
  export function rootScalar(f, options = {}) {
566
576
  if (typeof f !== 'function') {
package/src/steps.js CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /*
2
2
  * Update rules, one step at a time.
3
3
  *
4
4
  * The drivers in gradient.js run a whole minimization: they own the loop,
@@ -19,16 +19,17 @@
19
19
  * state per tensor.
20
20
  */
21
21
 
22
- /** @typedef {{ learningRate?: number }} StepOptions */
23
-
24
22
  /**
25
- * Plain gradient descent: `x -= lr · g`. Stateless; returns `null` so it has
26
- * the signature of the others.
23
+ * Take one plain gradient-descent step in place: `x -= lr · g`.
24
+ *
25
+ * Stateless; returns `null` so it has the signature of the other steps,
26
+ * `state = step(x, gradient, state, options)`.
27
27
  *
28
28
  * @param {number[]|Float64Array} x - moved in place
29
- * @param {number[]|Float64Array} gradient
30
- * @param {null} [_state]
31
- * @param {StepOptions} [options]
29
+ * @param {ArrayLike<number>} gradient - same length as x
30
+ * @param {null} [_state] - unused
31
+ * @param {Object} [options] - read on every call
32
+ * @param {number} [options.learningRate=0.01] - Step size
32
33
  * @returns {null}
33
34
  */
34
35
  export function gradientStep(x, gradient, _state = null, options = {}) {
@@ -38,13 +39,16 @@ export function gradientStep(x, gradient, _state = null, options = {}) {
38
39
  }
39
40
 
40
41
  /**
41
- * Gradient descent with momentum: `v = μ v + lr · g; x -= v`.
42
+ * Take one gradient-descent-with-momentum step in place: `v = μ v + lr · g; x -= v`.
42
43
  *
43
44
  * @param {number[]|Float64Array} x - moved in place
44
- * @param {number[]|Float64Array} gradient
45
- * @param {{ velocity: Float64Array }|null} [state]
46
- * @param {StepOptions & { momentum?: number }} [options]
47
- * @returns {{ velocity: Float64Array }}
45
+ * @param {ArrayLike<number>} gradient - same length as x
46
+ * @param {{ velocity: Float64Array }|null} [state] - from the previous call;
47
+ * null to start
48
+ * @param {Object} [options] - read on every call
49
+ * @param {number} [options.learningRate=0.01] - Step size
50
+ * @param {number} [options.momentum=0.9] - μ
51
+ * @returns {{ velocity: Float64Array }} the state, to pass to the next call
48
52
  */
49
53
  export function momentumStep(x, gradient, state = null, options = {}) {
50
54
  const lr = options.learningRate ?? 0.01;
@@ -59,13 +63,17 @@ export function momentumStep(x, gradient, state = null, options = {}) {
59
63
  }
60
64
 
61
65
  /**
62
- * RMSProp: a running mean of squared gradients scales each coordinate's step.
66
+ * Take one RMSProp step in place: a running mean of squared gradients scales each coordinate's step.
63
67
  *
64
68
  * @param {number[]|Float64Array} x - moved in place
65
- * @param {number[]|Float64Array} gradient
66
- * @param {{ cache: Float64Array }|null} [state]
67
- * @param {StepOptions & { decay?: number, epsilon?: number }} [options]
68
- * @returns {{ cache: Float64Array }}
69
+ * @param {ArrayLike<number>} gradient - same length as x
70
+ * @param {{ cache: Float64Array }|null} [state] - from the previous call;
71
+ * null to start
72
+ * @param {Object} [options] - read on every call
73
+ * @param {number} [options.learningRate=0.01] - Step size
74
+ * @param {number} [options.decay=0.9] - Decay of the running mean
75
+ * @param {number} [options.epsilon=1e-8] - Added to the denominator
76
+ * @returns {{ cache: Float64Array }} the state, to pass to the next call
69
77
  */
70
78
  export function rmspropStep(x, gradient, state = null, options = {}) {
71
79
  const lr = options.learningRate ?? 0.01;
@@ -82,15 +90,29 @@ export function rmspropStep(x, gradient, state = null, options = {}) {
82
90
  }
83
91
 
84
92
  /**
85
- * Adam (Kingma and Ba, 2015): bias-corrected first and second moments. The
86
- * step count lives in the state, so bias correction is right however many
87
- * tensors share one options object.
93
+ * Take one Adam step in place (Kingma and Ba, 2015): bias-corrected first and second moments.
94
+ *
95
+ * Keep one state per parameter tensor. The step count lives in the state, so
96
+ * bias correction is right however many tensors share one options object.
88
97
  *
89
98
  * @param {number[]|Float64Array} x - moved in place
90
- * @param {number[]|Float64Array} gradient
91
- * @param {{ m: Float64Array, v: Float64Array, t: number }|null} [state]
92
- * @param {StepOptions & { beta1?: number, beta2?: number, epsilon?: number }} [options]
93
- * @returns {{ m: Float64Array, v: Float64Array, t: number }}
99
+ * @param {ArrayLike<number>} gradient - same length as x
100
+ * @param {{ m: Float64Array, v: Float64Array, t: number }|null} [state] - from
101
+ * the previous call; null to start
102
+ * @param {Object} [options] - read on every call
103
+ * @param {number} [options.learningRate=0.01] - Step size
104
+ * @param {number} [options.beta1=0.9] - First-moment decay
105
+ * @param {number} [options.beta2=0.999] - Second-moment decay
106
+ * @param {number} [options.epsilon=1e-8] - Added to the denominator
107
+ * @returns {{ m: Float64Array, v: Float64Array, t: number }} the state, to
108
+ * pass to the next call
109
+ * @example
110
+ * const x = [3, -2];
111
+ * let state = null;
112
+ * for (let i = 0; i < 500; i++) {
113
+ * state = adamStep(x, [2 * x[0], 2 * x[1]], state, { learningRate: 0.1 });
114
+ * }
115
+ * // x ≈ [0, 0]
94
116
  */
95
117
  export function adamStep(x, gradient, state = null, options = {}) {
96
118
  const lr = options.learningRate ?? 0.01;
package/src/version.js ADDED
@@ -0,0 +1,2 @@
1
+ /** This package's version. */
2
+ export const version = '0.2.2';
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Dense vector helpers (BLAS level-1 style).
3
+ *
4
+ * Ported from fmin (https://github.com/benfred/fmin),
5
+ * Copyright 2016, Ben Frederickson, BSD-3-Clause.
6
+ * See THIRD_PARTY_NOTICES.md.
7
+ */
8
+ /**
9
+ * Dot product of two vectors
10
+ * @param {Array<number>} a
11
+ * @param {Array<number>} b
12
+ * @returns {number}
13
+ */
14
+ export function dot(a: Array<number>, b: Array<number>): number;
15
+ /**
16
+ * Euclidean norm of a vector
17
+ * @param {Array<number>} a
18
+ * @returns {number}
19
+ */
20
+ export function norm2(a: Array<number>): number;
21
+ /**
22
+ * In-place weighted sum: out = w1 * v1 + w2 * v2
23
+ * @param {Array<number>} out - Output vector (mutated)
24
+ * @param {number} w1
25
+ * @param {Array<number>} v1
26
+ * @param {number} w2
27
+ * @param {Array<number>} v2
28
+ */
29
+ export function weightedSum(out: Array<number>, w1: number, v1: Array<number>, w2: number, v2: Array<number>): void;
30
+ /**
31
+ * In-place scale: out = c * value
32
+ * @param {Array<number>} out - Output vector (mutated)
33
+ * @param {Array<number>} value
34
+ * @param {number} c
35
+ */
36
+ export function scale(out: Array<number>, value: Array<number>, c: number): void;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Build the transform between bounded parameters and unbounded internal ones, or null when every parameter is free.
3
+ *
4
+ * Both bounds: `ext = lo + (hi - lo) * (sin(int) + 1) / 2`; lower only:
5
+ * `ext = lo - 1 + sqrt(int^2 + 1)`; upper only: `ext = hi + 1 - sqrt(int^2 + 1)`.
6
+ * This is MINUIT's transform; `minimize`, `leastSquares` and `curveFit` use
7
+ * it for their `bounds`.
8
+ *
9
+ * @param {Array<[number|null|undefined, number|null|undefined]|null|undefined>} bounds - Per-parameter
10
+ * [lo, hi]; null/undefined/±Infinity mean unbounded on that side
11
+ * @param {number} n - Number of parameters
12
+ * @returns {{toInternal: (xExt: number[]) => number[], toExternal: (xInt: number[]) => number[], dExtDInt: (xInt: number[]) => number[], clamp: (xExt: number[]) => number[]}|null}
13
+ * `toInternal` first clamps a point on or past a bound to just inside it;
14
+ * `dExtDInt` is d ext_i / d int_i at an internal point
15
+ * @throws {Error} if `bounds.length !== n` or a lower bound is not below its upper
16
+ */
17
+ export function makeBoundsTransform(bounds: Array<[number | null | undefined, number | null | undefined] | null | undefined>, n: number): {
18
+ toInternal: (xExt: number[]) => number[];
19
+ toExternal: (xInt: number[]) => number[];
20
+ dExtDInt: (xInt: number[]) => number[];
21
+ clamp: (xExt: number[]) => number[];
22
+ } | null;
23
+ /**
24
+ * Wrap an objective (and optional gradient) so optimizers can run in
25
+ * internal space. Supports scalar objectives and combined objectives
26
+ * returning either {loss, gradient} or {value, gradient}.
27
+ *
28
+ * @param {Function} f - External objective
29
+ * @param {Function|undefined} grad - External gradient
30
+ * @param {Object} T - Transform from makeBoundsTransform
31
+ * @returns {{f: Function, grad: Function|undefined}}
32
+ */
33
+ export function wrapObjective(f: Function, grad: Function | undefined, T: Object): {
34
+ f: Function;
35
+ grad: Function | undefined;
36
+ };
37
+ /**
38
+ * Wrap residuals (and optional Jacobian) for bounded least squares.
39
+ *
40
+ * @param {Function} residuals - External residuals (p) => Array<number>
41
+ * @param {Function|undefined} jacobian - External Jacobian (p) => m-by-n
42
+ * @param {Object} T - Transform from makeBoundsTransform
43
+ * @returns {{residuals: Function, jacobian: Function|undefined}}
44
+ */
45
+ export function wrapResiduals(residuals: Function, jacobian: Function | undefined, T: Object): {
46
+ residuals: Function;
47
+ jacobian: Function | undefined;
48
+ };
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Create a `tangent/ds`-style optimizer by name.
3
+ *
4
+ * @param {'gd'|'gradient_descent'|'sgd'|'momentum'|'rmsprop'|'adam'} name - Case-insensitive;
5
+ * `'gd'`, `'gradient_descent'` and `'sgd'` all give a `GradientDescent`
6
+ * @param {Object} [options] - The class's constructor options
7
+ * @returns {GradientDescent|MomentumOptimizer|RMSProp|AdamOptimizer}
8
+ * @throws {Error} for an unknown name
9
+ * @example
10
+ * const opt = createOptimizer('adam', { learningRate: 0.1 });
11
+ * opt.minimize((x) => ({ loss: x[0] ** 2, gradient: [2 * x[0]] }), [3]).x; // ≈ [0]
12
+ */
13
+ export function createOptimizer(name: "gd" | "gradient_descent" | "sgd" | "momentum" | "rmsprop" | "adam", options?: Object): GradientDescent | MomentumOptimizer | RMSProp | AdamOptimizer;
14
+ /**
15
+ * Batch gradient descent, with optional backtracking line search, as a `tangent/ds`-style optimizer object: `minimize(lossFn, x0)` returns `{x, history}`.
16
+ *
17
+ * New code can call `gradientDescent` or `minimize` directly.
18
+ */
19
+ export class GradientDescent extends Optimizer {
20
+ /**
21
+ * @param {Object} [options]
22
+ * @param {number} [options.learningRate=0.01] - Step size
23
+ * @param {number} [options.maxIter=1000] - Maximum iterations
24
+ * @param {number} [options.tol=1e-6] - Convergence tolerance on the gradient norm
25
+ * @param {boolean} [options.verbose=false] - Log progress every 100 iterations
26
+ * @param {boolean} [options.lineSearch=false] - Backtracking line search in place of the fixed step
27
+ * @param {boolean} [options.stochastic=false] - Stored, unused
28
+ * @param {number} [options.batchSize=32] - Stored, unused
29
+ */
30
+ constructor(options?: {
31
+ learningRate?: number | undefined;
32
+ maxIter?: number | undefined;
33
+ tol?: number | undefined;
34
+ verbose?: boolean | undefined;
35
+ lineSearch?: boolean | undefined;
36
+ stochastic?: boolean | undefined;
37
+ batchSize?: number | undefined;
38
+ });
39
+ stochastic: boolean;
40
+ batchSize: number;
41
+ lineSearch: boolean;
42
+ /**
43
+ * Minimize `lossFn` from `x0`.
44
+ * @param {(x: number[]) => number | {loss: number, gradient: number[]|Float64Array} | {value: number, gradient: number[]|Float64Array}} lossFn
45
+ * - Objective; usually `{loss, gradient}`, finite differences for a number
46
+ * @param {number[]} x0 - Initial parameters
47
+ * @param {Object} [options] - Overrides for this call
48
+ * @param {number} [options.maxIter] - Maximum iterations
49
+ * @param {number} [options.tol] - Gradient-norm tolerance
50
+ * @returns {{x: number[], history: {loss: number[], gradNorm: number[], learningRate: number[]}}}
51
+ */
52
+ minimize(lossFn: (x: number[]) => number | {
53
+ loss: number;
54
+ gradient: number[] | Float64Array;
55
+ } | {
56
+ value: number;
57
+ gradient: number[] | Float64Array;
58
+ }, x0: number[], options?: {
59
+ maxIter?: number | undefined;
60
+ tol?: number | undefined;
61
+ }): {
62
+ x: number[];
63
+ history: {
64
+ loss: number[];
65
+ gradNorm: number[];
66
+ learningRate: number[];
67
+ };
68
+ };
69
+ }
70
+ /**
71
+ * Gradient descent with momentum as a `tangent/ds`-style optimizer object: `minimize(lossFn, x0)` returns `{x, history}`.
72
+ *
73
+ * New code can call `momentumDescent` or `minimize` directly.
74
+ */
75
+ export class MomentumOptimizer extends Optimizer {
76
+ /**
77
+ * @param {Object} [options]
78
+ * @param {number} [options.learningRate=0.01] - Step size
79
+ * @param {number} [options.maxIter=1000] - Maximum iterations
80
+ * @param {number} [options.tol=1e-6] - Convergence tolerance on the gradient norm
81
+ * @param {boolean} [options.verbose=false] - Log progress every 100 iterations
82
+ * @param {number} [options.momentum=0.9] - Momentum coefficient
83
+ */
84
+ constructor(options?: {
85
+ learningRate?: number | undefined;
86
+ maxIter?: number | undefined;
87
+ tol?: number | undefined;
88
+ verbose?: boolean | undefined;
89
+ momentum?: number | undefined;
90
+ });
91
+ momentum: number;
92
+ /**
93
+ * Minimize `lossFn` from `x0`.
94
+ * @param {(x: number[]) => number | {loss: number, gradient: number[]|Float64Array} | {value: number, gradient: number[]|Float64Array}} lossFn
95
+ * - Objective; usually `{loss, gradient}`, finite differences for a number
96
+ * @param {number[]} x0 - Initial parameters
97
+ * @param {Object} [options] - Overrides for this call
98
+ * @param {number} [options.maxIter] - Maximum iterations
99
+ * @param {number} [options.tol] - Gradient-norm tolerance
100
+ * @returns {{x: number[], history: {loss: number[], gradNorm: number[]}}}
101
+ */
102
+ minimize(lossFn: (x: number[]) => number | {
103
+ loss: number;
104
+ gradient: number[] | Float64Array;
105
+ } | {
106
+ value: number;
107
+ gradient: number[] | Float64Array;
108
+ }, x0: number[], options?: {
109
+ maxIter?: number | undefined;
110
+ tol?: number | undefined;
111
+ }): {
112
+ x: number[];
113
+ history: {
114
+ loss: number[];
115
+ gradNorm: number[];
116
+ };
117
+ };
118
+ }
119
+ /**
120
+ * RMSProp as a `tangent/ds`-style optimizer object: `minimize(lossFn, x0)` returns `{x, history}`.
121
+ *
122
+ * New code can call `rmsprop` or `minimize` directly.
123
+ */
124
+ export class RMSProp extends Optimizer {
125
+ /**
126
+ * @param {Object} [options]
127
+ * @param {number} [options.learningRate=0.01] - Step size
128
+ * @param {number} [options.maxIter=1000] - Maximum iterations
129
+ * @param {number} [options.tol=1e-6] - Convergence tolerance on the gradient norm
130
+ * @param {boolean} [options.verbose=false] - Log progress every 100 iterations
131
+ * @param {number} [options.decay=0.9] - Decay of the running mean of squared gradients
132
+ * @param {number} [options.epsilon=1e-8] - Added to the denominator
133
+ */
134
+ constructor(options?: {
135
+ learningRate?: number | undefined;
136
+ maxIter?: number | undefined;
137
+ tol?: number | undefined;
138
+ verbose?: boolean | undefined;
139
+ decay?: number | undefined;
140
+ epsilon?: number | undefined;
141
+ });
142
+ decay: number;
143
+ epsilon: number;
144
+ /**
145
+ * Minimize `lossFn` from `x0`.
146
+ * @param {(x: number[]) => number | {loss: number, gradient: number[]|Float64Array} | {value: number, gradient: number[]|Float64Array}} lossFn
147
+ * - Objective; usually `{loss, gradient}`, finite differences for a number
148
+ * @param {number[]} x0 - Initial parameters
149
+ * @param {Object} [options] - Overrides for this call
150
+ * @param {number} [options.maxIter] - Maximum iterations
151
+ * @param {number} [options.tol] - Gradient-norm tolerance
152
+ * @returns {{x: number[], history: {loss: number[], gradNorm: number[]}}}
153
+ */
154
+ minimize(lossFn: (x: number[]) => number | {
155
+ loss: number;
156
+ gradient: number[] | Float64Array;
157
+ } | {
158
+ value: number;
159
+ gradient: number[] | Float64Array;
160
+ }, x0: number[], options?: {
161
+ maxIter?: number | undefined;
162
+ tol?: number | undefined;
163
+ }): {
164
+ x: number[];
165
+ history: {
166
+ loss: number[];
167
+ gradNorm: number[];
168
+ };
169
+ };
170
+ }
171
+ /**
172
+ * Adam as a `tangent/ds`-style optimizer object: `minimize(lossFn, x0)` returns `{x, history}`.
173
+ *
174
+ * New code can call `adam` or `minimize` directly.
175
+ */
176
+ export class AdamOptimizer extends Optimizer {
177
+ /**
178
+ * @param {Object} [options]
179
+ * @param {number} [options.learningRate=0.01] - Step size
180
+ * @param {number} [options.maxIter=1000] - Maximum iterations
181
+ * @param {number} [options.tol=1e-6] - Convergence tolerance on the gradient norm
182
+ * @param {boolean} [options.verbose=false] - Log progress every 100 iterations
183
+ * @param {number} [options.beta1=0.9] - First-moment decay
184
+ * @param {number} [options.beta2=0.999] - Second-moment decay
185
+ * @param {number} [options.epsilon=1e-8] - Added to the denominator
186
+ */
187
+ constructor(options?: {
188
+ learningRate?: number | undefined;
189
+ maxIter?: number | undefined;
190
+ tol?: number | undefined;
191
+ verbose?: boolean | undefined;
192
+ beta1?: number | undefined;
193
+ beta2?: number | undefined;
194
+ epsilon?: number | undefined;
195
+ });
196
+ beta1: number;
197
+ beta2: number;
198
+ epsilon: number;
199
+ /**
200
+ * Minimize `lossFn` from `x0`.
201
+ * @param {(x: number[]) => number | {loss: number, gradient: number[]|Float64Array} | {value: number, gradient: number[]|Float64Array}} lossFn
202
+ * - Objective; usually `{loss, gradient}`, finite differences for a number
203
+ * @param {number[]} x0 - Initial parameters
204
+ * @param {Object} [options] - Overrides for this call
205
+ * @param {number} [options.maxIter] - Maximum iterations
206
+ * @param {number} [options.tol] - Gradient-norm tolerance
207
+ * @returns {{x: number[], history: {loss: number[], gradNorm: number[]}}}
208
+ */
209
+ minimize(lossFn: (x: number[]) => number | {
210
+ loss: number;
211
+ gradient: number[] | Float64Array;
212
+ } | {
213
+ value: number;
214
+ gradient: number[] | Float64Array;
215
+ }, x0: number[], options?: {
216
+ maxIter?: number | undefined;
217
+ tol?: number | undefined;
218
+ }): {
219
+ x: number[];
220
+ history: {
221
+ loss: number[];
222
+ gradNorm: number[];
223
+ };
224
+ };
225
+ }
226
+ declare class Optimizer {
227
+ /**
228
+ * @param {Object} [options]
229
+ * @param {number} [options.learningRate=0.01] - Step size
230
+ * @param {number} [options.maxIter=1000] - Maximum iterations
231
+ * @param {number} [options.tol=1e-6] - Convergence tolerance on the gradient norm
232
+ * @param {boolean} [options.verbose=false] - Log progress every 100 iterations
233
+ */
234
+ constructor(options?: {
235
+ learningRate?: number | undefined;
236
+ maxIter?: number | undefined;
237
+ tol?: number | undefined;
238
+ verbose?: boolean | undefined;
239
+ });
240
+ learningRate: number;
241
+ maxIter: number;
242
+ tol: number;
243
+ verbose: boolean;
244
+ /**
245
+ * Minimize a loss function
246
+ * @param {Function} lossFn - Function that returns {loss, gradient}
247
+ * @param {Array<number>} x0 - Initial parameters
248
+ * @param {Object} options - Additional options
249
+ * @returns {Object} {x, history}
250
+ */
251
+ minimize(_lossFn: any, _x0: any, _options?: {}): Object;
252
+ _options(options: any): {
253
+ learningRate: number;
254
+ maxIter: any;
255
+ tol: any;
256
+ verbose: boolean;
257
+ };
258
+ }
259
+ export {};
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Build a normalized evaluator from a user-supplied objective.
3
+ *
4
+ * @param {Function} f - Objective: (x) => number or (x) => {loss, gradient}
5
+ * @param {Function} [grad] - Optional gradient: (x) => Array<number>
6
+ * @param {Object} [options]
7
+ * @param {number} [options.h] - Finite-difference step size
8
+ * @returns {Function} evaluate(x) => {loss, gradient}
9
+ */
10
+ export function makeEvaluator(f: Function, grad?: Function, options?: {
11
+ h?: number | undefined;
12
+ }): Function;
13
+ /**
14
+ * Build a scalar-only evaluator (for derivative-free methods).
15
+ *
16
+ * @param {Function} f - Objective: (x) => number or (x) => {loss, gradient}
17
+ * @returns {Function} (x) => number
18
+ */
19
+ export function makeScalarEvaluator(f: Function): Function;