當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.regularizers.l2()用法及代碼示例

Tensorflow.js 中的正則化器附有模型的各種組件,這些組件與評分函數一起工作以幫助驅動可訓練的值、大值。 tf.regularizers.l2() 方法繼承自regularizers 類。 tf.regularizers.l2() 方法在模型訓練的懲罰情況下應用 l2 正則化。這種方法在損失中增加了一項以對大權重進行懲罰。它增加了 Loss+=sum(l2 * x^2) 損失。所以在這篇文章中,我們將看到 tf.regularizers.l2() 函數是如何工作的。

用法:

tf.regularizers.l2 (args);

參數:

  • l2:數字表示正則化率,默認為 0.01。

返回:正則化器



範例1:在這個例子中,我們將看到單獨使用 l2 正則化器應用於核權重矩陣。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Add layer to it
model.add(tf.layers.dense({
    units:32, batchInputShape:[null,50],
    kernelRegularizer:tf.regularizers.l2()
}));
  
// Model summary
model.summary();

輸出:

Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense1 (Dense)         [null,32]                 1632      
=================================================================
Total params:1632
Trainable params:1632
Non-trainable params:0

範例2:在這個例子中,我們將看到應用於偏置向量的 l2 正則化器的獨立使用。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Add layer to it
model.add(tf.layers.dense({
    units:32, batchInputShape:[null,50],
    biasRegularizer:tf.regularizers.l2()
}));
  
// Model summary
model.summary();

輸出:

Layer (type)                 Output shape              Param #    
=================================================================
dense_Dense2 (Dense)         [null,32]                 1632      
=================================================================
Total params:1632
Trainable params:1632
Non-trainable params:0

參考文獻:https://js.tensorflow.org/api/latest/#regularizers.l2




相關用法


注:本文由純淨天空篩選整理自abhijitmahajan772大神的英文原創作品 Tensorflow.js tf.regularizers.l2() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。