Tensorflow.js是Google开发的开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。
Tensorflow.js tf.registerOp() 函数用于在 TensorFlow 中为图模型执行器注册 Ops(操作)。使用此函数,用户不仅可以在 Tensorflow 中创建自定义 Ops,还可以覆盖现有 Ops
用法:
tf.registerOp (name, opFunc)
Parameters: 以下是上述函数接受的参数,如下所示:
- name: 这是字符串类型参数。该参数代表 Tensorflow Op 名称。
- opFunc: 这是对象类型参数。该参数接受 op 函数。在执行期间必须使用当前图节点(包含 attr 和输入)调用该函数,并且必须返回张量或张量列表。
返回值:它返回无效。
笔记:
- 节点对象的输入和属性基于 TensorFlow op 注册表。
- 要检查 NewOp Op 是否已注册,请使用 Tensorflow.js tf.getRegisteredOp() 函数。
示例 1:使用校验码注册新 Op NewOp 以及新 Op 函数的示例。
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
const x = tf.registerOp('NewOp', customOp);
// Check code and new op functioning
const a = tf.scalar(1);
const b = tf.tensor1d([1, 2, 3, 4]);
const name = tf.getRegisteredOp('NewOp');
const ans = name.customExecutor({"inputs":[a,b]});
console.log(ans.print())输出:
Tensor
[2, 3, 4, 5]
undefined
示例 2:使用检查代码覆盖现有 Op 的示例以及覆盖 Op 的函数。
Javascript
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
// Try to override add Op with sub op
const x = tf.registerOp(tf.add, tf.sub);
// Check code and override op functioning
const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.scalar(5);
const ans = tf.add(a,b);
console.log(ans.print())输出:
Tensor
[15, 25, 35, 45]
undefined
参考:https://js.tensorflow.org/api/latest/#registerOp
相关用法
- Tensorflow.js tf.registerBackend()用法及代码示例
- Tensorflow.js tf.regularizers.l2()用法及代码示例
- Tensorflow.js tf.regularizers.l1()用法及代码示例
- Tensorflow.js tf.regularizers.l1l2()用法及代码示例
- Tensorflow.js tf.reciprocal()用法及代码示例
- Tensorflow.js tf.reverse()用法及代码示例
- Tensorflow.js tf.real()用法及代码示例
- Tensorflow.js tf.removeBackend()用法及代码示例
- Tensorflow.js tf.relu()用法及代码示例
- Tensorflow.js tf.relu6()用法及代码示例
- Tensorflow.js tf.reshape()用法及代码示例
- Tensorflow.js tf.ready()用法及代码示例
- Tensorflow.js tf.range()用法及代码示例
- Tensorflow.js tf.round()用法及代码示例
- Tensorflow.js tf.rsqrt()用法及代码示例
- Tensorflow.js tf.randomGamma()用法及代码示例
- Tensorflow.js tf.randomNormal()用法及代码示例
- Tensorflow.js tf.randomUniform()用法及代码示例
- Tensorflow.js tf.depthToSpace()用法及代码示例
- Tensorflow.js tf.abs()用法及代码示例
- Tensorflow.js tf.acos()用法及代码示例
- Tensorflow.js tf.acosh()用法及代码示例
- Tensorflow.js tf.asin()用法及代码示例
- Tensorflow.js tf.asinh()用法及代码示例
- Tensorflow.js tf.atan()用法及代码示例
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Tensorflow.js tf.registerOp() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
