Tensorflow.js tf.outerProduct()函数
Tensorflow.js 是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.outerProduct()函数用于查找两个指定向量(即 v1 和 v2)的外积。
句法:
tf.outerProduct(v1, v2)
参数:此函数接受三个参数,如下所示:
- v1:外积函数中的第一个向量,可以是tf.Tensor1D,也可以是TypedArray,也可以是Array。
- v2:它是外积函数中的第二个向量,它的类型可以是 tf.Tensor1D,也可以是 TypedArray,也可以是 Array。
返回值:返回 tf.Tensor2D 对象。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const y = tf.tensor1d([1, 15, 38, 7]);
const z = tf.tensor1d([5, 12, 21, 9]);
// Calling outerProduct() method and
// printing output
tf.outerProduct(y, z).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining float values
var val1 = [0.5, 9.5, .56];
var val2 = [0.51, 1.5, .63];
// Calling outerProduct() method
var res = tf.outerProduct(tf.tensor1d(val1), tf.tensor1d(val2))
// Printing output
res.print();
输出:
Tensor
[[5 , 12 , 21 , 9 ],
[75 , 180, 315, 135],
[190, 456, 798, 342],
[35 , 84 , 147, 63 ]]
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining float values
var val1 = [0.5, 9.5, .56];
var val2 = [0.51, 1.5, .63];
// Calling outerProduct() method
var res = tf.outerProduct(tf.tensor1d(val1), tf.tensor1d(val2))
// Printing output
res.print();
输出:
Tensor
[[0.255 , 0.75 , 0.315 ],
[4.8449998, 14.25, 5.9850001],
[0.2856 , 0.84 , 0.3528 ]]