首页 > 解决方案 > 如何使用 TensorFlow.js 预测有害词

问题描述

使用 TensorFlow.js 预测有害词,请给一个完整的例子......

我已经搜索了很多,但有一些示例仅适用于 python,不适用于 JavaScript

标签: javascriptpredictionwordtensorflow.js

解决方案


有一个毒性模型可以开箱即用地预测一个词或一个短语是否有毒。这是来自文档的示例

const threshold = 0.9;
 
// Load the model. Users optionally pass in a threshold and an array of
// labels to include.
toxicity.load(threshold).then(model => {
  const sentences = ['you suck'];
 
  model.classify(sentences).then(predictions => {
    // `predictions` is an array of objects, one for each prediction head,
    // that contains the raw probabilities for each input along with the
    // final prediction in `match` (either `true` or `false`).
    // If neither prediction exceeds the threshold, `match` is `null`.
 
    console.log(predictions);
    /*
    prints:
    {
      "label": "identity_attack",
      "results": [{
        "probabilities": [0.9659664034843445, 0.03403361141681671],
        "match": false
      }]
    },
    {
      "label": "insult",
      "results": [{
        "probabilities": [0.08124706149101257, 0.9187529683113098],
        "match": true
      }]
    },
    ...
     */
  });
});
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"></script>


推荐阅读