Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

tensorflow.js - 如何检查tfjs模型是否正确加载到浏览器(How to check if tfjs model is loaded to browser properly)

im trying to do text classification, loaded the model back to browser by

(我试图进行文本分类,通过以下方式将模型加载回浏览器)

async function loadFile(){    const jsonUpload = document.getElementById('json-upload');    
model = await tf.loadLayersModel(tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]));
model.summary();

have complete summary in console

(在控制台中有完整的摘要)

Layer (type)                 Output shape              Param #   
    tfjs@latest:2 =================================================================
    tfjs@latest:2 embedding_Embedding1 (Embedd [null,15,50]              1009200   
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 conv1d_Conv1D1 (Conv1D)      [null,15,100]             15100     
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 max_pooling1d_MaxPooling1D1  [null,7,100]              0         
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 conv1d_Conv1D2 (Conv1D)      [null,7,100]              40100     
    tfjs@latest:2 _________________________________________________________________
    tfjs@latest:2 max_pooling1d_MaxPooling1D2  [null,3,100]              0         
    tfjs@latest:2 _________________________________________________________________
....
...
..
dense_Dense26 (Dense)        [null,2]                  42        
tfjs@latest:2 =================================================================
tfjs@latest:2 Total params: 1702322
tfjs@latest:2 Trainable params: 1702322
tfjs@latest:2 Non-trainable params: 0
tfjs@latest:2 
  1. is there any other way we can check if the model is loaded properly?

    (还有什么其他方法可以检查模型是否正确加载?)

  ask by TKK translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tf.loadLayersModel will return a model only if the model is successfully loaded.

(tf.loadLayersModel仅在成功加载模型后才返回模型。)

Otherwise an error is thown and need to be caught.

(否则,将引发错误并需要捕获。)

Using an if statement will fail in case the model could not be loaded.

(if无法加载模型, if使用if语句将失败。)

Here is how you can check if the model is loaded successfully.

(这是检查模型是否成功加载的方法。)

try {
    model = await tf.loadLayersModel(tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]))
} catch(e) {
   console.log("the model could not be loaded")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...