Tensorflow.js 是一個開源庫,用於在 Javascript 中創建機器學習模型,允許用戶直接在瀏覽器中運行模型。
tf.eye() 是在類 tf.Tensor 中定義的函數。它用於創建指定行和列的單位矩陣。
形狀為 [m, n] 的單位矩陣由所有對角線元素的值 1 和其餘位置的值 0 組成。
用法:
tf.eye(numRows, numColumns, batchShape, dtype)
參數:
- numRows:返回單位矩陣的行數。
- numColumns:返回單位矩陣的列數。這是一個可選參數。如果未指定,則返回單位矩陣的形狀為 [numRows, numRows]。
- batchShape:它是一個數組,通過複製單位矩陣定義將附加到返回單位矩陣形狀開頭的形狀。這是一個可選參數。假設 batchShape = [a, b] 那麽返回的單位矩陣形狀是 [a, b, numRows, numColumns]
- dtype:指定為返回單位矩陣中元素的數據類型。它是可選的。
返回值:它返回單位矩陣的張量。
範例1:創建方形單位矩陣
- 通過在 tf.eye() 方法中定義 numRows = 3,創建形狀為 [3, 3] 的單位矩陣。
Javascript
// Dynamic loading the "@tensorflow/tfjs" module
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
// Creating an identity matrix of shape [3,3]
var matrix = tf.eye(numRows = 3)
// Printing the identity matrix
matrix.print()
輸出:
Tensor [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
範例2:創建矩形的單位矩陣
- 通過在 tf.eye() 方法中定義 numRows = 3 和 numColumns = 4,創建形狀為 [3, 4] 的單位矩陣。
Javascript
// Dynamic loading the "@tensorflow/tfjs" module
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
// Creating an identity matrix of shape [3,4]
var matrix = tf.eye(numRows = 3, numColumns = 4)
// Printing the identity matrix
matrix.print()
輸出:
Tensor [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
範例3:通過指定 batchShape 創建單位矩陣
- 通過在 tf.eye() 方法中定義 numRows = 2 和 numColumns = 3,創建形狀為 [2, 3] 的單位矩陣。
- 複製通過包含 batchShape = [3] 將上述單位矩陣 3 次
- 那麽返回單位矩陣的形狀是 [3, 2, 3]
Javascript
// Dynamic loading the "@tensorflow/tfjs" module
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
// Creating an identity matrix of shape [2,3]
// And include batchShape = [3] to replicate identity matrix 3 times
// Shape of returning tensor is [3, 2, 3]
var matrix = tf.eye(numRows = 2, numColumns = 3, batchShape = [3])
// Printing the identity matrix
matrix.print()
輸出:
Tensor [[[1, 0, 0], [0, 1, 0]], [[1, 0, 0], [0, 1, 0]], [[1, 0, 0], [0, 1, 0]]]
參考:https://js.tensorflow.org/api/latest/#eye
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- Tensorflow.js tf.layers.embedding()用法及代碼示例
- PHP opendir()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
注:本文由純淨天空篩選整理自ManikantaBandla大神的英文原創作品 Tensorflow.js tf.eye() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。