簡體   English   中英

Tokeniser 從 Keras 到 Tensorflowjs

[英]Tokeniser from Keras to Tensorflowjs

我知道這可能看起來重復,但我無法找到適合我的解決方案。 或者也許我只需要一個完整的例子。

問題是:我想實現一個預測輸入文本 class 的網頁,這要歸功於預訓練的 model。我有 json 文件對應於 tensorflowjs model 和兩者

  • tokeniser.json (由Keras Tokenizer().to_json()保存
  • vocab.json (保存為對應tokenizer.word_index這個問題

現在,我知道如何在 8822996504788 object 中加載 model,使用 tensorflowjs 的異步 function。 我怎樣才能為標記器做同樣的事情? 以及我如何標記輸入文本(在導入的標記器下)?

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

可以在這些鏈接中找到我的 json 文件的示例

我嘗試了以下代碼

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

其中vocabPath是一個包含上面 url 的字符串。

在腳本的末尾,我調用了 function 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

下一步的相關問題是如何對其進行矢量化: Tensorflow.js tokenizer

我最終通過以下方式解決了這個問題,

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM