Open In App

Tensorflow.js tf.LayersModel class .fitDataset() 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.js tf.LayersModel class .fitDataset() method is used to trains the layer model by using a dataset object.

Syntax:



LayerModel.fitDataset(dataset, args);

Parameters: This method accepts the following parameters:

Returns: Promise< History >



 

Example 1: In this example, we will train our layer model with CSV dataset.




import * as tf from "@tensorflow/tfjs"
  
// Path for the CSV file 
const gfg_CsvFile =
  
async function run() {
  
    // Creating model
    const gfg_LayerModel = tf.sequential();
  
    // Adding layer to model
    const config = { units: 1, inputShape: [12] }
    const gfg_layer = tf.layers.dense(config);
    gfg_LayerModel.add(gfg_layer);
  
    // Compiling the model
    const opt = tf.train.sgd(0.00000001)
    gfg_LayerModel.compile({ optimizer: opt, 
            loss: 'meanSquaredError' });
  
    // Here we want to predict column tax
    const config2 = { columnConfigs: {
             rad: { isLabel: true } } };
  
    const csvDataset = tf.data.csv(gfg_CsvFile, config2);
  
    // Creating dataset for training
    const flattenedDataset =
        csvDataset.map(({ xs, ys }) => {
            return { xs: Object.values(xs), 
                    ys: Object.values(ys) };
        }).batch(6);
  
    // Training the model
    const Tm = await gfg_LayerModel.fitDataset(
            flattenedDataset, { epochs: 10 });
  
    for (let i = 0; i < 6; i++) {
        console.log(Tm.history.loss[i])
    }
}
run();

Output:

41480.109375
28887.93359375
20162.228515625
14115.478515625
9924.9404296875
7020.56005859375

Example 2: In this example, we will train our layer model with a dataset of the array.




import * as tf from "@tensorflow/tfjs"
  
async function run() {
  
    // Creating Layer model
    const gfg_LayerModel = tf.sequential();
  
    // Adding layer to model
    const config = { units: 4, inputShape: [4] }
    const gfg_layer = tf.layers.dense(config);
    gfg_LayerModel.add(gfg_layer);
  
    // Compiling the model
    const config2 = { optimizer: 'sgd', loss: 'meanSquaredError' }
    gfg_LayerModel.compile(config2);
  
    // Creating Datasets for training
    const array1 = [
        [1, 2, 3, 4],
        [1, 4, 6, 8],
        [1, 3, 4, 7],
        [3, 4, 7, 8]
    ];
    const array2 = [1, 1, 1, 1];
    const arrData1 = tf.data.array(array1);
    const arrData2 = tf.data.array(array2);
  
    const config3 = { xs: arrData1, ys: arrData2 }
    const arrayDataset = tf.data.zip(config3)
    const ArrayDataset = arrayDataset.batch(4);
  
    // Training the model
    const Tm = await gfg_LayerModel.fitDataset(
            ArrayDataset, { epochs: 4 });
  
    // Printing the loss after training
    console.log("Loss After Training Layer Model"
        + Tm.history.loss[0]);
}
run();

Output:

Loss After Training Layer Model 4.386415958404541

Reference: https://js.tensorflow.org/api/latest/#tf.LayersModel.fitDataset


Article Tags :