当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Tensorflow.js tf.div()用法及代码示例

Tensorflow.js是Google开发的开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。

tf.div()函数用于按元素划分两个指定的张量。它支持广播。

用法:

tf.div (a, b)

参数:此函数接受两个参数,如下所示:

  • a:第一个输入张量作为分子。
  • b:第二个输入张量作为分母。它必须具有与“a”相同的数据类型。

返回值:它为a /b的结果返回一个Tensor,其中a是第一个Tensor,b是第二个Tensor。



范例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two Tensors
const a = tf.tensor1d([3, 5, 10, 15]);
const b = tf.tensor1d([2, 5, 2, 7]);
  
// Calling the .div() function over the 
// above Tensors as its parameters
a.div(b).print();

输出:

Tensor
   [1.5, 1, 5, 2.1428571]

范例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Broadcasting the div a with b
const a = tf.tensor1d([1, 12, 17, 20]);
const b = tf.scalar(3);
  
// Calling the .div() function over the 
// above Tensors as its parameters
a.div(b).print();

输出:

Tensor
   [0.3333333, 3.9999998, 5.6666665, 6.666666]

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Tensorflow.js tf.div() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。