当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Tensorflow.js tf.Sequential.fit()用法及代码示例


Tensorflow.js是Google开发的开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。

Tensorflow.jstf.Sequential 类的 .fit() 方法用于训练模型的固定次数(数据集上的迭代)。

用法:

model.fit( x, y, args?);

参数:此方法接受以下参数:

  • x:tf.Tensor 包含所有输入数据。
  • y:tf.Tensor 包含所有输出数据。
  • args:它是对象类型,它的变量如下:
    • batchSize:它定义了将通过训练传播的样本数量。
    • epochs:它定义了训练数据数组的迭代。
    • verbose:它有助于显示每个时期的进度。
      如果值为 0 - 表示在 fit() 调用期间没有打印消息。如果值为 1 - 这意味着在 Node-js 中,它会打印进度条。在浏览器中它不显示任何操作。值 1 是默认值。 2 - 值 2 尚未实现。
    • callbacks:它定义了在训练期间要调用的回调列表。变量可以有一个或多个这些回调onTrainBegin()、onTrainEnd()、onEpochBegin()、onEpochEnd()、onBatchBegin()、onBatchEnd()、onYield()。
    • validationSplit:用户可以轻松地将训练数据集拆分为训练和验证。
      例如:如果它的值为 validation-Split = 0.5 ,则表示使用 shuffle 前最后 50% 的数据进行验证。
    • validationData:它用于在最终模型之间进行选择时给出最终模型的估计。
    • shuffle:该值定义了每个时期之前数据的洗牌。当 stepsPerEpoch 不为空时,它不起作用。
    • classWeight:它用于对损失函数进行加权。告诉模型更多地关注来自 under-represented 类的样本会很有用。
    • sampleWeight:它是应用于每个样本的模型损失的权重数组。
    • initialEpoch:它是值定义开始训练的时期。它对于恢复之前的训练运行很有用。
    • stepsPerEpoch:它在宣布一个 epoch 完成并开始下一个 epoch 之前定义了样本的批次数。如果未确定,则等于 1。
    • validationSteps:如果指定了 stepsPerEpoch,则相关。停止前要验证的总步数。
    • yieldEvery:它定义了将主线程交给其他任务的频率的配置。它可以是自动的,这意味着屈服发生在一定的帧率下。批次,如果值是这个,它会产生每批。 epoch,如果值是这个,它产生每个纪元。任何数字,如果值为任何数字,则每毫秒产生一次。从不,如果值为这个,则永远不会产生。

返回值:承诺<历史>



范例1:在这个例子中,我们将在默认配置下训练我们的模型。

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]);

输出:

Loss after Epoch :2.8400533199310303

范例2:在这个例子中,我们将通过进行一些配置来训练我们的模型。

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]
                        ]);
  
// Treaining the model
for( let i = 0; i < 5; i++){
const config = { suffle:true, epoch:10 }
const Tm = await Model.fit(xs, ys, config);
  
// Printing the loss after training
console.log("Loss " + ":" + Tm.history.loss[0]);
  
}

输出:

Loss  :0.1362573206424713
Loss  :0.13617873191833496
Loss  :0.13610021770000458
Loss  :0.13602176308631897
Loss  :0.13594339787960052

参考: https://js.tensorflow.org/api/3.8.0/#tf.Sequential.fit




相关用法


注:本文由纯净天空筛选整理自satyam00so大神的英文原创作品 Tensorflow.js tf.Sequential class .fit() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。