Tensorflow.js tf.Environment Class
Last Updated :
29 Aug, 2021
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
import * as tf from "@tensorflow/tfjs"
var x = tf.tensor([1, 2, 3, 4]);
tf.disposeVariables();
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
import * as tf from "@tensorflow/tfjs"
await tf.enableDebugMode();
tf.env().set( 'PROD' , false );
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
import * as tf from "@tensorflow/tfjs"
await tf.enableProdMode();
tf.env().set( 'DEBUG' , false );
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
import * as tf from "@tensorflow/tfjs"
tf.engine().startScope();
const res = tf.ones([200, 250]);
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
import * as tf from "@tensorflow/tfjs"
const res = tf.env().getBool( 'WEBGL_RENDER_FLOAT32_ENABLED' );
console.log(res);
|
Output:
true
Reference: https://js.tensorflow.org/api/latest/#class:Environment
Share your thoughts in the comments
Please Login to comment...