Tensorflow.js tf.einsum()函数
Tensorflow.js是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
这 。 einsum()函数用于对指定索引和外积进行张量收缩。
句法 :
tf.einsum (equation, tensors)
参数:
- 方程:它是第一个输入张量元素,是描述收缩的字符串,格式与 numpy.einsum 相同。
- . . .张量:它是第二个输入张量元素,其中输入用于收缩(每个张量),其形状应与方程一致。
限制:
- 它不支持 2 个输入张量。
- 它不支持任何给定输入张量的重复轴。例如,不支持等式“ii→”。
- 不支持 ... 表示法。
返回值:返回 tf.张量。
示例 1:在此示例中,我们讲述的是矩阵乘法等特殊情况。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first tensor input elements
const a = tf.tensor2d([[1, 1, 3], [4, 3, 6]]);
// Defining the second input tensor elements
const b = tf.tensor2d([[1, 1], [2, 3], [4, 5]]);
// Calling the einsum() function and printing outputs
tf.einsum('ij,jk->ik', a, b).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first input elements
const x = tf.tensor1d([1, 1, 3]);
// Defining the second input elements
const y = tf.tensor1d([1, 1, 2]);
// Calling the einsum() function
// and printing outputs
tf.einsum('i,i->', x, y).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first tensor input elements
const x = tf.tensor2d([[1, 3, 3], [4, 5, 4]]);
// Defining the second tensor input elements
const y = tf.tensor2d([[2, 1, 2], [2, 4, 5]]);
// Calling the einsum() function and printing output
tf.einsum('bi,bi->b', x, y).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first tensor input elements
const x = tf.tensor1d([2, 3, 5]);
// Defining the second tensor input elements
const y = tf.tensor1d([2, 5, 6]);
// Calling the einsum() function and printing outputs
tf.einsum('i,j->ij', x, y).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const x = tf.tensor2d([[1, 4], [3, 4]]);
// Calling the einsum() function and
// printing output
tf.einsum('ij->ji', x).print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const x = tf.tensor3d([[[1, 2], [3, 5]], [[-1, -2], [-3, -4]]]);
// Calling the einsum() function and printing output
tf.einsum('bij->bji', x).print();
输出:
Tensor
[[14, 19],
[30, 43]]
示例 2:在此示例中,我们讲述的是点积等特殊情况。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first input elements
const x = tf.tensor1d([1, 1, 3]);
// Defining the second input elements
const y = tf.tensor1d([1, 1, 2]);
// Calling the einsum() function
// and printing outputs
tf.einsum('i,i->', x, y).print();
输出:
Tensor
8
示例 3:在此示例中,我们讲述的是批量点积等特殊情况。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first tensor input elements
const x = tf.tensor2d([[1, 3, 3], [4, 5, 4]]);
// Defining the second tensor input elements
const y = tf.tensor2d([[2, 1, 2], [2, 4, 5]]);
// Calling the einsum() function and printing output
tf.einsum('bi,bi->b', x, y).print();
输出:
Tensor
[11, 48]
例 4:在这个例子中,我们讲述的是像外积这样的特殊情况。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining the first tensor input elements
const x = tf.tensor1d([2, 3, 5]);
// Defining the second tensor input elements
const y = tf.tensor1d([2, 5, 6]);
// Calling the einsum() function and printing outputs
tf.einsum('i,j->ij', x, y).print();
输出:
Tensor
[[4 , 10, 12],
[6 , 15, 18],
[10, 25, 30]]
示例 5:在此示例中,我们将讲述矩阵转置等特殊情况。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const x = tf.tensor2d([[1, 4], [3, 4]]);
// Calling the einsum() function and
// printing output
tf.einsum('ij->ji', x).print();
输出:
Tensor
[[1, 3],
[4, 4]]
示例 6:在此示例中,我们将讲述诸如批量矩阵转置之类的特殊情况。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining tensor input elements
const x = tf.tensor3d([[[1, 2], [3, 5]], [[-1, -2], [-3, -4]]]);
// Calling the einsum() function and printing output
tf.einsum('bij->bji', x).print();
输出:
Tensor
[[[1 , 3 ],
[2 , 5 ]],
[[-1, -3],
[-2, -4]]]
参考: https://js.tensorflow.org/api/latest/#einsum