Tensorflow.js tf.Sequential class .fit() Method
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
Tensorflow.jstf.Sequential class .fit( ) method is used to train the model for the fixed number of epochs( iterations on a dataset ).
model.fit( x, y, args?);
Parameters: This method accept following parameters:
- x: It is tf.Tensor that contains all the input data.
- y: It is tf.Tensor that contains all the output data.
- args: It is object type, it’s variables are follows:
- batchSize: It define the number of samples that will propagate through training.
- epochs: It define iteration over the training data arrays.
- verbose: It help in showing the progress for each epoch.
If the value is 0 – It means no printed message during fit() call. If the value is 1 – It means in Node-js , it prints the progress bar. In the browser it shows no action. Value 1 is the default value . 2 – Value 2 is not implement yet.
- callbacks: It define list of callbacks to be call during training. Variable can have one or more of these callbacks onTrainBegin( ), onTrainEnd( ), onEpochBegin( ), onEpochEnd( ), onBatchBegin( ), onBatchEnd( ), onYield( ).
- validationSplit: It makes easy for the user to split the training dataset into train and validation.
For example : if it value is validation-Split = 0.5 ,it means use last 50% of data before shuffling for validation.
- validationData: It is used to give an estimate of final model when selecting between final models.
- shuffle: This value define shuffle of the data before each epoch. it has not effect when stepsPerEpoch is not null.
- classWeight: It is used for weighting the loss function. It can be useful to tell the model to pay more attention to samples from an under-represented class.
- sampleWeight: It is array of weights to apply to model’s loss for each sample.
- initialEpoch: It is value define epoch at which to start training. It is useful for resuming a previous training run.
- stepsPerEpoch: It define number of batches of samples before declaring one epoch finished and starting the next epoch. It is equal to 1 if not determined.
- validationSteps: It is relevant if stepsPerEpoch is specified. Total number of steps to validate before stopping.
- yieldEvery: It define configuration of the frequency of yielding the main thread to other tasks. It can be auto, It means the yielding happens at a certain frame rate. batch, If the value is this, It yield every batch. epoch, If the value is this, It yield every epoch. any number, If the value is any number, it yield every number milliseconds.never, If the value is this, it never yield.
Returns: Promise< History >
Example 1: In this example we will train our model by default configuration.
Loss after Epoch : 2.8400533199310303
Example 2: In this example will train our model by making some configuration.
Loss : 0.1362573206424713 Loss : 0.13617873191833496 Loss : 0.13610021770000458 Loss : 0.13602176308631897 Loss : 0.13594339787960052