當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.transpose()用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。

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();

輸出:

 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

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Tensorflow.js tf.transpose() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。