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


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

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

tf.layers.averagePooling2d() 函数用于对空间数据应用平均池化操作。如果其 dataFormat 字段设置为 CHANNEL_LAST,它将采用 4d 形状 [batchSize、rows、cols、channels] 的张量作为输入,并输出 4d 形状的张量:[batchSize、poolsRows、pooledCols、channels]。其 dataFormat 字段设置为 CHANNEL_FIRST,它以 4d 形状的张量作为输入:[batchSize,channels,rows,cols]并输出形状为:[batchSize,channels,poolsRows,pooledCols]的张量。

用法:

tf.layers.averagePooling2d( args )

参数:

  • args:它是一个具有以下属性的对象:
    • poolSize:它用于缩小每个维度(即[垂直、水平])的因子。它是一个整数或两个整数的数组。
    • strides: 在池化窗口的每个维度中,步幅大小。它是一个整数或两个整数的数组。
    • padding: 用于池化层的填充类型。
    • dataFormat: 用于池化层的数据格式。
    • inputShape: 如果指定了,它将用于构造一个输入层,该输入层将插入到该层之前。
    • batchInputShape: 如果指定,它将用于创建一个输入层,该输入层将插入到该层之前。
    • batchSize: 它支持inputShape来构建batchInputShape。
    • dtype: 这是该层的数据类型。该参数仅适用于输入层。
    • name: 它是字符串类型。这是该层的名称。
    • trainable: 如果设置为 true,则只有该层的权重会因拟合而改变。
    • weights: 图层的初始权重值。

返回:它返回 AveragePooling2D

示例 1:

Javascript


import * as tf from "@tensorflow.js/tfjs"
const model = tf.sequential();
// First layer must have a defined input shape
model.add(tf.layers.averagePooling2d({
    poolSize: 2,
    strides: 3,
    padding: 'valid',
    inputShape: [2, 3, 2]
}));
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 3}));
model.add(tf.layers.averagePooling2d({
    poolSize: 2,
    strides: 3,
    padding: 'valid'
}));
// Inspect the inferred shape of the model's output.
model.summary();

输出:

__________________________________________________________________________________________
Layer (type)                Input Shape               Output shape              Param #   
==========================================================================================
average_pooling2d_AveragePo [[null,2,3,2]]            [null,1,1,2]              0         
__________________________________________________________________________________________
dense_Dense4 (Dense)        [[null,1,1,2]]            [null,1,1,3]              9         
__________________________________________________________________________________________
average_pooling2d_AveragePo [[null,1,1,3]]            [null,0,0,3]              0         
==========================================================================================
Total params: 9
Trainable params: 9
Non-trainable params: 0
__________________________________________________________________________________________

示例 2:

Javascript


import * as tf from "@tensorflow/tfjs";
const Input = tf.input({ shape: [2, 3, 5] });
const averagePooling2DLayer =
    tf.layers.averagePooling2d(
        { dataFormat: 'channelsLast' }
    );
const Output = averagePooling2DLayer.apply(Input);
const Data = tf.ones([2, 2, 3, 5]);
const model =
    tf.model({ inputs: Input, outputs: Output });
model.predict(Data).print();

输出:

Tensor
    [ [ [[1, 1, 1, 1, 1],]],


      [ [[1, 1, 1, 1, 1],]]]

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



相关用法


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