TensorFlow tf.mul()函数
tf.mul()函数返回两个 tf.Tensor 对象元素的乘积。 tf.Tensor 对象表示数字的多维数组。
句法:
tf.mul( a, b )
参数:
- a:它包含第一个 tf.Tensor 对象乘以第二个 tf.Tensor 对象元素。该参数的值可以是 tf.TensorTypedArray|Array。
- b:它包含乘以第一个 tf.Tensor 对象的第二个 tf.Tensor 对象。该参数的值可以是 (tf.Tensor|TypedArray|Array)。该参数的类型与a的类型相同。
返回值:此函数返回 tf.Tensor 对象。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declare the Tensor array
const arr1 = tf.tensor1d([10, 20, 30, 40, 50]);
const arr2 = tf.tensor1d([5, 10, 15, 20, 25]);
// Use mul() function to multiply
// two Tensor objects
arr1.mul(arr2).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declare the Tensor array
const arr1 = tf.tensor1d([30, 40, 50]);
const arr2 = tf.tensor1d([5, 10, 15, 20, 25]);
// Use mul() function to multiply
// two Tensor objects
arr1.mul(arr2).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declare the Tensor array
const arr = tf.tensor1d([5, 10, 15, 20, 25]);
// Declare a number
const num = tf.scalar(30);
// Use mul() function to multiply
// Tensor object and a number
arr.mul(num).print();
输出:
Tensor
[50, 200, 450, 800, 1250]
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declare the Tensor array
const arr1 = tf.tensor1d([30, 40, 50]);
const arr2 = tf.tensor1d([5, 10, 15, 20, 25]);
// Use mul() function to multiply
// two Tensor objects
arr1.mul(arr2).print();
输出:
An error occured on line: 7
Operands could not be broadcast together with shapes 3 and 5.
示例 3:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Declare the Tensor array
const arr = tf.tensor1d([5, 10, 15, 20, 25]);
// Declare a number
const num = tf.scalar(30);
// Use mul() function to multiply
// Tensor object and a number
arr.mul(num).print();
输出:
Tensor
[150, 300, 450, 600, 750]
参考: https://js.tensorflow.org/api/latest/#mul