javascript - 如何使用 TensorFlow.js 预测有害词
问题描述
使用 TensorFlow.js 预测有害词,请给一个完整的例子......
我已经搜索了很多,但有一些示例仅适用于 python,不适用于 JavaScript
解决方案
有一个毒性模型可以开箱即用地预测一个词或一个短语是否有毒。这是来自文档的示例
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>
推荐阅读
- flutter - 如何在 microsoft graph api 中添加二进制数据以使用上传会话上传文件
- c++ - 检查模板化成员函数是否存在 SFINAE
- php - 为 Json 从 MYSQL 中分离检索到的数据(为 API C# 做准备)
- c# - 在 DotNetCore 3.0 中分页不返回任何结果
- sql - xp_cmdshell 引号和参数大小问题
- javascript - PWA:让 Web Share API 在开发中工作
- rxjs - RXJS:是否有 .pipe(take(1)).toPromise() 的简短等价物?
- c++ - 在 C++ 中验证整数输入
- linux - 如何在fish下使用awk将字符串以三元符号($$$)作为字段分隔符?
- api - 通过 Microsoft 的 Graph API 从 OneDrive 中提取数据需要哪些信息?