📌  相关文章
📜  Tensorflow.js tf.LayersModel 类 .fit() 方法(1)

📅  最后修改于: 2023-12-03 14:47:55.353000             🧑  作者: Mango

TensorFlow.js - tf.LayersModel.fit() Method

Introduction

TensorFlow.js is a library for building and training machine learning models in JavaScript. The tf.LayersModel.fit() method is used to train a neural network model on a given set of training data.

Usage

The tf.LayersModel.fit() method takes in several parameters:

  • x: This is the input data. It can be either an array or a tensor.
  • y: This is the output data. It can be either an array or a tensor.
  • batchSize: The number of samples to use in each training batch.
  • epochs: The number of times to iterate over the entire training dataset.
  • callbacks: You can pass a list of callback functions that will be called during the training process.
  • validationSplit: This option specifies the fraction of input data to use as validation data.
  • shuffle: This option specifies whether to shuffle the training data before each epoch.

Here's an example usage of the tf.LayersModel.fit() method:

const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [784], units: 32, activation: 'relu'}));
model.add(tf.layers.dense({units: 10, activation: 'softmax'}));
model.compile({optimizer: 'adam', loss: 'categoricalCrossentropy'});

const xTrain = tf.ones([100, 784]);
const yTrain = tf.oneHot(tf.zeros([100], 'int32'), 10);

await model.fit(xTrain, yTrain, {
  batchSize: 32,
  epochs: 10,
  callbacks: {
    onEpochEnd: async (epoch, logs) => {
      console.log(`Epoch ${epoch}: loss = ${logs.loss}`);
    }
  },
  validationSplit: 0.1,
  shuffle: true
});
Conclusion

The tf.LayersModel.fit() method is an important method for training a neural network model in TensorFlow.js. By passing in input and output data, as well as several training options, you can train a model to make accurate predictions.