Tensorflow.js tf.Sequential class .fit() Method Last Updated : 15 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 ). Syntax: 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. JavaScript import * as tf from "@tensorflow/tfjs" // Creating model const model = tf.sequential( ) ; // Adding layer to model const config = {units: 1, inputShape: [1]} const x = tf.layers.dense(config); model.add(x); // Compiling the model const config2 = {optimizer: 'sgd', loss: 'meanSquaredError'} model.compile(config2); // Tensor for training const xs = tf.tensor2d([1, 1.2,1.65, 1.29, 1.4, 1.7], [6, 1]); const ys = tf.tensor2d([1, 0.07,0.17, 0.29, 0.43, 1.02], [6, 1]); // Training the model const Tm = await model.fit(xs, ys); // Printing the loss after training console.log("Loss " + " : " + Tm.history.loss[0]); Output: Loss after Epoch : 2.8400533199310303 Example 2: In this example will train our model by making some configuration. JavaScript import * as tf from "@tensorflow/tfjs" // Creating model const Model = tf.sequential( ) ; // Adding layer to model const config = {units: 4, inputShape: [2], activation : "sigmoid"} const x = tf.layers.dense(config); Model.add(x); const config2 = {units: 3, activation : "sigmoid"} const y = tf.layers.dense(config2); Model.add(y); // Compiling the model const sgdOpt = tf.train.sgd(0.6) const config3 = {optimizer: 'sgd', loss: 'meanSquaredError'} Model.compile(config3); // Test Tensor const xs = tf.tensor2d([ [0.3, 0.24], [0.12, 0.73], [0.9, 0.54] ]); const ys = tf.tensor2d([ [0.43, 0.5, 0.92], [0.1, 0.39, 0.12], [0.76, 0.4, 0.92] ]); // Training the model for( let i = 0; i < 5; i++){ const config = { shuffle : true, epoch : 10 } const Tm = await Model.fit(xs, ys, config); // Printing the loss after training console.log("Loss " + " : " + Tm.history.loss[0]); } Output: Loss : 0.1362573206424713 Loss : 0.13617873191833496 Loss : 0.13610021770000458 Loss : 0.13602176308631897 Loss : 0.13594339787960052 Reference: https://js.tensorflow.org/api/3.8.0/#tf.Sequential.fit Comment More infoAdvertise with us Next Article Tensorflow.js tf.Sequential class .fit() Method S satyam00so Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Like