【问题标题】:How do you implement softmax with Tensorflow.JS如何使用 Tensorflow.JS 实现 softmax
【发布时间】:2020-03-23 19:25:21
【问题描述】:

使用 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的2个值)应该是更好的binaryCrossentropy并将softmax改为sigmoid。

标签: machine-learning deep-learning neural-network tensorflow.js softmax


【解决方案1】:

这是最后一层的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();

对于错误:

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

你可以考虑here给出的答案

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2022-12-22
    相关资源
    最近更新 更多