tfjs入门之toxicity
教程:【吴恩达团队Tensorflow2.0数据和部署第一课】基于浏览器的模型与TensorFlow.js
toxicity.html
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.1.0"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity@1.2.0"></script>
<script>
// The minimum prediction confidence.
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 => {
console.log(predictions);
for(i=0;i<7;i++){
if(predictions[i].results[0].match){
console.log(predictions[i].label *" was found with a probability of " * predictions[i].results[0].probabilities[1]);
}
}
});
});
</script>
</head>
<body></body>
</html>
在浏览器打开,按F12
则成功
说一说遇到的坑:
(1)指定tfjs和toxicity版本问题,此处分别为2.1.0,1.2.0,否则可能会遇到找不到函数等各种bug
(2)警告信息可以忽略,或者脚本标签改为
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.1.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity@1.2.0/dist/toxicity.js"></script>
注:具体tfjs、toxicity版本等信息可以参见