Tensorflow.js tf.deregisterOp() Function
Last Updated :
06 Oct, 2022
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
const tf = require( "@tensorflow/tfjs" );
const customOp = (node) =>
tf.add(
node.inputs[0], node.inputs[1]
);
tf.registerOp( 'NewOp' , customOp);
const x = tf.getRegisteredOp( 'NewOp' );
console.log( "Before deregister:" , x)
tf.deregisterOp( 'NewOp' );
const y = tf.getRegisteredOp( 'NewOp' );
console.log( "After deregister:" , y)
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
const tf = require( "@tensorflow/tfjs" );
tf.registerOp(tf.add, tf.sub);
const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.scalar(5);
const x = tf.getRegisteredOp(tf.add);
console.log( "Before deregister:" , x)
let ans = x.customExecutor(a, b);
console.log(ans.print());
tf.deregisterOp(tf.add);
const y = tf.getRegisteredOp(tf.add);
console.log( "\nAfter deregister:" , y)
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
const tf = require( "@tensorflow/tfjs" );
const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.scalar(5);
console.log( "Before deregister:" )
let ans = tf.add(a, b);
console.log(ans.print());
tf.deregisterOp(tf.add);
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
Share your thoughts in the comments
Please Login to comment...