Multi-layer Perceptron Regressor
MLPRegressor is a class for fitting a regression task using multi-layer perceptron neural network
Constructor
Import
import * as Datacook from 'datacook';
const { MLPRegressor } = DataCook.Model;
Constructor
const mlp = new MLPRegressor({ hiddenLayerSizes: [ 5, 6, 7 ], activations: 'relu' });
Option parameters
parameter | type | description |
---|---|---|
hiddenLayerSizes | number[] | size of nodes in hidden layers, length of array represents number of hidden layers in the network |
activations | { ‘relu’ | ‘sigmoid’ | ‘tanh’ } or Array of { ‘relu’ | ‘sigmoid’ | ‘tanh’ } | activation function of hidden layers, default is ‘sigmoid’ |
optimizerType | ‘sgd’ | ‘momentum’ | ‘adagrad’ | ‘adadelta’ | ‘adam’ | ‘adamax’ | ‘rmsprop’ | optimizer types for training. All of the following optimizers types supported in tensorflow.js can be applied. Default to ‘adam’ |
optimizerProps | OptimizerProps | parameters used to init corresponding optimizer, you can refer to documentations in tensorflow.js to find the supported initailization paratemters for a given type of optimizer. For example, { learningRate: 0.1, beta1: 0.1, beta2: 0.2, epsilon: 0.1 } could be used to initialize adam optimizer. |
Methods
fit
Fit model according to X, y.
async fit(xData: Tensor | RecursiveArray<number>,
yData: Tensor | RecursiveArray<number>,
params)
Parameters
parameter | type | description |
---|---|---|
xData | Tensor | RecursiveArray | Tensor like of shape (n_samples, n_features), input feature |
yData | Tensor | RecursiveArray | Tensor like of shape (n_sample, ), input target values |
params | MLPRegressorTrainParams | option in params: - batchSize: batch size: default to 32, - epochs: epochs for training, default to Math.ceil(10000 / ( nData / batchSize )) |
predict
Make predictions using logistic regression model.
async predict(xData: Tensor | RecursiveArray<number>): Promise<Tensor>
Parameters
parameter | type | description | |
---|---|---|---|
xData | Tensor | RecursiveArray | Input features |
fromJson
Load model paramters from json string object
async fromJson(modelJson: string)
Parameters
parameter | type | description |
---|---|---|
modelJson | string | model json string |
Examples
import '@tensorflow/tfjs-backend-cpu';
import * as tf from '@tensorflow/tfjs-core';
import * as DataCook from '@pipcook/datacook';
const MLPRegressor = DataCook.Model;
const cases = tf.mul(tf.randomNormal([ 10000, 5 ]), [ 1, 10, 100, 2, 3 ]);
const weight = tf.tensor([ 2, 3, 1, 4, 6 ]);
const y = tf.add(tf.sum(tf.mul(cases, weight), 1), 10);
const mlp = new MLPRegressor({
hiddenLayerSizes: [ 4, 10 ],
activations: 'relu',
optimizerType: 'adam',
optimizerProps: { learningRate: 0.01 }
});
// model fit
await mlp.fit(cases, y);
// predict
const yPred = await mlp.predict(cases) as Tensor1D;
// save and load
const modelJson = await mlp.toJson();
const mlp2 = new MLPRegressor({});
mlp2.fromJson(modelJson);
const yPred2 = await mlp2.predict(cases);
toJson
Dump model parameters to json string.
async toJson(): Promise<string>