Open In App

Tensorflow.js tf.deregisterOp() Function

Last Updated : 06 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Tensorflow.js tf.deregisterOp() function is used to Deregister the Ops(operations) for graph model executor from the TensorFlow. It is behaves opposite to Tensorflow.j tf.registerOp() function.

Syntax:

tf.deregisterOp(name)

 

Parameters: Following are the parameters accepted by the above function and the same are illustrated below:

  • name: This is the string type parameter. This parameter accepts the Tensorflow Op name.

Return Value: This function does not return any value.

Example 1: Example of deregistering a newly registered Op with a checking code.

Javascript




// Importing the tensorflow.js library 
const tf = require("@tensorflow/tfjs");
  
// Try to create a new Op
const customOp = (node) =>
    tf.add(
        node.inputs[0], node.inputs[1]
    );
  
// Try to register a new Op NewOp
tf.registerOp('NewOp', customOp);
  
// Check registerOp NewOp
const x = tf.getRegisteredOp('NewOp');
console.log("Before deregister:", x)
  
// Try to deregister NewOp
tf.deregisterOp('NewOp');
  
// Check deregisterOp NewOp
const y = tf.getRegisteredOp('NewOp');
console.log("After deregister:", y)
  
// Check Op functioning after get deregistered
const a = tf.scalar(1);
const b = tf.tensor1d([1, 2, 3, 4]);
const ans = y.customExecutor({ "inputs": [a, b] });
console.log(ans.print())


Output:

Before deregister: {
  tfOpName: 'NewOp',
  category: 'custom',
  inputs: [],
  attrs: [],
  customExecutor: [Function: customOp]
}
After deregister: undefined

============================
tensorflow1.js:27
const ans = y.customExecutor({"inputs":[a,b]});
              ^

TypeError: Cannot read properties of undefined (reading 'customExecutor')

Example 2: Example of deregistering a newly overridden Op with a checking code.

Javascript




// Importing the tensorflow.js library 
const tf = require("@tensorflow/tfjs");
  
// Try to override add Op with sub op
tf.registerOp(tf.add, tf.sub);
  
const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.scalar(5);
  
// Check overridden Op
const x = tf.getRegisteredOp(tf.add);
console.log("Before deregister:", x)
  
// Check Op functioning before get deregistered
let ans = x.customExecutor(a, b);
console.log(ans.print());
  
// Try to deregister NewOp
tf.deregisterOp(tf.add);
  
// Check deregisterOp NewOp
const y = tf.getRegisteredOp(tf.add);
console.log("\nAfter deregister:", y)
  
// Check Op functioning after get deregistered
ans = y.customExecutor(a, b);
console.log(ans.print());


Output:

Before deregister: {
  tfOpName: [Function: add],
  category: 'custom',
  inputs: [],
  attrs: [],
  customExecutor: [Function: sub]
}
Tensor
    [5, 15, 25, 35]
undefined

After deregister: undefined

============================
tensorflow1.js:26
ans = y.customExecutor(a,b);
        ^

TypeError: Cannot read properties of undefined (reading 'customExecutor')

Example 3: Example of trying to deregister a built-in function with a checking code.

Javascript




// Importing the tensorflow.js library 
const tf = require("@tensorflow/tfjs");
  
const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.scalar(5);
  
// Before get deregistered
console.log("Before deregister:")
let ans = tf.add(a, b);
console.log(ans.print());
  
// Try to deregister build-in
tf.deregisterOp(tf.add);
  
// After get deregistered
console.log("\nAfter deregister:")
ans = tf.add(a, b);
console.log(ans.print());


Output:

Before deregister:
Tensor
    [15, 25, 35, 45]
undefined

After deregister:
Tensor
    [15, 25, 35, 45]
undefined

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads