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


Tensorflow.js tf.layers.conv2d()用法及代码示例


Tensorflow.js是Google开发的开源工具包,用于在浏览器或节点平台上执行机器学习模型和深度学习神经网络。它还使开发人员能够在 JavaScript 中创建机器学习模型,并直接在浏览器中或通过 Node.js 使用它们。

tf.layers.conv2d() 函数用于对数据应用 2D 卷积运算。

用法:

tf.layers.conv2d(args)

参数:它接受参数对象可以具有以下属性:

  • filters (number):输出空间的维数(即卷积中的滤波器数量)。
  • 内核大小(数字|数字[]):卷积窗口的尺寸。如果 kernelSize 是数字,则卷积窗口将为正方形。
  • 步幅(数字|数字[]):每个维度的卷积步长。如果步幅是数字,则两个维度的步幅相等。
  • padding:填充模式。
  • dataFormat:数据格式。这指定了输入中维度的排序顺序。 ChannelLast 是默认值。
  • dilationRate:在每个维度中,用于扩张卷积的扩张率。它应该是一个整数或一个二元或three-int 数组。
  • activation:该层的激活函数。
  • useBias (boolean):该层是否有偏差向量。 True 是默认值。
  • kernelInitializer:卷积核权重矩阵的初始值设定项。
  • biasInitializer:偏置向量的初始值设定项。
  • kernelConstraint:卷积核权重的约束。
  • biasConstraint:偏置向量的约束。
  • kernelRegularizer:应用于核权重矩阵的正则化函数。
  • biasRegularizer:应用于偏置向量的正则化函数。
  • activityRegularizer:应用于激活的正则化函数。
  • inputShape:如果设置了此属性,它将用于构造一个输入层,该输入层将插入到该层之前。
  • batchInputShape:如果设置了此属性,将创建一个输入层并将其插入到该层之前。
  • batchSize:如果未提供batchInputShape而提供了inputShape,则使用batchSize来构建batchInputShape。
  • dtype:这是该层的数据类型。 float32 是默认值。该参数仅适用于输入层。
  • name:这是图层的名称,是字符串类型。
  • trainable:如果该层的权重可以通过拟合来改变。 True 是默认值。
  • weights:图层的初始权重值。

返回:它返回一个对象(Conv2D)。

示例 1:

Javascript


import * as tf from "@tensorflow/tfjs"; 
  
const input = tf.input({ shape: [4, 4, 4] }); 
const conv2DLayer = tf.layers.conv2d({ filters: 2, kernelSize: 2}); 
const output = conv2DLayer.apply(input); 
  
const model = tf.model({ inputs: input, outputs: output }); 
model.predict(tf.ones([1, 4, 4, 4])).print();

输出:

Tensor
   [[[[1.1710266, -1.0081921],
      [1.1710266, -1.0081921],
      [1.1710266, -1.0081921]],
     [[1.1710266, -1.0081921],
      [1.1710266, -1.0081921],
      [1.1710266, -1.0081921]],
     [[1.1710266, -1.0081921],
      [1.1710266, -1.0081921],
      [1.1710266, -1.0081921]]]]

示例 2:

Javascript


import * as tf from "@tensorflow/tfjs"; 
  
const input = tf.input({ shape: [4, 4, 1] }); 
  
const conv2DLayer = tf.layers 
    .conv2d({ filters: 2, kernelSize: 3}); 
      
const output = conv2DLayer.apply(input); 
  
const model = tf.model({ inputs: input, outputs: output }); 
  
const x = tf.tensor4d([1, 2, 3, 4, 5, 6, 7, 8,  
    9, 10, 11, 12, 13, 14, 15, 16], [1, 4, 4, 1]); 
  
model.predict(x).print();

输出:

Tensor
   [[[[2.3521864, 1.9806139],
      [2.9982643, 2.055856 ]],
     [[4.9365001, 2.2815819],
      [5.5825787, 2.3568242]]]]

参考:https://js.tensorflow.org/api/latest/#layers.conv2d



相关用法


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