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


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

Tensorflow.js 是一個由 Google 開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。它還可以幫助開發人員使用 JavaScript 語言開發 ML 模型,並且可以直接在瀏覽器或 Node.js 中使用 ML。

.linalg.qr() 函數用於計算參考 m × n 矩陣應用 Householder 變換的 QR 分解。

用法:

tf.linalg.qr(x, fullMatrices?)

Parameters: 

  • x:規定的 tf.Tensor 是 QR-decomposed。它的秩必須大於或等於 2。假設它的形狀為 [..., M, N]。它是 tf.Tensor 類型。
  • fullMatrices:它是一個可選參數,是布爾類型,默認值為 false。如果它是真的,那麽它評估 normal-sized Q 否則它隻評估 Q 和 R 的最高 N 列。

返回值:它返回 [tf.Tensor, tf.Tensor]。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining a 2d tensor
const tn = tf.tensor2d([[3, 5], [7, 2]]);
  
// Calling linalg.qr() function
let [Q, R] = tf.linalg.qr(tn);
  
// Printing outputs
console.log('q');
Q.print();
console.log('r');
R.print();

輸出:

q
Tensor
    [[-0.3939192, 0.919145  ],
     [-0.919145 , -0.3939193]]
r
Tensor
    [[-7.6157722, -3.8078861],
     [0         , 3.8078861 ]]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining a 2d tensor
const tn = tf.tensor2d([[3, 5], [7, 2]]);
  
// Calling linalg.qr() function
let [Q, R] = tf.linalg.qr(tn, true);
  
// Printing outputs
console.log('Orthogonalized:');
Q.transpose().print();
console.log('Regenerated:');
R.dot(Q).print();

輸出:

Orthogonalized:
Tensor
    [[-0.3939192, -0.919145 ],
     [0.919145  , -0.3939193]]
Regenerated:
Tensor
    [[6.4999986 , -5.499999 ],
     [-3.4999995, -1.4999998]]

參考: https://js.tensorflow.org/api/latest/#linalg.qr




相關用法


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