選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TensorFlow.js 教程


TensorFlow

什麼是 TensorFlow.js?

Tensorflow 是一個流行的 JavaScript 庫,用於 機器學習

Tensorflow 允許我們在 瀏覽器 中訓練和部署機器學習。

Tensorflow 允許我們將機器學習功能新增到任何 Web 應用程式

使用 TensorFlow

要使用 TensorFlow.js,請將以下指令碼標籤新增到您的 HTML 檔案中

示例

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.6.0/dist/tf.min.js"></script>

如果您想始終使用最新版本,請刪除版本號

示例 2

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

TensorFlow 由 Google Brain Team 開發,用於內部 Google 使用,並於 2015 年釋出為開源軟體。

2019 年 1 月,Google 開發者釋出了 TensorFlow.js,這是 TensorFlow 的 JavaScript 實現

Tensorflow.js 的設計旨在提供與用 Python 編寫的原始 TensorFlow 庫相同的功能。


張量

TensorFlow.js 是一個用於定義和操作 張量JavaScript 庫。

TensorFlow.js 中的主要資料型別是 Tensor

Tensor 與多維陣列非常相似。

Tensor 包含一個或多個維度中的值

Tensor

Tensor 具有以下主要屬性

屬性描述
dtype資料型別
rank維度數量
shape每個維度的尺寸

在機器學習中,“維度”有時與“”互換使用。

[10, 5] 是一個 2 維張量或 2 階張量。

此外,“維度”一詞可以指代一維的大小。

示例:在 2 維張量 [10, 5] 中,第一個維度的維度是 10。



建立張量

TensorFlow 中的主要資料型別是 Tensor

Tensor 可以使用 tf.tensor() 方法從任何 N 維陣列建立

示例 1

const myArr = [[1, 2, 3, 4]];
const tensorA = tf.tensor(myArr);

自己動手試一試 »

示例 2

const myArr = [[1, 2], [3, 4]];
const tensorA = tf.tensor(myArr);

自己動手試一試 »

示例 3

const myArr = [[1, 2], [3, 4], [5, 6]];
const tensorA = tf.tensor(myArr);

自己動手試一試 »


張量形狀

Tensor 也可以從 陣列shape 引數建立

示例 1

const myArr = [1, 2, 3, 4]
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

自己動手試一試 »

示例 2

const tensorA = tf.tensor([1, 2, 3, 4], [2, 2]);

自己動手試一試 »

示例 3

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

自己動手試一試 »


檢索張量值

您可以使用 tensor.data() 獲取張量背後的 資料

示例

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
tensorA.data().then(data => display(data));

function display(data) {
  document.getElementById("demo").innerHTML = data;
}

自己動手試一試 »

您可以使用 tensor.array() 獲取張量背後的 陣列

示例

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
tensorA.array().then(array => display(array[0]));

function display(data) {
  document.getElementById("demo").innerHTML = data;
}

自己動手試一試 »

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
tensorA.array().then(array => display(array[1]));

function display(data) {
  document.getElementById("demo").innerHTML = data;
}

自己動手試一試 »

您可以使用 tensor.rank 獲取張量的

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

document.getElementById("demo").innerHTML = tensorA.rank;

自己動手試一試 »

您可以使用 tensor.shape 獲取張量的 形狀

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

document.getElementById("demo").innerHTML = tensorA.shape;

自己動手試一試 »

您可以使用 tensor.dtype 獲取張量的 資料型別

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

document.getElementById("demo").innerHTML = tensorA.dtype;

自己動手試一試 »


張量資料型別

Tensor 可以具有以下資料型別

  • bool
  • int32
  • float32(預設)
  • complex64
  • string

建立張量時,可以將資料型別指定為第三個引數

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape, "int32");

自己動手試一試 »


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援