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


Tensorflow.js tf.regularizers.l1()用法及代码示例

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

.regularizers.l1() 函数用于 L1 正则化。此外,它为损失附加了一个名称以谴责巨大的权重:loss += sum(l1 * abs(x))。

用法:

tf.regularizers.l1(config?)

参数:

  • config:它是一个可选的对象。在它下面是l1。
  • l1:它是规定的 L1 正则化率,默认值为 0.01。它是类型号。

返回值:它返回正则化器。



范例1:在这个例子中,我们将看到单独使用 l1 正则化器应用于核权重矩阵。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Adding layer to it and calling 
// regularizers.l1() method
model.add(tf.layers.dense({
    units:37, batchInputShape:[null, 40],
    kernelRegularizer:tf.regularizers.l1()
}));
  
// Calling summary() method and 
// Printing output
model.summary();

输出:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense52 (Dense)        [null,37]                 1517      
=================================================================
Total params:1517
Trainable params:1517
Non-trainable params:0
_________________________________________________________________

范例2:在这个例子中,我们将看到单独使用 l1 正则化器应用于偏置向量。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Adding layer to it and calling 
// regularizers.l1() method
model.add(tf.layers.dense({
    units:2, batchInputShape:[null, 13],
    biasRegularizer:tf.regularizers.l1()
}));
  
// Calling summary() method and 
// Printing output
model.summary();

输出:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense54 (Dense)        [null,2]                  28        
=================================================================
Total params:28
Trainable params:28
Non-trainable params:0
_________________________________________________________________

参考: https://js.tensorflow.org/api/latest/#regularizers.l1




相关用法


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