Friday, November 22, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.Environment Class

Tensorflow.js tf.Environment Class

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. The tf.Environment() class includes assessed flags and the registered platform. It is every time utilized like a global singleton and can be restored from tf.env() function.

This Environment class contains five inbuilt functions which are illustrated below:  

The tf.Environment class .disposeVariables() function is used to dispose of every single variable stored in the backend engine.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Declaring a variable
var x = tf.tensor([1, 2, 3, 4]);
 
// Calling disposeVariables() method
tf.disposeVariables();
 
// Printing output
console.log("Variables disposed.")


Output:

Variables disposed.

 The tf.Environment class .enableDebugMode() function is used to enable the debug mode that would register data regarding every single executed kernel i.e. the runout time of the kernel implementation, including the rank, size, as well as the shape of the resultant tensor. 

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling enableDebugMode() method
await tf.enableDebugMode();
 
// Setting prod mode of the
// environment
tf.env().set('PROD', false);
 
// Printing output
console.log(tf.env().flags);


Output:

{
  "IS_BROWSER": true,
  "IS_NODE": false,
  "DEBUG": true,
  "CPU_HANDOFF_SIZE_THRESHOLD": 128,
  "PROD": false
}

The tf.Environment class .enableProdMode() function is used to enable the mode of production that deactivates the exactness restraints in support of the production.

Example 3:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling enableProdMode() method
await tf.enableProdMode();
 
// Setting debug mode of the
// environment
tf.env().set('DEBUG', false);
 
// Printing output
console.log(tf.env().flags);


Output:

{
  "IS_BROWSER": true,
  "IS_NODE": false,
  "DEBUG": false,
  "CPU_HANDOFF_SIZE_THRESHOLD": 128,
  "PROD": true,
  "WEBGL_VERSION": 2,
  "HAS_WEBGL": true,
  "WEBGL_CHECK_NUMERICAL_PROBLEMS": false,
  "IS_TEST": false,
  "WEBGL_CPU_FORWARD": true,
  "WEBGL_MAX_TEXTURE_SIZE": 16384,
  "WEBGL_FORCE_F16_TEXTURES": true,
  "WEBGL_RENDER_FLOAT32_CAPABLE": true,
  "WEBGL_RENDER_FLOAT32_ENABLED": true,
  "WEBGL_FLUSH_THRESHOLD": -1,
  "WEBGL_PACK": true,
  "WEBGL_LAZILY_UNPACK": true,
  "WEBGL_DELETE_TEXTURE_THRESHOLD": -1,
  "WEBGL_PACK_BINARY_OPERATIONS": true,
  "WEBGL_USE_SHAPES_UNIFORMS": false,
  "WEBGL_PACK_UNARY_OPERATIONS": true,
  "WEBGL_DOWNLOAD_FLOAT_ENABLED": true,
  "WEBGL_CONV_IM2COL": true,
  "WEBGL_PACK_DEPTHWISECONV": true,
  "WEBGL_MAX_TEXTURES_IN_SHADER": 16,
  "WEBGL_PACK_ARRAY_OPERATIONS": true
}

 The tf.Environment class .engine() function is used to return the global engine which saves the path of every single tensor as well as backends.

Example 4:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling engine() and startScope()
// method
tf.engine().startScope(); 
 
// Calling ones() method
const res = tf.ones([200, 250]);
 
// Printing output
console.log(res);


Output:

Tensor
    [[1, 1, 1, ..., 1, 1, 1],
     [1, 1, 1, ..., 1, 1, 1],
     [1, 1, 1, ..., 1, 1, 1],
     ...,
     [1, 1, 1, ..., 1, 1, 1],
     [1, 1, 1, ..., 1, 1, 1],
     [1, 1, 1, ..., 1, 1, 1]]

 The tf.Environment class .env() function is used to return the present environment i.e. a global entity. Moreover, the environment object includes the assessed attribute values along with the dynamic platform. 

Example 5:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling env() and getBool() method
// along with its parameter
const res = tf.env().getBool('WEBGL_RENDER_FLOAT32_ENABLED');
 
// Printing output
console.log(res);


Output:

true

Reference: https://js.tensorflow.org/api/latest/#class:Environment

RELATED ARTICLES

Most Popular

Recent Comments