Repository navigation
feat: add ml/base/sgd/params/struct-factory
#13875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 25 commits into
stdlib-js:develop
from
nakul-krishnakumar:sgd-trainer-opts
Aug 9, 2026
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
ede98fd
feat: add `ml/base/sgd/params/struct-factory`
nakul-krishnakumar ab43e1a
Merge branch 'develop' into sgd-trainer-opts
nakul-krishnakumar 58edc97
refactor: update import paths
nakul-krishnakumar 2acddf2
fix: update penalty params array size
nakul-krishnakumar 4c834e7
chore: update struct member ordering
nakul-krishnakumar 6928fba
chore: update according to code review
nakul-krishnakumar b1a2405
docs: broaden JSDocs
nakul-krishnakumar 5440662
docs: broaden JSDocs
nakul-krishnakumar c9ff466
Apply suggestions from code review
kgryte caf17cb
Apply suggestions from code review
kgryte 11c4ce7
Apply suggestions from code review
kgryte fe86974
Apply suggestions from code review
kgryte 411aacb
Apply suggestions from code review
kgryte fb7db0b
Apply suggestions from code review
kgryte 9d43944
refactor: inline constants
kgryte 40be548
Apply suggestions from code review
kgryte d279553
Apply suggestions from code review
kgryte 29a1642
Apply suggestions from code review
kgryte 246f17f
Apply suggestions from code review
kgryte bb071fe
docs: remove unwanted details
nakul-krishnakumar e9d922e
Apply suggestions from code review
kgryte 4927109
docs: update according to code review
nakul-krishnakumar 5807efb
docs: remove unnecessary references
nakul-krishnakumar b66237e
Apply suggestions from code review
kgryte 53ce435
chore: clean-up
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
213 changes: 213 additions & 0 deletions
213
lib/node_modules/@stdlib/ml/base/sgd/params/struct-factory/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # structFactory | ||
|
|
||
| > Create a new [`struct`][@stdlib/dstructs/struct] constructor tailored to a specified floating-point data type. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var structFactory = require( '@stdlib/ml/base/sgd/params/struct-factory' ); | ||
| ``` | ||
|
|
||
| #### structFactory( dtype ) | ||
|
|
||
| Returns a new [`struct`][@stdlib/dstructs/struct] constructor tailored to a specified floating-point data type. | ||
|
|
||
| ```javascript | ||
| var Struct = structFactory( 'float64' ); | ||
| // returns <Function> | ||
|
|
||
| var s = new Struct(); | ||
| // returns <Struct> | ||
| ``` | ||
|
|
||
| The function supports the following parameters: | ||
|
|
||
| - **dtype**: floating-point data type for storing floating-point parameters. Must be either `'float64'` or `'float32'`. | ||
|
|
||
| A returned [`struct`][@stdlib/dstructs/struct] constructor supports the following fields: | ||
|
|
||
| - **penalty**: [regularization function][@stdlib/ml/base/sgd/penalties]. | ||
|
|
||
| - **penaltyParams**: parameters specific to the regularization function being used. Must be an array having length `2`, with any unused elements set to zero. The expected array contents depend on `penalty`: | ||
|
|
||
| - **l1**: : `[ lambda, 0.0 ]` | ||
| - **l2**: `[ lambda, 0.0 ]` | ||
| - **elasticnet**: `[ lambda, l1Ratio ]` | ||
| - **none**: `[ 0.0, 0.0 ]` (unused) | ||
|
|
||
| where | ||
|
|
||
| - **lambda**: regularization parameter which determines the amount of shrinkage inflicted on the model coefficients. | ||
| - **l1Ratio**: mixing parameter on the interval `[0,1]` which determines the relative contribution of the L1 and L2 penalties. | ||
|
|
||
| - **learningRate**: [learning rate scheduler][@stdlib/ml/base/sgd/learning-rates]. | ||
|
|
||
| - **learningRateParams**: parameters specific to the learning rate scheduler being used. Must be an array having length `2`, with any unused elements set to zero. The expected array contents depend on `learningRate`: | ||
|
|
||
| - **basic**: `[ 0.0, 0.0 ]` (unused) | ||
| - **constant**: `[ eta0, 0.0 ]` | ||
| - **invscaling**: `[ eta0, powerT ]` | ||
| - **pegasos**: `[ lambda, 0.0 ]` | ||
|
|
||
| where | ||
|
|
||
| - **eta0**: initial learning rate. | ||
| - **powerT**: exponent controlling how quickly the learning rate decreases. | ||
| - **lambda**: regularization parameter. | ||
|
|
||
| - **lossFunction**: [loss function][@stdlib/ml/base/sgd/loss-functions]. | ||
|
|
||
| - **lossFunctionParams**: parameters specific to the loss function being used. Must be an array having length `1`. The expected array contents depend on `lossFunction`: | ||
|
|
||
| - **epsilon-insensitive**: `[ epsilon ]` | ||
| - **squared-epsilon-insensitive**: `[ epsilon ]` | ||
| - **huber**: `[ threshold ]` | ||
| - all other loss functions: `[ 0.0 ]` (unused) | ||
|
|
||
| where | ||
|
|
||
| - **epsilon**: insensitivity parameter (i.e., errors whose absolute value is less than `epsilon` incur no penalty). | ||
| - **threshold**: error magnitude at which the loss transitions from squared-error loss to linear loss. | ||
|
|
||
| - **fitIntercept**: boolean indicating whether to include an intercept. If `true`, an element equal to one is implicitly added to each provided feature vector. If `false`, the model assumes that feature vectors are already centered. | ||
|
|
||
| - **intercept**: initial intercept value. Only applicable when `fitIntercept` is `true`. | ||
|
|
||
| - **maxIter**: maximum number of iterations to run. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - A [`struct`][@stdlib/dstructs/struct] provides a fixed-width composite data structure for storing SGD trainer parameters and provides an ABI-stable data layout for JavaScript-C interoperation. | ||
| - Each parameter array is a fixed-length array which is large enough to accommodate the option requiring the most parameters (`penaltyParams`: `2`, `learningRateParams`: `2`, `lossFunctionParams`: `1`). Accordingly, one must provide an array having the expected length, with any unused elements set to zero (e.g., `[ lambda, 0.0 ]`), as providing an array having an unexpected length, including an empty array, raises an exception. As `struct` instances are zero-filled upon initialization, one may omit an array when the corresponding option requires no parameters. | ||
| - Consumers should only read as many elements as are applicable to the corresponding penalty, learning rate scheduler, or loss function, with any remaining elements being unused. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var resolveLREnum = require( '@stdlib/ml/base/sgd/learning-rate-resolve-enum' ); | ||
| var resolveLossFunctionEnum = require( '@stdlib/ml/base/sgd/loss-function-resolve-enum' ); | ||
| var resolvePenaltyEnum = require( '@stdlib/ml/base/sgd/penalty-resolve-enum' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
| var Float32Array = require( '@stdlib/array/float32' ); | ||
| var structFactory = require( '@stdlib/ml/base/sgd/params/struct-factory' ); | ||
|
|
||
| var Struct = structFactory( 'float64' ); | ||
| var params = new Struct({ | ||
| 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), | ||
| 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), | ||
| 'intercept': 0.0, | ||
| 'maxIter': 500, | ||
| 'penalty': resolvePenaltyEnum( 'l2' ), | ||
| 'learningRate': resolveLREnum( 'constant' ), | ||
| 'lossFunction': resolveLossFunctionEnum( 'hinge' ), | ||
| 'fitIntercept': true | ||
| }); | ||
|
|
||
| var str = params.toString({ | ||
| 'format': 'linear' | ||
| }); | ||
| console.log( str ); | ||
|
|
||
| Struct = structFactory( 'float32' ); | ||
| params = new Struct({ | ||
| 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), | ||
| 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), | ||
| 'intercept': 0.0, | ||
| 'maxIter': 500, | ||
| 'penalty': resolvePenaltyEnum( 'l2' ), | ||
| 'learningRate': resolveLREnum( 'constant' ), | ||
| 'lossFunction': resolveLossFunctionEnum( 'hinge' ), | ||
| 'fitIntercept': true | ||
| }); | ||
|
|
||
| str = params.toString({ | ||
| 'format': 'linear' | ||
| }); | ||
| console.log( str ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [@stdlib/dstructs/struct]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/dstructs/struct | ||
|
|
||
| [@stdlib/ml/base/sgd/penalties]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ml/base/sgd/penalties | ||
|
|
||
| [@stdlib/ml/base/sgd/learning-rates]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ml/base/sgd/learning-rates | ||
|
|
||
| [@stdlib/ml/base/sgd/loss-functions]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ml/base/sgd/loss-functions | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/ml/base/sgd/params/struct-factory/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isFunction = require( '@stdlib/assert/is-function' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var factory = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var values; | ||
| var v; | ||
| var i; | ||
|
|
||
| values = [ | ||
| 'float64', | ||
| 'float32' | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = factory( values[ i%values.length ] ); | ||
| if ( typeof v !== 'function' ) { | ||
| b.fail( 'should return a function' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isFunction( v ) ) { | ||
| b.fail( 'should return a function' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
24 changes: 24 additions & 0 deletions
24
lib/node_modules/@stdlib/ml/base/sgd/params/struct-factory/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
|
|
||
| {{alias}}( dtype ) | ||
| Returns a new struct constructor tailored to a specified floating-point data | ||
| type. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| dtype: string | ||
| Floating-point data type for storing floating-point parameters. | ||
|
|
||
| Returns | ||
| ------- | ||
| fcn: Function | ||
| Struct constructor. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var S = {{alias}}( 'float64' ); | ||
| > var r = new S(); | ||
| > r.toString() | ||
| <string> | ||
|
|
||
| See Also | ||
| -------- |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.