你如何用 Tensorflow.JS 实现 softmax

How do you implement softmax with Tensorflow.JS

使用 Tensorflow.JS,我试图获得一个机器学习模型 运行 使用 softmax 激活函数的最后一个密集层。当我尝试 运行 它时,我收到:

检查目标时出错:预期 dense_Dense2 的形状为 [1],但得到的数组的形状为 [3,2]。

如果我注释掉 fit 函数并 运行 它,我会得到一个 1x2 数组(正如预期的那样,因为我在最后一层有 2 个单元并且我只进入一个测试。

此外,当我将 y 变量更改为此数组时:[[1,2,3]],它训练(但我认为这是不正确的,因为 ys 不是最后一个的正确形状层 (2).

任何建议或帮助将不胜感激,以填补我的知识空白。

var tf = require('@tensorflow/tfjs');

let xs = tf.tensor([
  [1,2],
  [2,1],
  [0,0]
]);

let ys = tf.tensor([
  [1,2],
  [2,1],
  [1,1]
]);

async function createModel () {


  const model = tf.sequential();
  model.add(tf.layers.dense({inputShape: [2], units: 32, activation: "relu"}));
  model.add(tf.layers.dense({units: 2}));
  model.compile({loss: "sparseCategoricalCrossentropy",optimizer:"adam"});

  //await model.fit(xs, ys, {epochs:1000});

  model.predict(tf.tensor([[1,2]])).print();
}

createModel();

这是最后一层的softmax激活:

  const model = tf.sequential();
  model.add(tf.layers.dense({inputShape: [2], units: 32, activation: "relu"}));
  model.add(tf.layers.dense({units: 2, activation:'softmax'}));
  model.compile({loss: "sparseCategoricalCrossentropy",optimizer:"adam"});

  model.predict(tf.tensor([[1,2]])).print();

对于错误:

Error when checking target: expected dense_Dense2 to have shape [,1], but got array with shape [3,2].

可以考虑给出的答案

您的错误与期望标签为 tensor1d 的损失函数 sparseCategoricalCrossentropy 有关。如果将此损失函数更改为categoricalCrossentropy,它将起作用。两种损失都做同样的事情,不同的是标签的编码方式。但是正如问题中的那样,标签既没有为 categoricalCrossentropy 也没有为 sparseCategoricalCrossentropy 编码。

  • 使用sparseCategoricalCrossentropy时,标签是一维张量,其中值是类别的索引
  • 使用 categoricalCrossentropy 时,标签是二维张量,也就是单热编码