當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.encodeString()用法及代碼示例

Tensorflow.js是一個開放源代碼庫,由Google開發,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。

.encodeString()函數用於在給定的編碼方案的幫助下將聲明的字符串編碼為字節。

用法:

tf.encodeString(s, encoding?)

Parameters: 

  • s:是要編碼的指定字符串。它是字符串類型。
  • encoding:這是要使用的編碼方案。並且默認值是utf-8。

返回值:它返回Uint8Array。



範例1:未提供編碼方案時。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.encodeString() method and
// printing output
console.log(tf.util.encodeString("fghi"));

輸出:

102,103,104,105

範例2:提供編碼方案時。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Definging string and encoding 
// scheme
var str = "1234"
var encoding = "utf-8"
  
// Calling tf.encodeString() method and
// printing output
var x = tf.util.encodeString(str, encoding);
console.log(x);

輸出:

49,50,51,52

參考: https://js.tensorflow.org/api/latest/#encodeString

相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.encodeString() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。