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


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

Tensorflow.js是一個開放源代碼庫,由Google開發,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。

.einsum() 函數用於對指定索引和外積進行張量收縮。

用法:

tf.einsum (equation, tensors)

參數:

  • equation:它是第一個輸入張量元素,它是一個描述收縮的字符串,格式與 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();

輸出:

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:在這個例子中,我們講述了像 Batch dot product 這樣的特殊情況。

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




相關用法


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