Tensorflow.js tf.layers computeOutputShape() 方法
Tensorflow.js 是由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.computeOutputShape()函数用于枚举指定层的输出形状。它假定将创建图层以匹配提供的输入形状。
句法:
computeOutputShape(inputShape)
参数:
- inputShape:它是指定的形状,即整数集或形状集列表。此外, Shape元组可以包含 void 以支持自由大小,而不是整数。它可以是 ((null | number)[]|(null | number)[][]) 类型。
返回值:返回(null | number)[]|(null | number)[][]。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding a layer
model.add(tf.layers.dense({units: 1, inputShape: [3]}));
// Defining inputShape
const inputShape = [6, 2, 6];
// Calling computeOutputShape() method with its
// parameter
const val = model.layers[0].computeOutputShape(inputShape);
// Printing output
console.log(val);
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units: 1, inputShape: [3]}));
model.add(tf.layers.dense({units: 5}));
// Defining inputShape
const inputShape1 = [6, 2, 6, null];
const inputShape2 = [6.5, 2.6, 9.1, NaN];
// Calling computeOutputShape() method with its
// parameter
const val1 = model.layers[0].computeOutputShape(inputShape1);
const val2 = model.layers[1].computeOutputShape(inputShape2);
// Printing output
console.log(val1);
console.log(val2);
输出:
6,2,1
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units: 1, inputShape: [3]}));
model.add(tf.layers.dense({units: 5}));
// Defining inputShape
const inputShape1 = [6, 2, 6, null];
const inputShape2 = [6.5, 2.6, 9.1, NaN];
// Calling computeOutputShape() method with its
// parameter
const val1 = model.layers[0].computeOutputShape(inputShape1);
const val2 = model.layers[1].computeOutputShape(inputShape2);
// Printing output
console.log(val1);
console.log(val2);
输出:
6,2,6,1
6.5,2.6,9.1,5
参考: https://js.tensorflow.org/api/latest/#tf.layers.Layer.computeOutputShape