Dac*_*ble 5 javascript machine-learning tensorflow.js natural-language-processing
我是机器学习和Tensorflow的新手,因为我不知道python所以我决定使用那里的javascript版本(可能更像是一个包装器).
该问题是我试图建立一个处理自然语言的模型.因此,第一步是将文本标记化,以便将数据提供给模型.我做了很多研究,但大多数都使用的是使用方法的张量流的python版本:tf.keras.preprocessing.text.Tokenizer我在tensorflow.js中找不到类似的东西.我陷入了这一步,不知道如何将文本传输到可以提供给模型的矢量.请帮忙 :)
要将文本转换为向量,有很多方法可以做到,所有这些都取决于用例.最直观的一个是使用术语频率的那个,即,给定语料库的词汇(所有可能的词),所有文本文档将被表示为向量,其中每个条目表示文本文档中词的出现.
有了这个词汇:
["machine", "learning", "is", "a", "new", "field", "in", "computer", "science"]
Run Code Online (Sandbox Code Playgroud)
以下文字:
["machine", "is", "a", "field", "machine", "is", "is"]
Run Code Online (Sandbox Code Playgroud)
将被转换为此向量:
[2, 0, 3, 1, 0, 1, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)
这种技术的缺点之一是矢量中可能有大量的0,其大小与语料库的词汇相同.这就是为什么还有其他技术.然而,经常提到一袋词.并且使用tf.idf有一个略有不同的版本
const vocabulary = ["machine", "learning", "is", "a", "new", "field", "in", "computer", "science"]
const text = ["machine", "is", "a", "field", "machine", "is", "is"]
const parse = (t) => vocabulary.map((w, i) => t.reduce((a, b) => b === w ? ++a : a , 0))
console.log(parse(text))Run Code Online (Sandbox Code Playgroud)
还有以下模块可能有助于实现您的目标
好吧,我遇到了这个问题并按照以下步骤处理了它:
tokenizer.fit_on_texts([data])打印后tokenizer.word_index。
function getTokenisedWord(seedWord) {
const _token = word2index[seedWord.toLowerCase()]
return tf.tensor1d([_token])
}
const seedWordToken = getTokenisedWord('Hello');
model.predict(seedWordToken).data().then(predictions => {
const resultIdx = tf.argMax(predictions).dataSync()[0];
console.log('Predicted Word ::', index2word[resultIdx]);
})
index2word是word2indexjson对象的反向映射。