从 Keras 到 Tensorflow Js 的分词器

Tokeniser from Keras to Tensorflowjs

我知道这可能看起来重复,但我找不到适合我的解决方案。或者也许我只需要一个完整的例子。

问题是:我想实现一个预测输入文本 class 的网页,这要归功于预训练模型。 我有对应于 tensorflowjs 模型的 json 文件和

现在,我知道如何使用 tensorflowjs 的异步功能将模型加载到 javascript 对象中。 我怎样才能为标记器做同样的事情? 以及我如何标记输入文本(在导入的标记器下)?

======================= 澄清 =========== ================

我的 json 文件的示例可以在 these links

找到

我试过下面的代码

// loadVocab function to get the vocabulary from json.
async function loadVocab() {
  var word2index = await JSON.parse(await JSON.stringify(vocabPath));
  return word2index;
}

其中 vocabPath 是包含上述 url 的字符串。

在脚本的末尾,我调用了一个函数 init()

async function init(){
    model = await loadModel();
    word2index = await loadVocab();
    console.log(word2index["the"]); // I expect 1
}

但我当然得到了 undefined,因为我猜它将 url 的真实字符串作为 json,而不是 url 处的 json =].

有什么想法吗?

像这样加载从 python 保存的词汇表:

import json 
with open( 'word_dict.json' , 'w' ) as file:    
    json.dump( tokenizer.word_index , file )

您必须使用这样的 AJAX 调用加载 JSON:

function getJSON(url) {
    var resp ;
    var xmlHttp ;

    resp  = '' ;
    xmlHttp = new XMLHttpRequest();

    if(xmlHttp != null)
    {
        xmlHttp.open( "GET", url, false );
        xmlHttp.send( null );
        resp = xmlHttp.responseText;
    }

    return resp ;
}

var vocab = JSON.parse(getJSON('./word_dict.json'));

python 方面在这里有很好的解释:Converting Python Keras NLP Model to Tensorflowjs

下一步的相关问题,如何对其进行矢量化:

我最终通过以下方式解决了问题,

let vocabPath = '/url/to/my/vocab.json';

async function loadVocab() {
    let vocab = await (await fetch(vocabPath)).json();
    return vocab;
}