Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.encodeString() Function

Tensorflow.js tf.encodeString() Function

Tensorflow.js is an open-source library which is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .encodeString() function is used to encode the stated string into bytes with the help of the given encoding scheme.

Syntax :  

tf.encodeString(s, encoding?)

Parameters:  

  • s: It is the stated string which is to be encoded. It is of type string.
  • encoding: It is the encoding scheme which is to be used. And the by default value is utf-8.

Return Value: It returns Uint8Array.

Example 1: When encoding scheme is not provided.

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"));


 Output:

102,103,104,105

Example 2: When encoding scheme is provided.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Defining 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);


Output: 

49,50,51,52

 Reference: https://js.tensorflow.org/api/latest/#encodeString

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments