Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions lib/node_modules/@stdlib/ml/base/sgd/params/struct-factory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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 to be used.
- **penaltyParams**: parameters specific to the regularization function being used.
- **learningRate**: learning rate scheduler to be used.
- **learningRateParams**: parameters specific to the learning rate scheduler being used.
- **lossFunction**: loss function to be used.
- **lossFunctionParams**: parameters specific to the loss function being used.
- **fitIntercept**: boolean indicating whether to include an intercept.
- **intercept**: initial intercept value.
- **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 list is a fixed-length array which is large enough to accommodate the option requiring the most parameters (`penaltyParams`: `3`, `learningRateParams`: `2`, `lossFunctionParams`: `1`). Accordingly, one must provide a list having the expected length, with any unused elements set to zero. As struct instances are zero-filled upon initialization, one may omit a list when the corresponding option requires no parameters.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var resolveLREnum = require( '@stdlib/ml/base/sgd-classification/learning-rate-resolve-enum' );
var resolveLossFunctionEnum = require( '@stdlib/ml/base/sgd-classification/loss-function-resolve-enum' );
var resolvePenaltyEnum = require( '@stdlib/ml/base/sgd-classification/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' );

// Note: hinge loss requires no parameters, and thus we may omit the respective parameter list.
var Struct = structFactory( 'float64' );
var params = new Struct({
'penalty': resolvePenaltyEnum( 'l2' ),
'penaltyParams': new Float64Array( [ 2.5, 0.0, 0.0 ] ),
'learningRate': resolveLREnum( 'constant' ),
'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ),
'lossFunction': resolveLossFunctionEnum( 'hinge' ),
'fitIntercept': true,
'intercept': 0.0,
'maxIter': 500
});

var str = params.toString({
'format': 'linear'
});
console.log( str );

Struct = structFactory( 'float32' );
params = new Struct({
'penalty': resolvePenaltyEnum( 'l2' ),
'penaltyParams': new Float32Array( [ 2.5, 0.0, 0.0 ] ),
'learningRate': resolveLREnum( 'constant' ),
'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ),
'lossFunction': resolveLossFunctionEnum( 'hinge' ),
'fitIntercept': true,
'intercept': 0.0,
'maxIter': 500
});

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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();
});
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
--------
Loading
Loading