目录
使用通用语句编码器设置TensorFlow.js代码
TriviaQA数据集
通用句子编码器
聊天机器人在行动
终点线
下一步是什么?
- 下载项目代码-9.9 MB
TensorFlow + JavaScript。现在,最流行,最先进的AI框架支持地球上使用最广泛的编程语言。因此,让我们在Web浏览器中通过深度学习使文本和NLP(自然语言处理)聊天机器人神奇地发生,使用TensorFlow.js通过WebGL加速GPU!

我们的聊天专家聊天机器人的第1版使用递归神经网络(RNN)构建,存在一些缺点和局限性,这使得它常常无法预测匹配的聊天问题以提供答案,除非问题被逐字询问出现在数据库中。RNN学会根据序列进行预测,但他们不一定知道序列的哪些部分最重要。
这是转换器可以派上用场的地方。我们在上一篇文章中讨论了转换器。在那里,我们展示了他们如何帮助改善我们的情绪探测器。现在,让我们看看他们可以为聊天聊天机器人做什么。
使用通用语句编码器设置TensorFlow.js代码
该项目与第一个聊天专家代码非常相似,因此让我们以初始代码库为起点,去掉单词嵌入、模型和预测部分。我们将在此处添加一个重要且功能强大的库,即通用句子编码器(USE),它是一种经过预先训练的基于转换器的语言处理模型。这就是我们用来确定聊天机器人匹配的聊天问题的内容。我们还将在USE自述文件示例中添加两个实用程序函数dotProduct和zipWith,以帮助我们确定句子的相似性。
<html>
<head>
<title>Trivia Know-It-All: Chatbots in the Browser with TensorFlow.js</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
</head>
<body>
<h1 id="status">Trivia Know-It-All Bot</h1>
<label>Ask a trivia question:</label>
<input id="question" type="text" />
<button id="submit">Submit</button>
<p id="bot-question"></p>
<p id="bot-answer"></p>
<script>
function setText( text ) {
document.getElementById( "status" ).innerText = text;
}
// Calculate the dot product of two vector arrays.
const dotProduct = (xs, ys) => {
const sum = xs => xs ? xs.reduce((a, b) => a + b, 0) : undefined;
return xs.length === ys.length ?
sum(zipWith((a, b) => a * b, xs, ys))
: undefined;
}
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith =
(f, xs, ys) => {
const ny = ys.length;
return (xs.length <= ny ? xs : xs.slice(0, ny))
.map((x, i) => f(x, ys[i]));
}
(async () => {
// Load TriviaQA data
let triviaData = await fetch( "web/verified-wikipedia-dev.json" ).then( r => r.json() );
let data = triviaData.Data;
// Process all QA to map to answers
let questions = data.map( qa => qa.Question );
// Load the universal sentence encoder
setText( "Loading USE..." );
let encoder = await use.load();
setText( "Loaded!" );
const model = await use.loadQnA();
document.getElementById( "question" ).addEventListener( "keyup", function( event ) {
// Number 13 is the "Enter" key on the keyboard
if( event.keyCode === 13 ) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
document.getElementById( "submit" ).click();
}
});
document.getElementById( "submit" ).addEventListener( "click", async function( event ) {
let text = document.getElementById( "question" ).value;
document.getElementById( "question" ).value = "";
// Run the calculation things
const input = {
queries: [ text ],
responses: questions
};
// console.log( input );
let embeddings = await model.embed( input );
tf.tidy( () => {
const embed_query = embeddings[ "queryEmbedding" ].arraySync();
const embed_responses = embeddings[ "responseEmbedding" ].arraySync();
let scores = [];
embed_responses.forEach( response => {
scores.push( dotProduct( embed_query[ 0 ], response ) );
});
// Get the index of the highest value in the prediction
let id = scores.indexOf( Math.max( ...scores ) );
document.getElementById( "bot-question" ).innerText = questions[ id ];
document.getElementById( "bot-answer" ).innerText = data[ id ].Answer.Value;
});
embeddings.queryEmbedding.dispose();
embeddings.responseEmbedding.dispose();
});
})();
</script>
</body>
</html>
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/19010.html