【问题标题】:Why the accuracy of my model is 0.0? and ETA is always 0 as well为什么我的模型的准确率是 0.0?并且 ETA 也始终为 0
【发布时间】:2021-03-27 17:02:29
【问题描述】:

My accuracy is at 0.0 and I don't know why?

我的模型是一个给定文本序列的超过 1600 个类别的分类问题。为此,我使用了sparseCategoricalCrossentropy 损失函数。这是代码:

import * as tf from '@tensorflow/tfjs-node';
import * as use from '@tensorflow-models/universal-sentence-encoder';

import training from './data/data.json';
import original from './data/original.json'

const classes = ["0", ...original.map(o => o.id)]
const batchSize = 128
const epochs = 600
const embeddingDims = 512;

async function* generator() {
  const m = await use.load()
  for (let i = 0; i < training.length; i += 1) {
    const { intent, text } = training[i]
    yield {
      xs: Array.from((await m.embed(text.toLowerCase())).dataSync()),
      ys: [classes.indexOf(intent)]
    }
  }
}

const dataset = tf.data.generator(generator).batch(batchSize)
const model = tf.sequential();

model.add(tf.layers.dense({
  inputShape: [embeddingDims],
  activation: 'relu',
  units: 64,
}));
model.add(tf.layers.dropout({ rate: 0.25 }));

for (let i = 0; i < 5; i += 1) {
  model.add(tf.layers.dense({
    inputShape: [64],
    activation: 'relu',
    units: 64,
  }));
}
model.add(tf.layers.dropout({ rate: 0.25 }));

model.add(tf.layers.dense({
  inputShape: [64],
  activation: 'relu',
  units: 64,
}));

model.compile({
  loss: 'sparseCategoricalCrossentropy',
  optimizer: 'adam',
  metrics: [tf.metrics.sparseCategoricalAccuracy],
});

// Training
model.fitDataset(dataset, { epochs })
  .then(history => {
    console.log('Trained!!', history)
    model.save('file://./model')
  });

training.length121600,大约是每个类的 76 字符串。

损失函数看起来很小,看起来不错(至少对我来说)。但是精度我不明白为什么是0.00...

模型性能有点慢,每个 epoch 大约需要 1 小时才能完成,因此通过更改超参数进行测试有点乏味......让我们看看是否有人可以帮我看看有什么问题。感谢您的宝贵时间。

【问题讨论】:

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


    【解决方案1】:

    如果您使用 sparseCategoricalCrossentropy,我认为您应该使用 SparseCategoricalAccuracy,但我不知道您的标签是如何编码的 ?

    【讨论】:

    • 我正在使用 universal-sentence-encoder 模型 tfhub.dev/google/universal-sentence-encoder/1 好的,我可以添加 SparseCategoricalAccuracy 指标并查看它的作用。谢谢!
    • 我添加了指标metrics: ['accuracy', tf.metrics.sparseCategoricalAccuracy()]。我收到了这个错误:TypeError: Cannot read property 'rank' of undefined
    • 好的,现在它可以使用 sparseCategoricalAccuracy,我改为 metrics: [tf.metrics.sparseCategoricalAccuracy],。但仍然是 0.00 loss=0.0325 sparseCategoricalAccuracy$1=0.00
    • 我用这个更新了答案+结果的代码
    • 检查你的课程,看看这是否真的是你想要的
    猜你喜欢
    • 1970-01-01
    • 2020-07-29
    • 2014-02-23
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多