TensorFlowJS 中的单位和 inputShape

Vih*_*aAW 5 javascript tensorflow tensorflow.js

我是 TensorflowJS 和 ML 的新手。在API参考中,有以下代码。

const model = tf.sequential();

// First layer must have an input shape defined.
model.add(tf.layers.dense({units: 32, inputShape: [50]}));

// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 4}));

// Inspect the inferred shape of the model's output, which equals
// `[null, 4]`. The 1st dimension is the undetermined batch dimension; the
// 2nd is the output size of the model's last layer.
console.log(JSON.stringify(model.outputs[0].shape));
Run Code Online (Sandbox Code Playgroud)

我想知道的是,

什么是inputShape

自动形状是什么?

既然unit提到了数据集的属性,为什么unit要设置为4呢model.add(tf.layers.dense({units: 4}))unit(在 中将 定义为 32 的层model.add(tf.layers.dense({units: 32, inputShape: [50]})))由于sequential()一层的输出是下一层的输入,所以单位不是必须相同吗?

Seb*_*tel 4

什么是inputShape

它是一个包含张量维度的数组,在运行神经网络时用作输入。

自动形状是什么?

它只是使用之前层的输出形状。在这种情况下[32],因为之前的层是具有 32 个单元的密集层。

既然单位指的是数据集的属性,为什么单位设置为4就model.add(tf.layers.dense({units: 4}))行了。(该层在中将单位定义为 32model.add(tf.layers.dense({units: 32, inputShape: [50]})))由于顺序()的一层输出是下一层的输入,因此单位不是必须相同吗?

单位定义密集层的输出形状。在这种情况下,神经网络应该有 4 个输出,因此最后一层必须有 4 个单元。输出和输入形状不必相同,因为每个神经元的输出(其数量为输出形状)是基于前一层的所有神经元(输出)计算的。(致密层的情况)