diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/README.md b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/README.md new file mode 100644 index 000000000000..822d19e8324b --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/README.md @@ -0,0 +1,153 @@ + + +# paramsFactory + +> Create a new constructor for creating an SGD trainer params object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var paramsFactory = require( '@stdlib/ml/base/sgd/params/factory' ); +``` + +#### paramsFactory( dtype ) + +Returns a new constructor for creating an SGD trainer params object. + +```javascript +var Params = paramsFactory( 'float64' ); +// returns + +var r = new Params(); +// returns +``` + +The function supports the following parameters: + +- **dtype**: floating-point data type for storing floating-point params. Must be either `'float64'` or `'float32'`. + +
+ + + + + +
+ +## Notes + +- A params object is a [`struct`][@stdlib/dstructs/struct] providing a fixed-width composite data structure for storing SGD trainer params and providing an ABI-stable data layout for JavaScript-C interoperation. + +
+ + + + + +
+ +## Examples + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var paramsFactory = require( '@stdlib/ml/base/sgd/params/factory' ); + +var Params = paramsFactory( 'float64' ); +var params = new Params({ + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true +}); + +var str = params.toString({ + 'format': 'linear' +}); +console.log( str ); + +Params = paramsFactory( 'float32' ); +params = new Params({ + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true +}); + +str = params.toString({ + 'format': 'linear' +}); +console.log( str ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/benchmark/benchmark.js b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/benchmark/benchmark.js new file mode 100644 index 000000000000..54e2d1466769 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/benchmark/benchmark.js @@ -0,0 +1,106 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-object' ); +var format = require( '@stdlib/string/format' ); +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(); +}); + +bench( format( '%s::constructor,new', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + factory( 'float64' ), + factory( 'float32' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = new ( values[ i%values.length ] )(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::constructor,no_new', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + factory( 'float64' ), + factory( 'float32' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = values[ i%values.length ](); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/repl.txt b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/repl.txt new file mode 100644 index 000000000000..a8f2703bf094 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( dtype ) + Returns a constructor for creating an SGD trainer params object. + + Parameters + ---------- + dtype: string + Floating-point data type for storing floating-point params. + + Returns + ------- + fcn: Function + Constructor. + + Examples + -------- + > var R = {{alias}}( 'float64' ); + > var r = new R(); + > r.toString() + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/types/index.d.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/types/index.d.ts new file mode 100644 index 000000000000..3f24f5f0b8d7 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/types/index.d.ts @@ -0,0 +1,236 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Regularization Function. +*/ +type Penalty = 'elasticnet' | 'l1' | 'l2' | 'none'; + +/** +* Learning Rate Scheduler. +*/ +type LearningRate = 'basic' | 'constant' | 'invscaling' | 'pegasos'; + +/** +* Loss Function. +*/ +type LossFunction = 'epsilon-insensitive' | 'hinge' | 'huber' | 'log' | 'modified-huber' | 'perceptron' | 'squared-epsilon-insensitive' | 'squared-error' | 'squared-hinge'; + +/** +* Interface describing SGD trainer parameters. +*/ +interface Params { + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams?: T; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams?: T; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams?: T; + + /** + * Initial intercept value. + */ + intercept?: number; + + /** + * Maximum number of iterations to run. + */ + maxIter?: number; + + /** + * Regularization function to be used. + */ + penalty?: Penalty; + + /** + * Learning rate scheduler to be used. + */ + learningRate?: LearningRate; + + /** + * Loss function to be used. + */ + lossFunction?: LossFunction; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept?: boolean; +} + +/** +* Interface describing options when serializing a params object to a string. +*/ +interface ToStringOptions { + /** + * Number of digits to display after decimal points. Default: `4`. + */ + digits?: number; +} + +/** +* Interface describing a params data structure. +*/ +declare class ParamsStruct { + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params + */ + constructor( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ); + + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams: T; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams: T; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams: T; + + /** + * Initial intercept value. + */ + intercept: number; + + /** + * Maximum number of iterations to run. + */ + maxIter: number; + + /** + * Regularization function to be used. + */ + penalty: Penalty; + + /** + * Learning rate scheduler to be used. + */ + learningRate: LearningRate; + + /** + * Loss function to be used. + */ + lossFunction: LossFunction; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept: boolean; + + /** + * Serializes a params object as a formatted string. + * + * @param options - options object + * @returns serialized params + */ + toString( options?: ToStringOptions ): string; + + /** + * Serializes a params object as a JSON object. + * + * @returns serialized object + */ + toJSON(): object; + + /** + * Returns a DataView of a params object. + * + * @returns DataView + */ + toDataView(): DataView; +} + +/** +* Interface defining a params constructor which is both "newable" and "callable". +*/ +interface ParamsConstructor { + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params object + */ + new( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ): ParamsStruct; + + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params object + */ + ( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ): ParamsStruct; +} + +/** +* Returns a new params constructor for creating an SGD trainer params object. +* +* @param dtype - floating-point data type for storing floating-point params +* @returns params constructor +* +* @example +* var Params = paramsFactory( 'float64' ); +* // returns +* +* var r = new Params(); +* // returns +*/ +declare function paramsFactory( dtype: 'float64' ): ParamsConstructor; + +/** +* Returns a constructor for creating an SGD trainer params object. +* +* @param dtype - floating-point data type for storing floating-point params +* @returns params constructor +* +* @example +* var Params = paramsFactory( 'float32' ); +* // returns +* +* var r = new Params(); +* // returns +*/ +declare function paramsFactory( dtype: 'float32' ): ParamsConstructor; + + +// EXPORTS // + +export = paramsFactory; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/types/test.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/types/test.ts new file mode 100644 index 000000000000..f48ac4129b1e --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/docs/types/test.ts @@ -0,0 +1,199 @@ +/* +* @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. +*/ + +import paramsFactory = require( './index' ); + + +// TESTS // + +// The function returns a function... +{ + paramsFactory( 'float64' ); // $ExpectType ParamsConstructor + paramsFactory( 'float32' ); // $ExpectType ParamsConstructor +} + +// The compiler throws an error if not provided a supported data type... +{ + paramsFactory( 10 ); // $ExpectError + paramsFactory( true ); // $ExpectError + paramsFactory( false ); // $ExpectError + paramsFactory( null ); // $ExpectError + paramsFactory( undefined ); // $ExpectError + paramsFactory( [] ); // $ExpectError + paramsFactory( {} ); // $ExpectError + paramsFactory( ( x: number ): number => x ); // $ExpectError +} + +// The function returns a function which returns a params object... +{ + const Params = paramsFactory( 'float64' ); + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r1 = new Params( new ArrayBuffer( 80 ) ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r2 = new Params( new ArrayBuffer( 80 ), 8 ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r3 = new Params( new ArrayBuffer( 80 ), 8, 16 ); // $ExpectType ParamsStruct +} + +// The returned constructor can be invoked without `new`... +{ + const Params = paramsFactory( 'float64' ); + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r1 = Params( new ArrayBuffer( 80 ) ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r2 = Params( new ArrayBuffer( 80 ), 8 ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r3 = Params( new ArrayBuffer( 80 ), 8, 16 ); // $ExpectType ParamsStruct +} + +// The params object has the expected properties (float64)... +{ + const Params = paramsFactory( 'float64' ); + const r = new Params( {} ); + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penaltyParams; // $ExpectType Float64Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRateParams; // $ExpectType Float64Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunctionParams; // $ExpectType Float64Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.intercept; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.maxIter; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penalty; // $ExpectType Penalty + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRate; // $ExpectType LearningRate + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunction; // $ExpectType LossFunction + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.fitIntercept; // $ExpectType boolean +} + +// The params object has the expected properties (float32)... +{ + const Params = paramsFactory( 'float32' ); + const r = new Params( {} ); + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penaltyParams; // $ExpectType Float32Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRateParams; // $ExpectType Float32Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunctionParams; // $ExpectType Float32Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.intercept; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.maxIter; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penalty; // $ExpectType Penalty + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRate; // $ExpectType LearningRate + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunction; // $ExpectType LossFunction + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.fitIntercept; // $ExpectType boolean +} + +// The compiler throws an error if the constructor is provided a first argument which is not an ArrayBuffer or object... +{ + const Params = paramsFactory( 'float64' ); + + new Params( 'abc' ); // $ExpectError + new Params( 123 ); // $ExpectError + new Params( true ); // $ExpectError + new Params( false ); // $ExpectError + new Params( null ); // $ExpectError + new Params( [] ); // $ExpectError + new Params( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided a second argument which is not a number... +{ + const Params = paramsFactory( 'float64' ); + + new Params( new ArrayBuffer( 80 ), 'abc' ); // $ExpectError + new Params( new ArrayBuffer( 80 ), true ); // $ExpectError + new Params( new ArrayBuffer( 80 ), false ); // $ExpectError + new Params( new ArrayBuffer( 80 ), null ); // $ExpectError + new Params( new ArrayBuffer( 80 ), [] ); // $ExpectError + new Params( new ArrayBuffer( 80 ), {} ); // $ExpectError + new Params( new ArrayBuffer( 80 ), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided a third argument which is not a number... +{ + const Params = paramsFactory( 'float64' ); + + new Params( new ArrayBuffer( 80 ), 8, 'abc' ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, true ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, false ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, null ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, [] ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, {} ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, ( x: number ): number => x ); // $ExpectError +} + +// The params object has a `toString` method... +{ + const Params = paramsFactory( 'float64' ); + const r = new Params( {} ); + + r.toString(); // $ExpectType string + r.toString( {} ); // $ExpectType string + r.toString( { 'digits': 4 } ); // $ExpectType string +} + +// The params object has a `toJSON` method... +{ + const Params = paramsFactory( 'float64' ); + const r = new Params( {} ); + + r.toJSON(); // $ExpectType object +} + +// The params object has a `toDataView` method... +{ + const Params = paramsFactory( 'float64' ); + const r = new Params( {} ); + + r.toDataView(); // $ExpectType DataView +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/examples/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/examples/index.js new file mode 100644 index 000000000000..1ee9f41ff884 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/examples/index.js @@ -0,0 +1,59 @@ +/** +* @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'; + +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var paramsFactory = require( './../lib' ); + +var Params = paramsFactory( 'float64' ); +var params = new Params({ + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true +}); + +var str = params.toString({ + 'format': 'linear' +}); +console.log( str ); + +Params = paramsFactory( 'float32' ); +params = new Params({ + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true +}); + +str = params.toString({ + 'format': 'linear' +}); +console.log( str ); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/lib/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/lib/index.js new file mode 100644 index 000000000000..8438ca49f683 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/lib/index.js @@ -0,0 +1,56 @@ +/** +* @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'; + +/** +* Return a constructor for creating an SGD trainer params object. +* +* @module @stdlib/ml/base/sgd/params/factory +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var paramsFactory = require( '@stdlib/ml/base/sgd/params/factory' ); +* +* var Params = paramsFactory( 'float64' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/lib/main.js b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/lib/main.js new file mode 100644 index 000000000000..3df8ef9a8231 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/lib/main.js @@ -0,0 +1,508 @@ +/** +* @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. +*/ + +/* eslint-disable no-invalid-this, no-restricted-syntax */ + +'use strict'; + +// MODULES // + +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var isObject = require( '@stdlib/assert/is-object' ); +var hasProp = require( '@stdlib/assert/has-property' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' ); +var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); +var contains = require( '@stdlib/array/base/assert/contains' ).factory; +var join = require( '@stdlib/array/base/join' ); +var objectAssign = require( '@stdlib/object/assign' ); +var inherit = require( '@stdlib/utils/inherit' ); +var resolvePenaltyStr = require( '@stdlib/ml/base/sgd/penalty-resolve-str' ); +var resolveLRStr = require( '@stdlib/ml/base/sgd/learning-rate-resolve-str' ); +var resolveLossFnStr = require( '@stdlib/ml/base/sgd/loss-function-resolve-str' ); +var resolvePenaltyEnum = require( '@stdlib/ml/base/sgd/penalty-resolve-enum' ); +var resolveLREnum = require( '@stdlib/ml/base/sgd/learning-rate-resolve-enum' ); +var resolveLossFnEnum = require( '@stdlib/ml/base/sgd/loss-function-resolve-enum' ); +var structFactory = require( '@stdlib/ml/base/sgd/params/struct-factory' ); +var params2json = require( '@stdlib/ml/base/sgd/params/to-json' ); +var params2str = require( '@stdlib/ml/base/sgd/params/to-string' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var DTYPES = [ + 'float64', + 'float32' +]; + +var isDataType = contains( DTYPES ); + + +// MAIN // + +/** +* Returns a constructor for creating an SGD trainer params object. +* +* @param {string} dtype - storage data type for floating-point values +* @throws {TypeError} first argument must be a supported data type +* @returns {Function} constructor +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var Params = factory( 'float64' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ +function factory( dtype ) { + var learningRateDescriptor; + var lossFunctionDescriptor; + var penaltyDescriptor; + var Struct; + + if ( !isDataType( dtype ) ) { + throw new TypeError( format( 'invalid argument. First argument must be one of the following: "%s". Value: `%s`.', join( DTYPES, ', ' ), dtype ) ); + } + + // Create a struct constructor: + Struct = structFactory( dtype ); + + // Cache a reference to a property descriptors on the parent prototype so that we can intercept the return value: + learningRateDescriptor = propertyDescriptor( Struct.prototype, 'learningRate' ); + lossFunctionDescriptor = propertyDescriptor( Struct.prototype, 'lossFunction' ); + penaltyDescriptor = propertyDescriptor( Struct.prototype, 'penalty' ); + + /** + * Returns an SGD trainer params object. + * + * @private + * @constructor + * @param {(ArrayBuffer|Object)} [arg] - underlying byte buffer or a data object + * @param {NonNegativeInteger} [byteOffset] - byte offset + * @param {NonNegativeInteger} [byteLength] - maximum byte length + * @throws {TypeError} first argument must be an ArrayBuffer or a data object + * @returns {Params} params object + */ + function Params( arg, byteOffset, byteLength ) { + var nargs; + var args; + var v; + var i; + + nargs = arguments.length; + if ( !( this instanceof Params ) ) { + if ( nargs === 0 ) { + return new Params(); + } + if ( nargs === 1 ) { + return new Params( arg ); + } + if ( nargs === 2 ) { + return new Params( arg, byteOffset ); + } + return new Params( arg, byteOffset, byteLength ); + } + args = []; + if ( nargs > 0 ) { + if ( isArrayBuffer( arg ) ) { + for ( i = 0; i < nargs; i++ ) { + args.push( arguments[ i ] ); + } + } else if ( isObject( arg ) ) { + if ( hasProp( arg, 'learningRate' ) ) { + args.push( objectAssign( {}, arg ) ); + v = resolveLREnum( args[ 0 ].learningRate ); + args[ 0 ].learningRate = ( v === null ) ? NaN : v; + } + if ( hasProp( arg, 'lossFunction' ) ) { + args.push( objectAssign( {}, arg ) ); + v = resolveLossFnEnum( args[ 0 ].lossFunction ); + args[ 0 ].lossFunction = ( v === null ) ? NaN : v; + } + if ( hasProp( arg, 'penalty' ) ) { + args.push( objectAssign( {}, arg ) ); + v = resolvePenaltyEnum( args[ 0 ].penalty ); + args[ 0 ].penalty = ( v === null ) ? NaN : v; + } + } else { + throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer or a data object. Value: `%s`.', arg ) ); + } + } + // Call the parent constructor... + Struct.apply( this, args ); + return this; + } + + /* + * Inherit from the parent constructor. + */ + inherit( Params, Struct ); + + /** + * Constructor name. + * + * @private + * @name name + * @memberof Params + * @readonly + * @type {string} + */ + setReadOnly( Params, 'name', Struct.name ); + + /** + * Alignment. + * + * @private + * @name alignment + * @memberof Params + * @readonly + * @type {PositiveInteger} + */ + setReadOnly( Params, 'alignment', Struct.alignment ); + + /** + * Size (in bytes) of the `struct`. + * + * @private + * @name byteLength + * @memberof Params + * @readonly + * @type {PositiveInteger} + */ + setReadOnly( Params, 'byteLength', Struct.byteLength ); + + /** + * Returns a list of `struct` fields. + * + * @private + * @name fields + * @memberof Params + * @readonly + * @type {Array} + */ + setReadOnlyAccessor( Params, 'fields', function get() { + return Struct.fields; + }); + + /** + * Returns a string corresponding to the `struct` layout. + * + * @private + * @name layout + * @memberof Params + * @readonly + * @type {string} + */ + setReadOnlyAccessor( Params, 'layout', function get() { + return Struct.layout; + }); + + /** + * Returns the underlying byte buffer of a `struct`. + * + * @private + * @name bufferOf + * @memberof Params + * @readonly + * @type {Function} + * @param {Object} obj - struct instance + * @throws {TypeError} must provide a `struct` instance + * @returns {ArrayBuffer} underlying byte buffer + */ + setReadOnly( Params, 'bufferOf', Struct.bufferOf ); + + /** + * Returns the length, in bytes, of the value specified by the provided field name. + * + * @private + * @name byteLengthOf + * @memberof Params + * @readonly + * @type {Function} + * @param {string} name - field name + * @throws {Error} struct must have at least one field + * @throws {TypeError} must provide a recognized field name + * @returns {NonNegativeInteger} byte length + */ + setReadOnly( Params, 'byteLengthOf', Struct.byteLengthOf ); + + /** + * Returns the offset, in bytes, from the beginning of a `struct` to the value specified by the provided field name. + * + * @private + * @name byteOffsetOf + * @memberof Params + * @readonly + * @type {Function} + * @param {string} name - field name + * @throws {Error} struct must have at least one field + * @throws {TypeError} must provide a recognized field name + * @returns {NonNegativeInteger} byte offset + */ + setReadOnly( Params, 'byteOffsetOf', Struct.byteOffsetOf ); + + /** + * Returns the description associated with a provided field name. + * + * @private + * @name descriptionOf + * @memberof Params + * @readonly + * @type {Function} + * @param {string} name - field name + * @throws {Error} struct must have at least one field + * @throws {TypeError} must provide a recognized field name + * @returns {string} description + */ + setReadOnly( Params, 'descriptionOf', Struct.descriptionOf ); + + /** + * Returns a boolean indicating whether a provided value is a `struct` instance. + * + * @private + * @name isStruct + * @memberof Params + * @readonly + * @type {Function} + * @param {*} value - input value + * @returns {boolean} boolean indicating whether a value is a `struct` instance + */ + setReadOnly( Params, 'isStruct', Struct.isStruct ); + + /** + * Returns the type associated with a provided field name. + * + * @private + * @name typeOf + * @memberof Params + * @readonly + * @type {Function} + * @param {string} name - field name + * @throws {Error} struct must have at least one field + * @throws {TypeError} must provide a recognized field name + * @returns {(string|Object)} type + */ + setReadOnly( Params, 'typeOf', Struct.typeOf ); + + /** + * Returns the underlying byte buffer of a `struct` as a `DataView`. + * + * @private + * @name viewOf + * @memberof Params + * @readonly + * @type {Function} + * @param {Object} obj - struct instance + * @throws {TypeError} must provide a `struct` instance + * @returns {DataView} view of underlying byte buffer + */ + setReadOnly( Params, 'viewOf', Struct.viewOf ); + + /** + * Test name. + * + * @private + * @name method + * @memberof Params.prototype + * @type {string} + * @default 'Stochastic Gradient Descent' + */ + setReadOnly( Params.prototype, 'method', 'Stochastic Gradient Descent' ); + + /** + * Regularization Function. + * + * @private + * @name penalty + * @memberof Params.prototype + * @type {string} + */ + setReadWriteAccessor( Params.prototype, 'penalty', getPenalty, setPenalty ); + + /** + * Learning Rate Scheduler. + * + * @private + * @name penalty + * @memberof Params.prototype + * @type {string} + */ + setReadWriteAccessor( Params.prototype, 'learningRate', getLearningRate, setLearningRate ); + + /** + * Loss Function. + * + * @private + * @name penalty + * @memberof Params.prototype + * @type {string} + */ + setReadWriteAccessor( Params.prototype, 'lossFunction', getLossFunction, setLossFunction ); + + /** + * Serializes a params object as a string. + * + * ## Notes + * + * - Example output: + * + * ```text + * + * Stochastic Gradient Descent + * + * penalty: l2 + * learning rate: constant + * loss function: hinge + * lambda: 2.5000 + * eta0: 0.0100 + * fit intercept: true + * intercept: 0.0000 + * max iterations: 1000 + * + * ``` + * + * @private + * @name toString + * @memberof Params.prototype + * @type {Function} + * @param {Options} [opts] - options object + * @param {PositiveInteger} [opts.digits=4] - number of digits after the decimal point + * @throws {TypeError} options argument must be an object + * @throws {TypeError} must provide valid options + * @returns {string} serialized params + */ + setReadOnly( Params.prototype, 'toString', function toString( opts ) { + if ( arguments.length ) { + return params2str( this, opts ); + } + return params2str( this ); + }); + + /** + * Serializes a params object as a JSON object. + * + * ## Notes + * + * - `JSON.stringify()` implicitly calls this method when stringifying a `Params` instance. + * + * @private + * @name toJSON + * @memberof Params.prototype + * @type {Function} + * @returns {Object} serialized object + */ + setReadOnly( Params.prototype, 'toJSON', function toJSON() { + return params2json( this ); + }); + + /** + * Returns a DataView of a params object. + * + * @private + * @name toDataView + * @memberof Params.prototype + * @type {Function} + * @returns {DataView} DataView + */ + setReadOnly( Params.prototype, 'toDataView', function toDataView() { + return Struct.viewOf( this ); + }); + + return Params; + + /** + * Returns the regularization function. + * + * @private + * @returns {string} regularization function + */ + function getPenalty() { + return resolvePenaltyStr( penaltyDescriptor.get.call( this ) ); + } + + /** + * Sets the regularization function. + * + * @private + * @param {string} value - regularization function + */ + function setPenalty( value ) { + penaltyDescriptor.set.call( this, resolvePenaltyEnum( value ) ); + } + + /** + * Returns the learning rate scheduler. + * + * @private + * @returns {string} learning rate scheduler + */ + function getLearningRate() { + return resolveLRStr( learningRateDescriptor.get.call( this ) ); + } + + /** + * Sets the learning rate scheduler. + * + * @private + * @param {string} value - learning rate scheduler + */ + function setLearningRate( value ) { + learningRateDescriptor.set.call( this, resolveLREnum( value ) ); + } + + /** + * Returns the loss function. + * + * @private + * @returns {string} loss function + */ + function getLossFunction() { + return resolveLossFnStr( lossFunctionDescriptor.get.call( this ) ); + } + + /** + * Sets the loss function. + * + * @private + * @param {string} value - loss function + */ + function setLossFunction( value ) { + lossFunctionDescriptor.set.call( this, resolveLossFnEnum( value ) ); + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/package.json b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/package.json new file mode 100644 index 000000000000..8b440c61f475 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ml/base/sgd/params/factory", + "version": "0.0.0", + "description": "Return a constructor for creating an SGD trainer params object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "ml", + "machine learning", + "sgd", + "stochastic gradient descent", + "utilities", + "utility", + "utils", + "util", + "constructor", + "ctor", + "params" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/factory/test/test.js b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/test/test.js new file mode 100644 index 000000000000..56417815abe0 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/factory/test/test.js @@ -0,0 +1,754 @@ +/** +* @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 tape = require( 'tape' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var isDataView = require( '@stdlib/assert/is-dataview' ); +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var f32 = require( '@stdlib/number/float64/base/to-float32' ); +var paramsFactory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof paramsFactory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a supported data type', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + paramsFactory( value ); + }; + } +}); + +tape( 'the function returns a constructor which throws an error if provided a first argument which is not an ArrayBuffer or data object', function test( t ) { + var params; + var values; + var i; + + params = paramsFactory( 'float64' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( value ); + }; + } +}); + +tape( 'the function returns a constructor which throws an error if provided a second argument which is not a nonnegative integer', function test( t ) { + var params; + var values; + var i; + + params = paramsFactory( 'float64' ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( new ArrayBuffer( 1024 ), value ); + }; + } +}); + +tape( 'the function returns a constructor which throws an error if provided a third argument which is not a nonnegative integer', function test( t ) { + var params; + var values; + var i; + + params = paramsFactory( 'float64' ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( new ArrayBuffer( 1024 ), 0, value ); + }; + } +}); + +tape( 'the function returns a constructor which throws an error if provided an invalid `penalty` property value', function test( t ) { + var params; + var values; + var i; + + params = paramsFactory( 'float64' ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params({ + 'penalty': value + }); + }; + } +}); + +tape( 'the function returns a constructor which throws an error if provided an invalid `learningRate` property value', function test( t ) { + var params; + var values; + var i; + + params = paramsFactory( 'float64' ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params({ + 'learningRate': value + }); + }; + } +}); + +tape( 'the function returns a constructor which throws an error if provided an invalid `lossFunction` property value', function test( t ) { + var params; + var values; + var i; + + params = paramsFactory( 'float64' ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params({ + 'lossFunction': value + }); + }; + } +}); + +tape( 'the function returns a constructor which does not require the `new` operator', function test( t ) { + var params; + var p; + + params = paramsFactory( 'float64' ); + + p = params(); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( {} ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ) ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ), 0 ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ), 0, 1024 ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (dtype=float64)', function test( t ) { + var expected; + var Params; + var actual; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + actual = new Params({ + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }); + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (dtype=float32)', function test( t ) { + var expected; + var Params; + var actual; + + Params = paramsFactory( 'float32' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + actual = new Params({ + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }); + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (no arguments)', function test( t ) { + var expected; + var Params; + var actual; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + actual = new Params(); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (empty object)', function test( t ) { + var expected; + var Params; + var actual; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + actual = new Params( {} ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (ArrayBuffer)', function test( t ) { + var expected; + var Params; + var actual; + var buf; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + buf = new ArrayBuffer( 1024 ); + actual = new Params( buf ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.toDataView().buffer, buf, 'returns expected value' ); + t.strictEqual( actual.toDataView().byteOffset, 0, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (ArrayBuffer, byteOffset)', function test( t ) { + var expected; + var Params; + var actual; + var buf; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + buf = new ArrayBuffer( 1024 ); + actual = new Params( buf, 16 ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.toDataView().buffer, buf, 'returns expected value' ); + t.strictEqual( actual.toDataView().byteOffset, 16, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor for creating a fixed-width params object (ArrayBuffer, byteOffset, byteLength)', function test( t ) { + var expected; + var Params; + var actual; + var buf; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + buf = new ArrayBuffer( 1024 ); + actual = new Params( buf, 16, 160 ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Params, true, 'returns expected value' ); + t.strictEqual( actual.toDataView().buffer, buf, 'returns expected value' ); + t.strictEqual( actual.toDataView().byteOffset, 16, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor which returns an instance having a method property', function test( t ) { + var Params; + var params; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + params = new Params(); + + t.strictEqual( params.method, 'Stochastic Gradient Descent', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor which returns an instance having a `toString` method', function test( t ) { + var Params; + var params; + var actual; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + params = new Params(); + + actual = params.toString(); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + + actual = params.toString({ + 'decision': false + }); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor which returns an instance having a `toJSON` method', function test( t ) { + var Params; + var params; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + params = new Params(); + t.strictEqual( typeof params.toJSON, 'function', 'returns expected value' ); + t.strictEqual( typeof params.toJSON(), 'object', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a constructor which returns an instance having a `toDataView` method', function test( t ) { + var Params; + var params; + + Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + + params = new Params(); + t.strictEqual( typeof params.toDataView, 'function', 'returns expected value' ); + t.strictEqual( isDataView( params.toDataView() ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a constructor having a `name` property', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.name, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having an `alignment` property', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.alignment, 'number', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `byteLength` property', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.byteLength, 'number', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `fields` property', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( isStringArray( Params.fields ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `layout` property', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.layout, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `bufferOf` method', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.bufferOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `byteLengthOf` method', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.byteLengthOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `byteOffsetOf` method', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.byteOffsetOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `descriptionOf` method', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.descriptionOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having an `isStruct` method', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.isStruct, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a constructor having a `viewOf` method', function test( t ) { + var Params = paramsFactory( 'float64' ); + t.strictEqual( typeof Params, 'function', 'returns expected value' ); + t.strictEqual( typeof Params.viewOf, 'function', 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/README.md b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/README.md new file mode 100644 index 000000000000..840ad39a5e77 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/README.md @@ -0,0 +1,449 @@ + + +# Float32Params + +> Create an SGD single-precision floating-point params object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var Float32Params = require( '@stdlib/ml/base/sgd/params/float32' ); +``` + +#### Float32Params( \[arg\[, byteOffset\[, byteLength]]] ) + +Returns an SGD single-precision floating-point params object. + +```javascript +var params = new Float32Params(); +// returns {...} +``` + +The function supports the following parameters: + +- **arg**: an [`ArrayBuffer`][@stdlib/array/buffer] or a data object (_optional_). +- **byteOffset**: byte offset (_optional_). +- **byteLength**: maximum byte length (_optional_). + +A data object argument is an object having one or more of the following properties: + +- **penalty**: regularization function to be used (e.g., `'l1'`, `'l2'`, `'elasticnet'` or `'none'`). + +- **penaltyParams**: parameters specific to the regularization function being used as a [`Float32Array`][@stdlib/array/float32]. + + - When `'penalty = {l1,l2}`, `'penaltyParams' => [ lambda ]` + - When `'penalty = elasticnet`, `'penaltyParams' => [ lambda, l1Ratio ]` + - When `'penalty = none`, `'penaltyParams' => [ ]` + +- **learningRate**: learning rate scheduler to be used (e.g., `'basic'`, `'constant'`, `'invscaling'` or `'pegasos'`). + +- **learningRateParams**: parameters specific to the learning rate scheduler being used as a [`Float32Array`][@stdlib/array/float32]. + + - When `'learningRate = basic`, `'learningRateParams' => [ ]` + - When `'learningRate = constant`, `'learningRateParams' => [ eta0 ]` + - When `'learningRate = invscaling`, `'learningRateParams' => [ eta0, powerT ]` + - When `'learningRate = pegasos`, `'learningRateParams' => [ lambda ]` + +- **lossFunction**: loss function to be used (e.g., `'epsilon-insensitive'`, `'hinge'`, `'huber'`, `'log'`, `'modified-huber'`, `'perceptron'`, `'squared-epsilon-insensitive'`, `'squared-error'`, or `'squared-hinge'`). + +- **lossFunctionParams**: parameters specific to the loss function being used as a [`Float32Array`][@stdlib/array/float32]. + + - When `'lossFunction = {epsilon-insensitive,squared-epsilon-insensitive}`, `'lossFunctionParams' => [ epsilon ]` + - Else, `'lossFunctionParams' => [ ]` + +- **fitIntercept**: boolean indicating whether to include intercept. + +- **intercept**: initial intercept value. + +- **maxIter**: maximum number of iterations to run. + +#### Float32Params.prototype.penalty + +Regularization function to be used. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.penalty; +// returns +``` + +#### Float32Params.prototype.penaltyParams + +Parameters specific to the regularization function being used. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.penaltyParams; +// returns +``` + +#### Float32Params.prototype.learningRate + +Learning rate scheduler to be used. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.learningRate; +// returns +``` + +#### Float32Params.prototype.learningRateParams + +Parameters specific to the learning rate scheduler being used. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.learningRateParams; +// returns +``` + +#### Float32Params.prototype.lossFunction + +Loss function to be used. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.lossFunction; +// returns +``` + +#### Float32Params.prototype.lossFunctionParams + +Parameters specific to the loss function being used. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.lossFunctionParams; +// returns +``` + +#### Float32Params.prototype.fitIntercept + +Boolean indicating whether to include intercept. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.fitIntercept; +// returns +``` + +#### Float32Params.prototype.intercept + +Initial intercept value. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.intercept; +// returns +``` + +#### Float32Params.prototype.maxIter + +Maximum number of iterations to run. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.maxIter; +// returns +``` + +#### Float32Params.prototype.toString( \[options] ) + +Serializes a params object to a formatted string. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.toString(); +// returns +``` + +The method supports the following options: + +- **digits**: number of digits to display after decimal points. Default: `4`. + +Example output: + +```text + +Stochastic Gradient Descent + + penalty: l2 + learning rate: constant + loss function: hinge + lambda: 2.5000 + eta0: 0.0100 + fit intercept: true + intercept: 0.0000 + max iterations: 1000 + +``` + +#### Float32Params.prototype.toJSON( \[options] ) + +Serializes a params object as a JSON object. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.toJSON(); +// returns {...} +``` + +`JSON.stringify()` implicitly calls this method when stringifying a params instance. + +#### Float32Params.prototype.toDataView() + +Returns a [`DataView`][@stdlib/array/dataview] of a params object. + +```javascript +var params = new Float32Params(); +// returns {...} + +// ... + +var v = params.toDataView(); +// returns +``` + +
+ + + + + +
+ +## Notes + +- A params object is a [`struct`][@stdlib/dstructs/struct] providing a fixed-width composite data structure for storing SGD trainer params and providing an ABI-stable data layout for JavaScript-C interoperation. + +
+ + + + + +
+ +## Examples + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var Params = require( '@stdlib/ml/base/sgd/params/float32' ); + +var params = new Params({ + 'fitIntercept': true, + 'intercept': 0.0, + 'maxIter': 500, + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge' +}); + +var str = params.toString(); +console.log( str ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/ml/base/sgd/params/float32.h" +``` + +#### stdlib_ml_sgd_float32_params + +Structure for holding single-precision floating-point test params. + + + +```c +#include +#include + +struct stdlib_ml_sgd_float32_params { + // Parameters specific to the regularization function being used: + float penaltyParams[ 2 ]; + + // Parameters specific to the learning rate scheduler being used: + float learningRateParams[ 2 ]; + + // Parameters specific to the loss function being used: + float lossFunctionParams[ 1 ]; + + // Initial intercept value: + float intercept; + + // Maximum number of iterations to run: + int32_t maxIter; + + // Regularization function to be used: + int8_t penalty; + + // Learning rate scheduler to be used: + int8_t learningRate; + + // Loss function to be used: + int8_t lossFunction; + + // Boolean indicating whether to include intercept: + bool fitIntercept; +}; +``` + +
+ + + + + +
+ +
+ + + + + +
+ +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/benchmark/benchmark.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/benchmark/benchmark.js new file mode 100644 index 000000000000..d1d5951be3dc --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/benchmark/benchmark.js @@ -0,0 +1,71 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-object' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var Float32Params = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::constructor,new', pkg ), function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = new Float32Params(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::constructor,no_new', pkg ), function benchmark( b ) { + var params; + var v; + var i; + + params = Float32Params; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = params(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/repl.txt b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/repl.txt new file mode 100644 index 000000000000..79d886f817cb --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( [arg[, byteOffset[, byteLength]]] ) + Returns an SGD single-precision floating-point params object. + + Parameters + ---------- + arg: Object|ArrayBuffer (optional) + ArrayBuffer or data object. + + byteOffset: integer (optional) + Byte offset. + + byteLength: integer (optional) + Maximum byte length. + + Returns + ------- + out: Object + Params object. + + Examples + -------- + > var r = new {{alias}}(); + > r.toString() + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/types/index.d.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/types/index.d.ts new file mode 100644 index 000000000000..2fb4f74855f0 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/types/index.d.ts @@ -0,0 +1,240 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Regularization Function. +*/ +type Penalty = 'elasticnet' | 'l1' | 'l2' | 'none'; + +/** +* Learning Rate Scheduler. +*/ +type LearningRate = 'basic' | 'constant' | 'invscaling' | 'pegasos'; + +/** +* Loss Function. +*/ +type LossFunction = 'epsilon-insensitive' | 'hinge' | 'huber' | 'log' | 'modified-huber' | 'perceptron' | 'squared-epsilon-insensitive' | 'squared-error' | 'squared-hinge'; + +/** +* Interface describing SGD trainer parameters. +*/ +interface Params { + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams?: Float32Array; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams?: Float32Array; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams?: Float32Array; + + /** + * Initial intercept value. + */ + intercept?: number; + + /** + * Maximum number of iterations to run. + */ + maxIter?: number; + + /** + * Regularization function to be used. + */ + penalty?: Penalty; + + /** + * Learning rate scheduler to be used. + */ + learningRate?: LearningRate; + + /** + * Loss function to be used. + */ + lossFunction?: LossFunction; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept?: boolean; +} + +/** +* Interface describing options when serializing a params object to a string. +*/ +interface ToStringOptions { + /** + * Number of digits to display after decimal points. Default: `4`. + */ + digits?: number; +} + +/** +* Interface describing a params data structure. +*/ +declare class ParamsStruct { + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params + */ + constructor( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ); + + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams: Float32Array; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams: Float32Array; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams: Float32Array; + + /** + * Initial intercept value. + */ + intercept: number; + + /** + * Maximum number of iterations to run. + */ + maxIter: number; + + /** + * Regularization function to be used. + */ + penalty: Penalty; + + /** + * Learning rate scheduler to be used. + */ + learningRate: LearningRate; + + /** + * Loss function to be used. + */ + lossFunction: LossFunction; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept: boolean; + + /** + * Algorithm name. + */ + method: string; + + /** + * Serializes a params object as a formatted string. + * + * @param options - options object + * @returns serialized params + */ + toString( options?: ToStringOptions ): string; + + /** + * Serializes a params object as a JSON object. + * + * @returns serialized object + */ + toJSON(): object; + + /** + * Returns a DataView of a params object. + * + * @returns DataView + */ + toDataView(): DataView; +} + +/** +* Interface defining a params constructor which is both "newable" and "callable". +*/ +interface ParamsConstructor { + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params object + */ + new( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ): ParamsStruct; + + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params object + */ + ( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ): ParamsStruct; +} + +/** +* Returns an SGD single-precision floating-point params object. +* +* @param arg - buffer or data object +* @param byteOffset - byte offset +* @param byteLength - maximum byte length +* @returns params object +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float32Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ +declare var Params: ParamsConstructor; + + +// EXPORTS // + +export = Params; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/types/test.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/types/test.ts new file mode 100644 index 000000000000..2747f02040ed --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/docs/types/test.ts @@ -0,0 +1,140 @@ +/* +* @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. +*/ + +import Params = require( './index' ); + + +// TESTS // + +// The constructor returns a params object... +{ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r0 = new Params( {} ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r1 = new Params( new ArrayBuffer( 80 ) ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r2 = new Params( new ArrayBuffer( 80 ), 8 ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r3 = new Params( new ArrayBuffer( 80 ), 8, 16 ); // $ExpectType ParamsStruct +} + +// The constructor can be invoked without `new`... +{ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r0 = Params( {} ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r1 = Params( new ArrayBuffer( 80 ) ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r2 = Params( new ArrayBuffer( 80 ), 8 ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r3 = Params( new ArrayBuffer( 80 ), 8, 16 ); // $ExpectType ParamsStruct +} + +// The params object has the expected properties... +{ + const r = new Params( {} ); + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penaltyParams; // $ExpectType Float32Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRateParams; // $ExpectType Float32Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunctionParams; // $ExpectType Float32Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.intercept; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.maxIter; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penalty; // $ExpectType Penalty + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRate; // $ExpectType LearningRate + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunction; // $ExpectType LossFunction + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.fitIntercept; // $ExpectType boolean +} + +// The compiler throws an error if the constructor is provided a first argument which is not an ArrayBuffer or object... +{ + new Params( 'abc' ); // $ExpectError + new Params( 123 ); // $ExpectError + new Params( true ); // $ExpectError + new Params( false ); // $ExpectError + new Params( null ); // $ExpectError + new Params( [] ); // $ExpectError + new Params( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided a second argument which is not a number... +{ + new Params( new ArrayBuffer( 80 ), 'abc' ); // $ExpectError + new Params( new ArrayBuffer( 80 ), true ); // $ExpectError + new Params( new ArrayBuffer( 80 ), false ); // $ExpectError + new Params( new ArrayBuffer( 80 ), null ); // $ExpectError + new Params( new ArrayBuffer( 80 ), [] ); // $ExpectError + new Params( new ArrayBuffer( 80 ), {} ); // $ExpectError + new Params( new ArrayBuffer( 80 ), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided a third argument which is not a number... +{ + new Params( new ArrayBuffer( 80 ), 8, 'abc' ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, true ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, false ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, null ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, [] ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, {} ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, ( x: number ): number => x ); // $ExpectError +} + +// The params object has a `toString` method... +{ + const r = new Params( {} ); + + r.toString(); // $ExpectType string + r.toString( {} ); // $ExpectType string + r.toString( { 'digits': 4 } ); // $ExpectType string +} + +// The params object has a `toJSON` method... +{ + const r = new Params( {} ); + + r.toJSON(); // $ExpectType object +} + +// The params object has a `toDataView` method... +{ + const r = new Params( {} ); + + r.toDataView(); // $ExpectType DataView +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/examples/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/examples/index.js new file mode 100644 index 000000000000..cc0c7487f9d1 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var Float32Array = require( '@stdlib/array/float32' ); +var Params = require( './../lib' ); + +var params = new Params({ + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true +}); + +var str = params.toString(); +console.log( str ); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/include/stdlib/ml/base/sgd/params/float32.h b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/include/stdlib/ml/base/sgd/params/float32.h new file mode 100644 index 000000000000..0884e49f3447 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/include/stdlib/ml/base/sgd/params/float32.h @@ -0,0 +1,57 @@ +/** +* @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. +*/ + +#ifndef STDLIB_ML_BASE_SGD_PARAMS_FLOAT32_H +#define STDLIB_ML_BASE_SGD_PARAMS_FLOAT32_H + +#include +#include + +/** +* Struct for storing test params. +*/ +struct stdlib_ml_sgd_float32_params { + // Parameters specific to the regularization function being used: + float penaltyParams[ 2 ]; + + // Parameters specific to the learning rate scheduler being used: + float learningRateParams[ 2 ]; + + // Parameters specific to the loss function being used: + float lossFunctionParams[ 1 ]; + + // Initial intercept value: + float intercept; + + // Maximum number of iterations to run: + int32_t maxIter; + + // Regularization function to be used: + int8_t penalty; + + // Learning rate scheduler to be used: + int8_t learningRate; + + // Loss function to be used: + int8_t lossFunction; + + // Boolean indicating whether to include intercept: + bool fitIntercept; +}; + +#endif // !STDLIB_ML_BASE_SGD_PARAMS_FLOAT32_H diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/lib/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/lib/index.js new file mode 100644 index 000000000000..00024cce024c --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/lib/index.js @@ -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'; + +/** +* Create an SGD single-precision floating-point params object. +* +* @module @stdlib/ml/base/sgd/params/float32 +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var Params = require( '@stdlib/ml/base/sgd/params/float32' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float32Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/lib/main.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/lib/main.js new file mode 100644 index 000000000000..75e643d84fd9 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/lib/main.js @@ -0,0 +1,63 @@ +/** +* @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 factory = require( '@stdlib/ml/base/sgd/params/factory' ); + + +// MAIN // + +/** +* Returns an SGD single-precision floating-point params object. +* +* @name Params +* @constructor +* @type {Function} +* @param {(ArrayBuffer|Object)} [arg] - underlying byte buffer or data object +* @param {NonNegativeInteger} [byteOffset] - byte offset +* @param {NonNegativeInteger} [byteLength] - maximum byte length +* @returns {Params} params object +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float32Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ +var Params = factory( 'float32' ); + + +// EXPORTS // + +module.exports = Params; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/manifest.json b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/manifest.json new file mode 100644 index 000000000000..844d692f6439 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/manifest.json @@ -0,0 +1,36 @@ +{ + "options": {}, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "src": [], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/package.json b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/package.json new file mode 100644 index 000000000000..689abf2f6fd4 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ml/base/sgd/params/float32", + "version": "0.0.0", + "description": "Create an SGD single-precision floating-point params object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "ml", + "machine learning", + "sgd", + "stochastic gradient descent", + "utilities", + "utility", + "utils", + "util", + "constructor", + "ctor", + "params" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float32/test/test.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/test/test.js new file mode 100644 index 000000000000..d52c9ca26e75 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float32/test/test.js @@ -0,0 +1,507 @@ +/** +* @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 tape = require( 'tape' ); +var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var isDataView = require( '@stdlib/assert/is-dataview' ); +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +var Float32Array = require( '@stdlib/array/float32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var f32 = require( '@stdlib/number/float64/base/to-float32' ); +var Float32Params = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float32Params, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ArrayBuffer or data object', function test( t ) { + var params; + var values; + var i; + + params = Float32Params; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a nonnegative integer', function test( t ) { + var params; + var values; + var i; + + params = Float32Params; + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( new ArrayBuffer( 1024 ), value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not a nonnegative integer', function test( t ) { + var params; + var values; + var i; + + params = Float32Params; + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( new ArrayBuffer( 1024 ), 0, value ); + }; + } +}); + +tape( 'the function is a constructor which does not require the `new` operator', function test( t ) { + var params; + var p; + + params = Float32Params; + + p = params(); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( {} ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ) ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ), 0 ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ), 0, 1024 ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object ', function test( t ) { + var expected; + var actual; + + actual = new Float32Params({ + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }); + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float32Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (no arguments)', function test( t ) { + var expected; + var actual; + + actual = new Float32Params(); + + actual.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float32Array( [ 0.0 ] ); + actual.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); + actual.intercept = f32( 0.0 ); + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float32Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (empty object)', function test( t ) { + var expected; + var actual; + + actual = new Float32Params( {} ); + + actual.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float32Array( [ 0.0 ] ); + actual.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); + actual.intercept = f32( 0.0 ); + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float32Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (ArrayBuffer)', function test( t ) { + var expected; + var actual; + var buf; + + buf = new ArrayBuffer( 1024 ); + actual = new Float32Params( buf ); + + actual.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float32Array( [ 0.0 ] ); + actual.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); + actual.intercept = f32( 0.0 ); + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float32Params, true, 'returns expected value' ); + t.strictEqual( actual.toDataView().buffer, buf, 'returns expected value' ); + t.strictEqual( actual.toDataView().byteOffset, 0, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (ArrayBuffer, byteOffset)', function test( t ) { + var expected; + var actual; + var buf; + + buf = new ArrayBuffer( 1024 ); + actual = new Float32Params( buf, 16 ); + + actual.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float32Array( [ 0.0 ] ); + actual.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); + actual.intercept = f32( 0.0 ); + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float32Params, true, 'returns expected value' ); + t.strictEqual( actual.toDataView().buffer, buf, 'returns expected value' ); + t.strictEqual( actual.toDataView().byteOffset, 16, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (ArrayBuffer, byteOffset, byteLength)', function test( t ) { + var expected; + var actual; + var buf; + + buf = new ArrayBuffer( 1024 ); + actual = new Float32Params( buf, 16, 160 ); + + actual.penaltyParams = new Float32Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float32Array( [ 0.0 ] ); + actual.learningRateParams = new Float32Array( [ 0.01, 0.0 ] ); + actual.intercept = f32( 0.0 ); + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float32Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float32Array( [ 0.0 ] ), + 'learningRateParams': new Float32Array( [ 0.01, 0.0 ] ), + 'intercept': f32( 0.0 ), + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float32Params, true, 'returns expected value' ); + t.strictEqual( actual.toDataView().buffer, buf, 'returns expected value' ); + t.strictEqual( actual.toDataView().byteOffset, 16, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a method property', function test( t ) { + var params = new Float32Params(); + + t.strictEqual( params.method, 'Stochastic Gradient Descent', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `toString` method', function test( t ) { + var params; + var actual; + + params = new Float32Params(); + + actual = params.toString(); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + + actual = params.toString({ + 'digits': 4 + }); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `toJSON` method', function test( t ) { + var params = new Float32Params(); + t.strictEqual( typeof params.toJSON, 'function', 'returns expected value' ); + t.strictEqual( typeof params.toJSON(), 'object', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `toDataView` method', function test( t ) { + var params = new Float32Params(); + t.strictEqual( typeof params.toDataView, 'function', 'returns expected value' ); + t.strictEqual( isDataView( params.toDataView() ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `name` property', function test( t ) { + t.strictEqual( typeof Float32Params.name, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has an `alignment` property', function test( t ) { + t.strictEqual( typeof Float32Params.alignment, 'number', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `byteLength` property', function test( t ) { + t.strictEqual( typeof Float32Params.byteLength, 'number', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `fields` property', function test( t ) { + t.strictEqual( isStringArray( Float32Params.fields ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `layout` property', function test( t ) { + t.strictEqual( typeof Float32Params.layout, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `bufferOf` method', function test( t ) { + t.strictEqual( typeof Float32Params.bufferOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `byteLengthOf` method', function test( t ) { + t.strictEqual( typeof Float32Params.byteLengthOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `byteOffsetOf` method', function test( t ) { + t.strictEqual( typeof Float32Params.byteOffsetOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `descriptionOf` method', function test( t ) { + t.strictEqual( typeof Float32Params.descriptionOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has an `isStruct` method', function test( t ) { + t.strictEqual( typeof Float32Params.isStruct, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `viewOf` method', function test( t ) { + t.strictEqual( typeof Float32Params.viewOf, 'function', 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/README.md b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/README.md new file mode 100644 index 000000000000..d9902191ac45 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/README.md @@ -0,0 +1,449 @@ + + +# Float64Params + +> Create an SGD double-precision floating-point params object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +``` + +#### Float64Params( \[arg\[, byteOffset\[, byteLength]]] ) + +Returns an SGD double-precision floating-point params object. + +```javascript +var params = new Float64Params(); +// returns {...} +``` + +The function supports the following parameters: + +- **arg**: an [`ArrayBuffer`][@stdlib/array/buffer] or a data object (_optional_). +- **byteOffset**: byte offset (_optional_). +- **byteLength**: maximum byte length (_optional_). + +A data object argument is an object having one or more of the following properties: + +- **penalty**: regularization function to be used (e.g., `'l1'`, `'l2'`, `'elasticnet'` or `'none'`). + +- **penaltyParams**: parameters specific to the regularization function being used as a [`Float64Array`][@stdlib/array/float64]. + + - When `'penalty = {l1,l2}`, `'penaltyParams' => [ lambda ]` + - When `'penalty = elasticnet`, `'penaltyParams' => [ lambda, l1Ratio ]` + - When `'penalty = none`, `'penaltyParams' => [ ]` + +- **learningRate**: learning rate scheduler to be used (e.g., `'basic'`, `'constant'`, `'invscaling'` or `'pegasos'`). + +- **learningRateParams**: parameters specific to the learning rate scheduler being used as a [`Float64Array`][@stdlib/array/float64]. + + - When `'learningRate = basic`, `'learningRateParams' => [ ]` + - When `'learningRate = constant`, `'learningRateParams' => [ eta0 ]` + - When `'learningRate = invscaling`, `'learningRateParams' => [ eta0, powerT ]` + - When `'learningRate = pegasos`, `'learningRateParams' => [ lambda ]` + +- **lossFunction**: loss function to be used (e.g., `'epsilon-insensitive'`, `'hinge'`, `'huber'`, `'log'`, `'modified-huber'`, `'perceptron'`, `'squared-epsilon-insensitive'`, `'squared-error'`, or `'squared-hinge'`). + +- **lossFunctionParams**: parameters specific to the loss function being used as a [`Float64Array`][@stdlib/array/float64]. + + - When `'lossFunction = {epsilon-insensitive,squared-epsilon-insensitive}`, `'lossFunctionParams' => [ epsilon ]` + - Else, `'lossFunctionParams' => [ ]` + +- **fitIntercept**: boolean indicating whether to include intercept. + +- **intercept**: initial intercept value. + +- **maxIter**: maximum number of iterations to run. + +#### Float64Params.prototype.penalty + +Regularization function to be used. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.penalty; +// returns +``` + +#### Float64Params.prototype.penaltyParams + +Parameters specific to the regularization function being used. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.penaltyParams; +// returns +``` + +#### Float64Params.prototype.learningRate + +Learning rate scheduler to be used. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.learningRate; +// returns +``` + +#### Float64Params.prototype.learningRateParams + +Parameters specific to the learning rate scheduler being used. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.learningRateParams; +// returns +``` + +#### Float64Params.prototype.lossFunction + +Loss function to be used. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.lossFunction; +// returns +``` + +#### Float64Params.prototype.lossFunctionParams + +Parameters specific to the loss function being used. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.lossFunctionParams; +// returns +``` + +#### Float64Params.prototype.fitIntercept + +Boolean indicating whether to include intercept. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.fitIntercept; +// returns +``` + +#### Float64Params.prototype.intercept + +Initial intercept value. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.intercept; +// returns +``` + +#### Float64Params.prototype.maxIter + +Maximum number of iterations to run. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.maxIter; +// returns +``` + +#### Float64Params.prototype.toString( \[options] ) + +Serializes a params object to a formatted string. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.toString(); +// returns +``` + +The method supports the following options: + +- **digits**: number of digits to display after decimal points. Default: `4`. + +Example output: + +```text + +Stochastic Gradient Descent + + penalty: l2 + learning rate: constant + loss function: hinge + lambda: 2.5000 + eta0: 0.0100 + fit intercept: true + intercept: 0.0000 + max iterations: 1000 + +``` + +#### Float64Params.prototype.toJSON( \[options] ) + +Serializes a params object as a JSON object. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.toJSON(); +// returns {...} +``` + +`JSON.stringify()` implicitly calls this method when stringifying a params instance. + +#### Float64Params.prototype.toDataView() + +Returns a [`DataView`][@stdlib/array/dataview] of a params object. + +```javascript +var params = new Float64Params(); +// returns {...} + +// ... + +var v = params.toDataView(); +// returns +``` + +
+ + + + + +
+ +## Notes + +- A params object is a [`struct`][@stdlib/dstructs/struct] providing a fixed-width composite data structure for storing SGD trainer params and providing an ABI-stable data layout for JavaScript-C interoperation. + +
+ + + + + +
+ +## Examples + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Params = require( '@stdlib/ml/base/sgd/params/float64' ); + +var params = new Params({ + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true, +}); + +var str = params.toString(); +console.log( str ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/ml/base/sgd/params/float64.h" +``` + +#### stdlib_ml_sgd_float64_params + +Structure for holding double-precision floating-point SGD params. + + + +```c +#include +#include + +struct stdlib_ml_sgd_float64_params { + // Parameters specific to the regularization function being used: + double penaltyParams[ 2 ]; + + // Parameters specific to the learning rate scheduler being used: + double learningRateParams[ 2 ]; + + // Parameters specific to the loss function being used: + double lossFunctionParams[ 1 ]; + + // Initial intercept value: + double intercept; + + // Maximum number of iterations to run: + int32_t maxIter; + + // Regularization function to be used: + int8_t penalty; + + // Learning rate scheduler to be used: + int8_t learningRate; + + // Loss function to be used: + int8_t lossFunction; + + // Boolean indicating whether to include intercept: + bool fitIntercept; +}; +``` + +
+ + + + + +
+ +
+ + + + + +
+ +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/benchmark/benchmark.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/benchmark/benchmark.js new file mode 100644 index 000000000000..5cc8ca3ffed3 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/benchmark/benchmark.js @@ -0,0 +1,71 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-object' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var Float64Params = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::constructor,new', pkg ), function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = new Float64Params(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::constructor,no_new', pkg ), function benchmark( b ) { + var params; + var v; + var i; + + params = Float64Params; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = params(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/repl.txt b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/repl.txt new file mode 100644 index 000000000000..a22f9c69c720 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( [arg[, byteOffset[, byteLength]]] ) + Returns an SGD double-precision floating-point params object. + + Parameters + ---------- + arg: Object|ArrayBuffer (optional) + ArrayBuffer or data object. + + byteOffset: integer (optional) + Byte offset. + + byteLength: integer (optional) + Maximum byte length. + + Returns + ------- + out: Object + Params object. + + Examples + -------- + > var r = new {{alias}}(); + > r.toString() + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/types/index.d.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/types/index.d.ts new file mode 100644 index 000000000000..c20bd2486397 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/types/index.d.ts @@ -0,0 +1,240 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Regularization Function. +*/ +type Penalty = 'elasticnet' | 'l1' | 'l2' | 'none'; + +/** +* Learning Rate Scheduler. +*/ +type LearningRate = 'basic' | 'constant' | 'invscaling' | 'pegasos'; + +/** +* Loss Function. +*/ +type LossFunction = 'epsilon-insensitive' | 'hinge' | 'huber' | 'log' | 'modified-huber' | 'perceptron' | 'squared-epsilon-insensitive' | 'squared-error' | 'squared-hinge'; + +/** +* Interface describing SGD trainer parameters. +*/ +interface Params { + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams?: Float64Array; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams?: Float64Array; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams?: Float64Array; + + /** + * Initial intercept value. + */ + intercept?: number; + + /** + * Maximum number of iterations to run. + */ + maxIter?: number; + + /** + * Regularization function to be used. + */ + penalty?: Penalty; + + /** + * Learning rate scheduler to be used. + */ + learningRate?: LearningRate; + + /** + * Loss function to be used. + */ + lossFunction?: LossFunction; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept?: boolean; +} + +/** +* Interface describing options when serializing a params object to a string. +*/ +interface ToStringOptions { + /** + * Number of digits to display after decimal points. Default: `4`. + */ + digits?: number; +} + +/** +* Interface describing a params data structure. +*/ +declare class ParamsStruct { + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params + */ + constructor( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ); + + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams: Float64Array; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams: Float64Array; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams: Float64Array; + + /** + * Initial intercept value. + */ + intercept: number; + + /** + * Maximum number of iterations to run. + */ + maxIter: number; + + /** + * Regularization function to be used. + */ + penalty: Penalty; + + /** + * Learning rate scheduler to be used. + */ + learningRate: LearningRate; + + /** + * Loss function to be used. + */ + lossFunction: LossFunction; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept: boolean; + + /** + * Algorithm name. + */ + method: string; + + /** + * Serializes a params object as a formatted string. + * + * @param options - options object + * @returns serialized params + */ + toString( options?: ToStringOptions ): string; + + /** + * Serializes a params object as a JSON object. + * + * @returns serialized object + */ + toJSON(): object; + + /** + * Returns a DataView of a params object. + * + * @returns DataView + */ + toDataView(): DataView; +} + +/** +* Interface defining a params constructor which is both "newable" and "callable". +*/ +interface ParamsConstructor { + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params object + */ + new( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ): ParamsStruct; + + /** + * Params constructor. + * + * @param arg - buffer or data object + * @param byteOffset - byte offset + * @param byteLength - maximum byte length + * @returns params object + */ + ( arg?: ArrayBuffer | Params, byteOffset?: number, byteLength?: number ): ParamsStruct; +} + +/** +* Returns an SGD double-precision floating-point params object. +* +* @param arg - buffer or data object +* @param byteOffset - byte offset +* @param byteLength - maximum byte length +* @returns params object +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ +declare var Params: ParamsConstructor; + + +// EXPORTS // + +export = Params; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/types/test.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/types/test.ts new file mode 100644 index 000000000000..19fd2c06ff80 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/docs/types/test.ts @@ -0,0 +1,143 @@ +/* +* @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. +*/ + +import Params = require( './index' ); + + +// TESTS // + +// The constructor returns a params object... +{ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r0 = new Params( {} ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r1 = new Params( new ArrayBuffer( 80 ) ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r2 = new Params( new ArrayBuffer( 80 ), 8 ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r3 = new Params( new ArrayBuffer( 80 ), 8, 16 ); // $ExpectType ParamsStruct +} + +// The constructor can be invoked without `new`... +{ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r0 = Params( {} ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r1 = Params( new ArrayBuffer( 80 ) ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r2 = Params( new ArrayBuffer( 80 ), 8 ); // $ExpectType ParamsStruct + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const r3 = Params( new ArrayBuffer( 80 ), 8, 16 ); // $ExpectType ParamsStruct +} + +// The params object has the expected properties... +{ + const r = new Params( {} ); + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penaltyParams; // $ExpectType Float64Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRateParams; // $ExpectType Float64Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunctionParams; // $ExpectType Float64Array + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.intercept; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.maxIter; // $ExpectType number + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.penalty; // $ExpectType Penalty + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.learningRate; // $ExpectType LearningRate + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.lossFunction; // $ExpectType LossFunction + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.fitIntercept; // $ExpectType boolean + + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + r.method; // $ExpectType string +} + +// The compiler throws an error if the constructor is provided a first argument which is not an ArrayBuffer or object... +{ + new Params( 'abc' ); // $ExpectError + new Params( 123 ); // $ExpectError + new Params( true ); // $ExpectError + new Params( false ); // $ExpectError + new Params( null ); // $ExpectError + new Params( [] ); // $ExpectError + new Params( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided a second argument which is not a number... +{ + new Params( new ArrayBuffer( 80 ), 'abc' ); // $ExpectError + new Params( new ArrayBuffer( 80 ), true ); // $ExpectError + new Params( new ArrayBuffer( 80 ), false ); // $ExpectError + new Params( new ArrayBuffer( 80 ), null ); // $ExpectError + new Params( new ArrayBuffer( 80 ), [] ); // $ExpectError + new Params( new ArrayBuffer( 80 ), {} ); // $ExpectError + new Params( new ArrayBuffer( 80 ), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the constructor is provided a third argument which is not a number... +{ + new Params( new ArrayBuffer( 80 ), 8, 'abc' ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, true ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, false ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, null ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, [] ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, {} ); // $ExpectError + new Params( new ArrayBuffer( 80 ), 8, ( x: number ): number => x ); // $ExpectError +} + +// The params object has a `toString` method... +{ + const r = new Params( {} ); + + r.toString(); // $ExpectType string + r.toString( {} ); // $ExpectType string + r.toString( { 'digits': 4 } ); // $ExpectType string +} + +// The params object has a `toJSON` method... +{ + const r = new Params( {} ); + + r.toJSON(); // $ExpectType object +} + +// The params object has a `toDataView` method... +{ + const r = new Params( {} ); + + r.toDataView(); // $ExpectType DataView +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/examples/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/examples/index.js new file mode 100644 index 000000000000..c21716cb74d0 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var Float64Array = require( '@stdlib/array/float64' ); +var Params = require( './../lib' ); + +var params = new Params({ + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true +}); + +var str = params.toString(); +console.log( str ); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/include/stdlib/ml/base/sgd/params/float64.h b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/include/stdlib/ml/base/sgd/params/float64.h new file mode 100644 index 000000000000..c7ba6839f6a3 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/include/stdlib/ml/base/sgd/params/float64.h @@ -0,0 +1,57 @@ +/** +* @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. +*/ + +#ifndef STDLIB_ML_BASE_SGD_PARAMS_FLOAT64_H +#define STDLIB_ML_BASE_SGD_PARAMS_FLOAT64_H + +#include +#include + +/** +* Struct for storing SGD parameters. +*/ +struct stdlib_ml_sgd_float64_params { + // Parameters specific to the regularization function being used: + double penaltyParams[ 2 ]; + + // Parameters specific to the learning rate scheduler being used: + double learningRateParams[ 2 ]; + + // Parameters specific to the loss function being used: + double lossFunctionParams[ 1 ]; + + // Initial intercept value: + double intercept; + + // Maximum number of iterations to run: + int32_t maxIter; + + // Regularization function to be used: + int8_t penalty; + + // Learning rate scheduler to be used: + int8_t learningRate; + + // Loss function to be used: + int8_t lossFunction; + + // Boolean indicating whether to include intercept: + bool fitIntercept; +}; + +#endif // !STDLIB_ML_BASE_SGD_PARAMS_FLOAT64_H diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/lib/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/lib/index.js new file mode 100644 index 000000000000..63598683536d --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/lib/index.js @@ -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'; + +/** +* Create an SGD double-precision floating-point params object. +* +* @module @stdlib/ml/base/sgd/params/float64 +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Params = require( '@stdlib/ml/base/sgd/params/float64' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/lib/main.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/lib/main.js new file mode 100644 index 000000000000..1039d3077479 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/lib/main.js @@ -0,0 +1,63 @@ +/** +* @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 factory = require( '@stdlib/ml/base/sgd/params/factory' ); + + +// MAIN // + +/** +* Returns an SGD double-precision floating-point params object. +* +* @name Params +* @constructor +* @type {Function} +* @param {(ArrayBuffer|Object)} [arg] - underlying byte buffer or data object +* @param {NonNegativeInteger} [byteOffset] - byte offset +* @param {NonNegativeInteger} [byteLength] - maximum byte length +* @returns {Params} params object +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var params = new Params(); +* // returns +* +* params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +* params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +* params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +* params.intercept = 0.0; +* params.maxIter = 500; +* params.penalty = 'l2'; +* params.learningRate = 'constant'; +* params.lossFunction = 'hinge'; +* params.fitIntercept = true; +* +* var str = params.toString(); +* // returns +*/ +var Params = factory( 'float64' ); + + +// EXPORTS // + +module.exports = Params; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/manifest.json b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/manifest.json new file mode 100644 index 000000000000..844d692f6439 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/manifest.json @@ -0,0 +1,36 @@ +{ + "options": {}, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "src": [], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/package.json b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/package.json new file mode 100644 index 000000000000..471748d81893 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ml/base/sgd/params/float64", + "version": "0.0.0", + "description": "Create an SGD double-precision floating-point params object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "ml", + "machine learning", + "sgd", + "stochastic gradient descent", + "utilities", + "utility", + "utils", + "util", + "constructor", + "ctor", + "params" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/float64/test/test.js b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/test/test.js new file mode 100644 index 000000000000..4cd9746d5215 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/float64/test/test.js @@ -0,0 +1,500 @@ +/** +* @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 tape = require( 'tape' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isDataView = require( '@stdlib/assert/is-dataview' ); +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var Float64Params = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float64Params, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ArrayBuffer or data object', function test( t ) { + var params; + var values; + var i; + + params = Float64Params; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a nonnegative integer', function test( t ) { + var params; + var values; + var i; + + params = Float64Params; + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( new ArrayBuffer( 1024 ), value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not a nonnegative integer', function test( t ) { + var params; + var values; + var i; + + params = Float64Params; + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params( new ArrayBuffer( 1024 ), 0, value ); + }; + } +}); + +tape( 'the function is a constructor which does not require the `new` operator', function test( t ) { + var params; + var p; + + params = Float64Params; + + p = params(); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( {} ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ) ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ), 0 ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + p = params( new ArrayBuffer( 1024 ), 0, 1024 ); + t.strictEqual( p instanceof params, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object ', function test( t ) { + var expected; + var actual; + + actual = new Float64Params({ + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }); + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float64Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (no arguments)', function test( t ) { + var expected; + var actual; + + actual = new Float64Params(); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float64Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (empty object)', function test( t ) { + var expected; + var actual; + + actual = new Float64Params( {} ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float64Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (ArrayBuffer)', function test( t ) { + var expected; + var actual; + var buf; + + buf = new ArrayBuffer( 1024 ); + actual = new Float64Params( buf ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float64Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (ArrayBuffer, byteOffset)', function test( t ) { + var expected; + var actual; + var buf; + + buf = new ArrayBuffer( 1024 ); + actual = new Float64Params( buf, 16 ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float64Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is a constructor for a fixed-width params object (ArrayBuffer, byteOffset, byteLength)', function test( t ) { + var expected; + var actual; + var buf; + + buf = new ArrayBuffer( 1024 ); + actual = new Float64Params( buf, 16, 160 ); + + actual.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); + actual.lossFunctionParams = new Float64Array( [ 0.0 ] ); + actual.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); + actual.intercept = 0.0; + actual.maxIter = 500; + actual.penalty = 'l2'; + actual.learningRate = 'constant'; + actual.lossFunction = 'hinge'; + actual.fitIntercept = true; + + expected = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 500, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true + }; + + t.strictEqual( actual instanceof Float64Params, true, 'returns expected value' ); + t.strictEqual( actual.fitIntercept, expected.fitIntercept, 'returns expected value' ); + t.strictEqual( actual.intercept, expected.intercept, 'returns expected value' ); + t.strictEqual( actual.maxIter, expected.maxIter, 'returns expected value' ); + t.strictEqual( actual.penalty, expected.penalty, 'returns expected value' ); + t.strictEqual( actual.lossFunction, expected.lossFunction, 'returns expected value' ); + t.strictEqual( actual.learningRate, expected.learningRate, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.penaltyParams, expected.penaltyParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.lossFunctionParams, expected.lossFunctionParams ), true, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( actual.learningRateParams, expected.learningRateParams ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a method property', function test( t ) { + var params = new Float64Params(); + + t.strictEqual( params.method, 'Stochastic Gradient Descent', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `toString` method', function test( t ) { + var params; + var actual; + + params = new Float64Params(); + + actual = params.toString(); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + + actual = params.toString({ + 'digits': 2 + }); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `toJSON` method', function test( t ) { + var params = new Float64Params(); + t.strictEqual( typeof params.toJSON, 'function', 'returns expected value' ); + t.strictEqual( typeof params.toJSON(), 'object', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `toDataView` method', function test( t ) { + var params = new Float64Params(); + t.strictEqual( typeof params.toDataView, 'function', 'returns expected value' ); + t.strictEqual( isDataView( params.toDataView() ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `name` property', function test( t ) { + t.strictEqual( typeof Float64Params.name, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has an `alignment` property', function test( t ) { + t.strictEqual( typeof Float64Params.alignment, 'number', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `byteLength` property', function test( t ) { + t.strictEqual( typeof Float64Params.byteLength, 'number', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `fields` property', function test( t ) { + t.strictEqual( isStringArray( Float64Params.fields ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `layout` property', function test( t ) { + t.strictEqual( typeof Float64Params.layout, 'string', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `bufferOf` method', function test( t ) { + t.strictEqual( typeof Float64Params.bufferOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `byteLengthOf` method', function test( t ) { + t.strictEqual( typeof Float64Params.byteLengthOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `byteOffsetOf` method', function test( t ) { + t.strictEqual( typeof Float64Params.byteOffsetOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `descriptionOf` method', function test( t ) { + t.strictEqual( typeof Float64Params.descriptionOf, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has an `isStruct` method', function test( t ) { + t.strictEqual( typeof Float64Params.isStruct, 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor has a `viewOf` method', function test( t ) { + t.strictEqual( typeof Float64Params.viewOf, 'function', 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/README.md b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/README.md new file mode 100644 index 000000000000..b502f894d94e --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/README.md @@ -0,0 +1,131 @@ + + +# params2json + +> Serialize an SGD trainer params object as a JSON object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var params2json = require( '@stdlib/ml/base/sgd/params/to-json' ); +``` + +#### params2json( params ) + +Serializes an SGD trainer params object as a JSON object. + +```javascript +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); + +var params = new Float64Params(); + +// ... + +var o = params2json( params ); +// returns {...} +``` + +The function supports the following parameters: + +- **params**: SGD trainer params object. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +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 params2json = require( '@stdlib/ml/base/sgd/params/to-json' ); + +var params = new Float64Params(); +params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +params.intercept = 0.0; +params.maxIter = 500; +params.penalty = resolvePenaltyEnum( 'l2' ); +params.learningRate = resolveLREnum( 'constant' ); +params.lossFunction = resolveLossFunctionEnum( 'hinge' ); +params.fitIntercept = true; + +var o = params2json( params ); +console.log( o ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/benchmark/benchmark.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/benchmark/benchmark.js new file mode 100644 index 000000000000..153dc894c869 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @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 Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +var Float32Params = require( '@stdlib/ml/base/sgd/params/float32' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var pkg = require( './../package.json' ).name; +var params2json = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var v; + var i; + + values = [ + new Float64Params(), + new Float32Params() + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = params2json( values[ i%values.length ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isPlainObject( v ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/repl.txt b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/repl.txt new file mode 100644 index 000000000000..7e88fb438d7e --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( params ) + Serializes an SGD trainer params object as a JSON object. + + Parameters + ---------- + params: Object + SGD params object. + + Returns + ------- + out: Object + Serialized object. + + Examples + -------- + > var params = { + ... 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + ... 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + ... 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + ... 'intercept': 0.0, + ... 'maxIter': 1000, + ... 'penalty': 'l2', + ... 'learningRate': 'constant', + ... 'lossFunction': 'hinge', + ... 'fitIntercept': true, + ... 'method': 'Stochastic Gradient Descent', + ... }; + > var o = {{alias}}( params ) + {...} + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/types/index.d.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/types/index.d.ts new file mode 100644 index 000000000000..c3d330b7b2fa --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Interface describing SGD trainer parameters. +*/ +interface Params { + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams: Float64Array | Float32Array; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams: Float64Array | Float32Array; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams: Float64Array | Float32Array; + + /** + * Initial intercept value. + */ + intercept: number; + + /** + * Maximum number of iterations to run. + */ + maxIter: number; + + /** + * Regularization function to be used. + */ + penalty: string; + + /** + * Learning rate scheduler to be used. + */ + learningRate: string; + + /** + * Loss function to be used. + */ + lossFunction: string; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept: boolean; +} + +/** +* Serializes an SGD trainer params object as a JSON object. +* +* @param params - SGD trainer params object +* @returns serialized object +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var params = { +* 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), +* 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), +* 'lossFunctionParams': new Float64Array( [ 0.0 ] ), +* 'intercept': 0.0, +* 'maxIter': 1000, +* 'penalty': 'l2', +* 'learningRate': 'constant', +* 'lossFunction': 'hinge', +* 'fitIntercept': true, +* 'method': 'Stochastic Gradient Descent' +* }; +* +* var obj = params2json( params ); +* // returns {...} +*/ +declare function params2json( params: Params ): Params; + + +// EXPORTS // + +export = params2json; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/types/test.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/types/test.ts new file mode 100644 index 000000000000..1ba23b6213e4 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/docs/types/test.ts @@ -0,0 +1,51 @@ +/* +* @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. +*/ + +import params2json = require( './index' ); + + +// TESTS // + +// The function returns an object... +{ + const params = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 1000, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true, + 'method': 'Stochastic Gradient Descent' + }; + params2json( params ); // $ExpectType Params +} + +// The compiler throws an error if not provided a params object... +{ + params2json( 10 ); // $ExpectError + params2json( true ); // $ExpectError + params2json( false ); // $ExpectError + params2json( null ); // $ExpectError + params2json( undefined ); // $ExpectError + params2json( [] ); // $ExpectError + params2json( {} ); // $ExpectError + params2json( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/examples/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/examples/index.js new file mode 100644 index 000000000000..433b0339ba7f --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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'; + +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +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 params2json = require( './../lib' ); + +var params = new Float64Params(); +params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +params.intercept = 0.0; +params.maxIter = 500; +params.penalty = resolvePenaltyEnum( 'l2' ); +params.learningRate = resolveLREnum( 'constant' ); +params.lossFunction = resolveLossFunctionEnum( 'hinge' ); +params.fitIntercept = true; + +var o = params2json( params ); +console.log( o ); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/lib/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/lib/index.js new file mode 100644 index 000000000000..18f9f3251366 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/lib/index.js @@ -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'; + +/** +* Serialize an SGD trainer params object as a JSON object. +* +* @module @stdlib/ml/base/sgd/params/to-json +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var params2json = require( '@stdlib/ml/base/sgd/params/to-json' ); +* +* var params = { +* 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), +* 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), +* 'lossFunctionParams': new Float64Array( [ 0.0 ] ), +* 'intercept': 0.0, +* 'maxIter': 1000, +* 'penalty': 'l2', +* 'learningRate': 'constant', +* 'lossFunction': 'hinge', +* 'fitIntercept': true, +* 'method': 'Stochastic Gradient Descent', +* }; +* +* var obj = params2json( params ); +* // returns {...} +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/lib/main.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/lib/main.js new file mode 100644 index 000000000000..e0a116458c5b --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/lib/main.js @@ -0,0 +1,71 @@ +/** +* @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 typedarray2json = require( '@stdlib/array/to-json' ); + + +// MAIN // + +/** +* Serializes an SGD trainer params object as a JSON object. +* +* @param {Object} params - SGD trainer params object +* @returns {Object} serialized object +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var params = { +* 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), +* 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), +* 'lossFunctionParams': new Float64Array( [ 0.0 ] ), +* 'intercept': 0.0, +* 'maxIter': 1000, +* 'penalty': 'l2', +* 'learningRate': 'constant', +* 'lossFunction': 'hinge', +* 'fitIntercept': true, +* 'method': 'Stochastic Gradient Descent', +* }; +* +* var obj = toJSON( params ); +* // returns {...} +*/ +function toJSON( params ) { + return { + 'fitIntercept': params.fitIntercept, + 'intercept': params.intercept, + 'maxIter': params.maxIter, + 'penalty': params.penalty, + 'learningRate': params.learningRate, + 'lossFunction': params.lossFunction, + 'penaltyParams': typedarray2json( params.penaltyParams ), + 'learningRateParams': typedarray2json( params.learningRateParams ), + 'lossFunctionParams': typedarray2json( params.lossFunctionParams ), + 'method': params.method + }; +} + + +// EXPORTS // + +module.exports = toJSON; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/package.json b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/package.json new file mode 100644 index 000000000000..0df12f43e9bc --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ml/base/sgd/params/to-json", + "version": "0.0.0", + "description": "Serialize an SGD trainer params object as a JSON object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "ml", + "machine learning", + "sgd", + "stochastic gradient descent", + "params", + "utilities", + "utility", + "utils", + "util", + "json" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/test/test.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/test/test.js new file mode 100644 index 000000000000..44e5215fd9b3 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-json/test/test.js @@ -0,0 +1,86 @@ +/** +* @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 tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +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 params2json = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof params2json, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function serializes a params object to JSON', function test( t ) { + var expected; + var lambda; + var actual; + var value; + var eta0; + + lambda = 2.5; + eta0 = 0.01; + + value = new Float64Params(); + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + expected = { + 'penaltyParams': { + 'type': 'Float64Array', + 'data': [ 2.5, 0.0 ] + }, + 'learningRateParams': { + 'type': 'Float64Array', + 'data': [ 0.01, 0.0 ] + }, + 'lossFunctionParams': { + 'type': 'Float64Array', + 'data': [ 0.0 ] + }, + 'intercept': 0.0, + 'maxIter': 1000, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true, + 'method': 'Stochastic Gradient Descent' + }; + + actual = params2json( value ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/README.md b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/README.md new file mode 100644 index 000000000000..7ddba3578bd8 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/README.md @@ -0,0 +1,152 @@ + + +# params2str + +> Serialize an SGD trainer params object as a formatted string. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var params2str = require( '@stdlib/ml/base/sgd/params/to-string' ); +``` + +#### params2str( params\[, options] ) + +Serializes an SGD trainer params object as a formatted string. + +```javascript +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); + +var params = new Float64Params(); + +// ... + +var s = params2str( params ); +// returns +``` + +The function supports the following parameters: + +- **params**: SGD trainer params object. +- **options**: function options. + +The function supports the following options: + +- **digits**: number of digits to display after decimal points. Default: `4`. + +
+ + + + + +
+ +## Notes + +- Example output: + + ```text + + Stochastic Gradient Descent + + penalty: l2 + learning rate: constant + loss function: hinge + lambda: 2.5000 + eta0: 0.0100 + fit intercept: true + intercept: 0.0000 + max iterations: 1000 + + ``` + +
+ + + + + +
+ +## Examples + + + +```javascript +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +var Float64Array = require( '@stdlib/array/float64' ); +var params2str = require( '@stdlib/ml/base/sgd/params/to-string' ); + +var params = new Float64Params(); +params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +params.intercept = 0.0; +params.maxIter = 500; +params.penalty = 'l2'; +params.learningRate = 'constant'; +params.lossFunction = 'hinge'; +params.fitIntercept = true; + +var s = params2str( params ); +console.log( s ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/benchmark/benchmark.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/benchmark/benchmark.js new file mode 100644 index 000000000000..0588ea220c37 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @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 Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +var Float32Params = require( '@stdlib/ml/base/sgd/params/float32' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var params2str = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var v; + var i; + + values = [ + new Float64Params(), + new Float32Params() + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = params2str( values[ i%values.length ] ); + if ( typeof v !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( v ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/repl.txt b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/repl.txt new file mode 100644 index 000000000000..5185404bff28 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/repl.txt @@ -0,0 +1,40 @@ + +{{alias}}( params[, options] ) + Serializes an SGD trainer params object as a formatted string. + + Parameters + ---------- + params: Object + SGD params object. + + options: Object (optional) + Function options. + + options.digits: number (optional) + Number of digits to display after decimal points. Default: 4. + + Returns + ------- + out: string + Serialized params. + + Examples + -------- + > var params = { + ... 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + ... 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + ... 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + ... 'intercept': 0.0, + ... 'maxIter': 1000, + ... 'penalty': 'l2', + ... 'learningRate': 'constant', + ... 'lossFunction': 'hinge', + ... 'fitIntercept': true, + ... 'method': 'Stochastic Gradient Descent', + ... }; + > var s = {{alias}}( params ) + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/types/index.d.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/types/index.d.ts new file mode 100644 index 000000000000..7c5365dddd8a --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/types/index.d.ts @@ -0,0 +1,132 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Interface describing SGD trainer parameters. +*/ +interface Params { + /** + * Parameters specific to the regularization function being used. + */ + penaltyParams: Float64Array | Float32Array; + + /** + * Parameters specific to the learning rate scheduler being used. + */ + learningRateParams: Float64Array | Float32Array; + + /** + * Parameters specific to the loss function being used. + */ + lossFunctionParams: Float64Array | Float32Array; + + /** + * Initial intercept value. + */ + intercept: number; + + /** + * Maximum number of iterations to run. + */ + maxIter: number; + + /** + * Regularization function to be used. + */ + penalty: string; + + /** + * Learning rate scheduler to be used. + */ + learningRate: string; + + /** + * Loss function to be used. + */ + lossFunction: string; + + /** + * Boolean indicating whether to include intercept. + */ + fitIntercept: boolean; +} + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Number of digits to display after decimal points. Default: 4. + */ + digits?: number; +} + +/** +* Serializes an SGD trainer params object as a formatted string. +* +* ## Notes +* +* - Example output: +* +* ```text +* +* Stochastic Gradient Descent +* +* penalty: l2 +* learning rate: constant +* loss function: hinge +* lambda: 2.5000 +* eta0: 0.0100 +* fit intercept: true +* intercept: 0.0000 +* max iterations: 1000 +* +* ``` +* +* @param params - SGD trainer params object +* @param options - options object +* @param options.digits - number of digits to display after decimal points +* @returns serialized params +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var params = { +* 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), +* 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), +* 'lossFunctionParams': new Float64Array( [ 0.0 ] ), +* 'intercept': 0.0, +* 'maxIter': 1000, +* 'penalty': 'l2', +* 'learningRate': 'constant', +* 'lossFunction': 'hinge', +* 'fitIntercept': true, +* 'method': 'Stochastic Gradient Descent', +* }; +* +* var str = params2str( params ); +* // returns +*/ +declare function params2str( params: Params, options?: Options ): string; + + +// EXPORTS // + +export = params2str; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/types/test.ts b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/types/test.ts new file mode 100644 index 000000000000..09f624dc40d3 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/docs/types/test.ts @@ -0,0 +1,111 @@ +/* +* @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. +*/ + +import params2str = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + const params = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 1000, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true, + 'method': 'Stochastic Gradient Descent' + }; + params2str( params ); // $ExpectType string + params2str( params, {} ); // $ExpectType string +} + +// The compiler throws an error if provided first argument which is not a params object... +{ + params2str( '10' ); // $ExpectError + params2str( 10 ); // $ExpectError + params2str( true ); // $ExpectError + params2str( false ); // $ExpectError + params2str( null ); // $ExpectError + params2str( undefined ); // $ExpectError + params2str( [] ); // $ExpectError + params2str( {} ); // $ExpectError + params2str( ( x: number ): number => x ); // $ExpectError + + params2str( '10', {} ); // $ExpectError + params2str( 10, {} ); // $ExpectError + params2str( true, {} ); // $ExpectError + params2str( false, {} ); // $ExpectError + params2str( null, {} ); // $ExpectError + params2str( undefined, {} ); // $ExpectError + params2str( [], {} ); // $ExpectError + params2str( {}, {} ); // $ExpectError + params2str( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if provided a second argument which is not an object... +{ + const params = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 1000, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true, + 'method': 'Stochastic Gradient Descent' + }; + + params2str( params, '10' ); // $ExpectError + params2str( params, 10 ); // $ExpectError + params2str( params, true ); // $ExpectError + params2str( params, false ); // $ExpectError + params2str( params, null ); // $ExpectError + params2str( params, [] ); // $ExpectError + params2str( params, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if provided a `digits` option which is not a number... +{ + const params = { + 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), + 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), + 'lossFunctionParams': new Float64Array( [ 0.0 ] ), + 'intercept': 0.0, + 'maxIter': 1000, + 'penalty': 'l2', + 'learningRate': 'constant', + 'lossFunction': 'hinge', + 'fitIntercept': true, + 'method': 'Stochastic Gradient Descent' + }; + + params2str( params, { 'digits': '10' } ); // $ExpectError + params2str( params, { 'digits': true } ); // $ExpectError + params2str( params, { 'digits': false } ); // $ExpectError + params2str( params, { 'digits': null } ); // $ExpectError + params2str( params, { 'digits': [] } ); // $ExpectError + params2str( params, { 'digits': {} } ); // $ExpectError + params2str( params, { 'digits': ( x: number ): number => x } ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/examples/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/examples/index.js new file mode 100644 index 000000000000..dff958dc9bbb --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +var Float64Array = require( '@stdlib/array/float64' ); +var params2str = require( './../lib' ); + +var params = new Float64Params(); +params.penaltyParams = new Float64Array( [ 2.5, 0.0 ] ); +params.learningRateParams = new Float64Array( [ 0.01, 0.0 ] ); +params.lossFunctionParams = new Float64Array( [ 0.0 ] ); +params.intercept = 0.0; +params.maxIter = 500; +params.penalty = 'l2'; +params.learningRate = 'constant'; +params.lossFunction = 'hinge'; +params.fitIntercept = true; + +var s = params2str( params ); +console.log( s ); diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/lib/index.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/lib/index.js new file mode 100644 index 000000000000..a934370bb7e0 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/lib/index.js @@ -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'; + +/** +* Serialize an SGD trainer params object as a formatted string. +* +* @module @stdlib/ml/base/sgd/params/to-string +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var params2str = require( '@stdlib/ml/base/sgd/params/to-string' ); +* +* var params = { +* 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), +* 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), +* 'lossFunctionParams': new Float64Array( [ 0.0 ] ), +* 'intercept': 0.0, +* 'maxIter': 1000, +* 'penalty': 'l2', +* 'learningRate': 'constant', +* 'lossFunction': 'hinge', +* 'fitIntercept': true, +* 'method': 'Stochastic Gradient Descent', +* }; +* +* var str = params2str( params ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/lib/main.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/lib/main.js new file mode 100644 index 000000000000..56aab8b4a298 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/lib/main.js @@ -0,0 +1,139 @@ +/** +* @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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Serializes an SGD trainer params object as a formatted string. +* +* ## Notes +* +* - Example output: +* +* ```text +* +* Stochastic Gradient Descent +* +* penalty: l2 +* learning rate: constant +* loss function: hinge +* lambda: 2.5000 +* eta0: 0.0100 +* fit intercept: true +* intercept: 0.0000 +* max iterations: 1000 +* +* ``` +* +* @param {Object} params - SGD trainer params object +* @param {Options} [opts] - options object +* @param {PositiveInteger} [opts.digits=4] - number of digits to display after decimal points +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {string} serialized params +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var params = { +* 'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ), +* 'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ), +* 'lossFunctionParams': new Float64Array( [ 0.0 ] ), +* 'intercept': 0.0, +* 'maxIter': 1000, +* 'penalty': 'l2', +* 'learningRate': 'constant', +* 'lossFunction': 'hinge', +* 'fitIntercept': true, +* 'method': 'Stochastic Gradient Descent', +* }; +* +* var str = toString( params ); +* // returns +*/ +function toString( params, opts ) { // eslint-disable-line stdlib/no-redeclare + var fitIntercept; + var dgts; + var out; + + dgts = 4; + if ( arguments.length > 1 ) { + if ( !isObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Must provide an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'digits' ) ) { + if ( !isPositiveInteger( opts.digits ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'digits', opts.digits ) ); + } + dgts = opts.digits; + } + } + + fitIntercept = 'false'; + if ( params.fitIntercept === true ) { + fitIntercept = 'true'; + } + + out = [ + '', + params.method, + '', + format( ' penalty: %s', params.penalty ), + format( ' learning rate: %s', params.learningRate ), + format( ' loss function: %s', params.lossFunction ) + ]; + if ( params.penalty !== 'none' ) { + out.push( format( ' lambda: %0.'+dgts+'f', params.penaltyParams[ 0 ] ) ); + } + else if ( params.penalty === 'elasticnet' ) { + out.push( format( ' l1 ratio: %0.'+dgts+'f', params.penaltyParams[ 1 ] ) ); + } + if ( params.learningRate === 'constant' ) { + out.push( format( ' eta0: %0.'+dgts+'f', params.learningRateParams[ 0 ] ) ); + } + else if ( params.learningRate === 'invscaling' ) { + out.push( format( ' eta0: %0.'+dgts+'f', params.learningRateParams[ 0 ] ) ); + out.push( format( ' powerT: %0.'+dgts+'f', params.learningRateParams[ 1 ] ) ); + } else if ( params.learningRate === 'pegasos' && params.penalty === 'none' ) { + out.push( format( ' lambda: %0.'+dgts+'f', params.learningRateParams[ 1 ] ) ); + } + if ( params.lossFunction === 'epsilon-insensitive' || params.lossFunction === 'squared-epsilon-insensitive' ) { + out.push( format( ' epsilon: %0.'+dgts+'f', params.lossFunctionParams[ 0 ] ) ); + } + out.push( format( ' fit intercept: %s', fitIntercept ) ); + if ( params.fitIntercept === true ) { + out.push( format( ' intercept: %0.'+dgts+'f', params.intercept ) ); + } + out.push( format( ' max iterations: %d', params.maxIter ) ); + return out.join( '\n' ); +} + + +// EXPORTS // + +module.exports = toString; diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/package.json b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/package.json new file mode 100644 index 000000000000..2fd1b2a2c7cd --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ml/base/sgd/params/to-string", + "version": "0.0.0", + "description": "Serialize an SGD trainer params object as a formatted string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "ml", + "machine learning", + "sgd", + "stochastic gradient descent", + "params", + "utilities", + "utility", + "utils", + "util", + "string" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/test/test.js b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/test/test.js new file mode 100644 index 000000000000..affb56e8f043 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/base/sgd/params/to-string/test/test.js @@ -0,0 +1,523 @@ +/** +* @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 tape = require( 'tape' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var Float64Array = require( '@stdlib/array/float64' ); +var Float64Params = require( '@stdlib/ml/base/sgd/params/float64' ); +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 params2str = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof params2str, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var params; + var values; + var i; + + params = new Float64Params(); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params2str( params, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `digits` option which is not a positive integer', function test( t ) { + var params; + var values; + var i; + + params = new Float64Params(); + + values = [ + '5', + -5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + params2str( params, { + 'digits': value + }); + }; + } +}); + +tape( 'the function serializes a params object to a string (l1,l2 penalty)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + var eta0; + + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: constant', + ' loss function: hinge', + ' lambda: 2.5000', + ' eta0: 0.0100', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (elasticnet penalty)', function test( t ) { + var expected; + var l1Ratio; + var lambda; + var actual; + var value; + var eta0; + + l1Ratio = 0.6; + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, l1Ratio ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'elasticnet' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: elasticnet', + ' learning rate: constant', + ' loss function: hinge', + ' lambda: 2.5000', + ' l1 ratio: 0.6000', + ' eta0: 0.0100', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (no penalty)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + var eta0; + + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'none' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: none', + ' learning rate: constant', + ' loss function: hinge', + ' eta0: 0.0100', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (basic learning rate)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + + lambda = 2.5; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ 0.0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'basic' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: basic', + ' loss function: hinge', + ' lambda: 2.5000', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (constant learning rate)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + var eta0; + + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: constant', + ' loss function: hinge', + ' lambda: 2.5000', + ' eta0: 0.0100', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (invscaling learning rate)', function test( t ) { + var expected; + var powerT; + var lambda; + var actual; + var value; + var eta0; + + powerT = 2.0; + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, powerT ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'invscaling' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: invscaling', + ' loss function: hinge', + ' lambda: 2.5000', + ' eta0: 0.0100', + ' powerT: 2.0000', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (pegasos learning rate)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + + lambda = 2.5; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ lambda, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'pegasos' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: pegasos', + ' loss function: hinge', + ' lambda: 2.5000', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (epsilon insensitive, squared epsilon insensitive loss function)', function test( t ) { + var expected; + var epsilon; + var lambda; + var actual; + var value; + var eta0; + + epsilon = 0.2; + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ epsilon ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'squared-epsilon-insensitive' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: invscaling', + ' loss function: hinge', + ' lambda: 2.5000', + ' eta0: 0.0100', + ' epsilon: 0.2000', + ' fit intercept: true', + ' intercept: 0.0000', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes a params object to a string (no intercept)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + var eta0; + + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = false; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: constant', + ' loss function: hinge', + ' lambda: 2.5000', + ' eta0: 0.0100', + ' fit intercept: false', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the number of displayed digits (digits=2)', function test( t ) { + var expected; + var lambda; + var actual; + var value; + var eta0; + + lambda = 2.5; + eta0 = 0.01; + + value.penaltyParams = new Float64Array( [ lambda, 0.0 ] ); + value.learningRateParams = new Float64Array( [ eta0, 0.0 ] ); + value.lossFunctionParams = new Float64Array( [ 0.0 ] ); + value.intercept = 0.0; + value.maxIter = 1000; + value.penalty = resolvePenaltyEnum( 'l2' ); + value.learningRate = resolveLREnum( 'constant' ); + value.lossFunction = resolveLossFunctionEnum( 'hinge' ); + value.fitIntercept = true; + + actual = params2str( value ); + t.strictEqual( isString( actual ), true, 'returns expected value' ); + + expected = [ + '', + 'Stochastic Gradient Descent', + '', + ' penalty: l2', + ' learning rate: constant', + ' loss function: hinge', + ' lambda: 2.50', + ' eta0: 0.01', + ' fit intercept: true', + ' intercept: 0.00', + ' max iterations: 1000', + '' + ].join( '\n' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +});