📜  Tensorflow.js tf.transpose()函数

📅  最后修改于: 2022-05-13 01:56:39.253000             🧑  作者: Mango

Tensorflow.js tf.transpose()函数

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

tf.transpose()函数用于对指定的二维输入张量执行常规矩阵转置操作。

句法:

tf.transpose (x)

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

  • x:要转置的指定输入张量。

返回值:它返回一个张量作为转置操作的输出。

示例 1:

Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of matrix along with
// its dimension
const a = tf.tensor2d([2, 4, 6, 8], [2, 2]);
  
// Calling the .transpose() function over the
// above tensor as its parameter
a.transpose().print();


Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using some tensor of matrix along with its dimensions
// as the parameter for .transpose() functions
tf.tensor2d([1, 3, 5, 7, 9, 11], [2, 3]).transpose().print(); 
tf.tensor2d([1, 3, 5, 7, 9, 11], [3, 2]).transpose().print();


输出:

Tensor
   [[2, 6],
    [4, 8]]

示例 2:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using some tensor of matrix along with its dimensions
// as the parameter for .transpose() functions
tf.tensor2d([1, 3, 5, 7, 9, 11], [2, 3]).transpose().print(); 
tf.tensor2d([1, 3, 5, 7, 9, 11], [3, 2]).transpose().print();

输出:

Tensor
   [[1, 7 ],
    [3, 9 ],
    [5, 11]]
Tensor
   [[1, 5, 9 ],
    [3, 7, 11]]

参考: https://js.tensorflow.org/api/latest/#transpose