New to machine learning? Watch a video course to get practical working knowledge of ML using web technologies
View series
開始使用
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
TensorFlow.js 是 JavaScript 程式庫,可用於在瀏覽器和 Node.js 中訓練和部署機器學習模型。
請參閱下列章節,瞭解各種入門方式。
無須直接處理張量,即可編寫機器學習程式
想要開始使用機器學習,並且略過張量或最佳化器等低階層的瑣碎項目嗎?
ml5.js 程式庫是根據 TensorFlow.js 建構而成,可讓您使用簡單好上手的 API,在瀏覽器中存取機器學習演算法和模型。
瞭解 ml5.js
開始設定 TensorFlow.js
熟悉張量、層、最佳化器和損失函式等概念,或是想要瞭解這些概念嗎?TensorFlow.js 提供了使用 JavaScript 進行類神經網路程式設計的彈性構成要素。
瞭解如何在瀏覽器或 Node.js 中,使用 TensorFlow.js 程式碼快速設定和執行。
進行設定
將預先訓練的模型轉換為 TensorFlow.js
瞭解如何將預先訓練的模型從 Python 轉換為 TensorFlow.js
Keras 模型
GraphDef 模型
透過現有的 TensorFlow.js 程式碼學習
tfjs-examples 提供了小型的程式碼範例,這些範例使用 TensorFlow.js 來實作各種機器學習工作。
前往 GitHub 查看
以視覺化的方式呈現 TensorFlow.js 模型的行為
tfjs-vis 是用於在瀏覽器中視覺化呈現資料的小型程式庫,旨在搭配 TensorFlow.js 使用。
前往 GitHub 查看
查看示範
使用 TensorFlow.js 備妥資料以進行處理
TensorFlow.js 支援使用機器學習的最佳做法來處理資料。
查看文件
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2021-09-01 (世界標準時間)。
[null,null,["上次更新時間:2021-09-01 (世界標準時間)。"],[],[],null,["# Get started with TensorFlow.js\n\nTensorFlow.js is a JavaScript library for training and deploying machine\nlearning models in the web browser and in Node.js. This tutorial shows you how\nto get started with TensorFlow.js by training a minimal model in the browser and\nusing the model to make a prediction.\n\nThe example code is available on\n[GitHub](https://github.com/tensorflow/tfjs-examples/tree/master/getting-started).\n\nPrerequisites\n-------------\n\nTo complete this tutorial, you need the following installed in your development\nenvironment:\n\n- Node.js ([download](https://nodejs.org/en/download/))\n- Yarn ([install](https://yarnpkg.com/en/docs/install))\n\nInstall the example\n-------------------\n\nGet the source code and install dependencies:\n\n1. Clone or download the [tfjs-examples](https://github.com/tensorflow/tfjs-examples) repository.\n2. Change into the `getting-started` directory: `cd tfjs-examples/getting-started`.\n3. Install dependencies: `yarn install`.\n\nIf you look at the `package.json` file, you might notice that TensorFlow.js is\nnot a dependency. This is because the example loads TensorFlow.js from a CDN.\nHere's the complete HTML from\n[`index.html`](https://github.com/tensorflow/tfjs-examples/blob/master/getting-started/index.html): \n\n \u003chtml\u003e\n \u003chead\u003e\n \u003cscript src=\"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest\"\u003e \u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003ch4\u003eTiny TFJS example\u003chr/\u003e\u003c/h4\u003e\n \u003cdiv id=\"micro-out-div\"\u003eTraining...\u003c/div\u003e\n \u003cscript src=\"./index.js\"\u003e \u003c/script\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nThe `\u003cscript\u003e` element in the head loads the TensorFlow.js library, and the\n`\u003cscript\u003e` element at the end of the body loads the machine learning script.\n\nFor other ways to take a dependency on TensorFlow.js, see the\n[setup tutorial](/js/tutorials/setup).\n\nRun the example\n---------------\n\nRun the example and check out the results:\n\n1. In the `tfjs-examples/getting-started` directory, run `yarn watch`.\n2. Navigate to `http://127.0.0.1:1234` in your browser.\n\nYou should see a page title and beneath that a number like **38.31612014770508**.\nThe exact number will vary, but it should be close to 39.\n\nWhat just happened?\n-------------------\n\nWhen\n[`index.js`](https://github.com/tensorflow/tfjs-examples/blob/master/getting-started/index.js)\nis loaded, it trains a\n[`tf.sequential`](https://js.tensorflow.org/api/latest/#sequential) model using\n$ x $ and $ y $ values that satisfy the equation $ y = 2x - 1 $. \n\n // Create a simple model.\n const model = tf.sequential();\n model.add(tf.layers.dense({units: 1, inputShape: [1]}));\n\n // Prepare the model for training: Specify the loss and the optimizer.\n model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});\n\n // Generate some synthetic data for training. (y = 2x - 1)\n const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);\n const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);\n\n // Train the model using the data.\n await model.fit(xs, ys, {epochs: 250});\n\nThen it predicts a $ y $ value for the unseen $ x $ value `20` and updates the\nDOM to display the prediction. \n\n // Use the model to do inference on a data point the model hasn't seen.\n // Should print approximately 39.\n document.getElementById('micro-out-div').innerText =\n model.predict(tf.tensor2d([20], [1, 1])).dataSync();\n\nThe result of $ 2 \\* 20 - 1 $ is 39, so the predicted $ y $ value should be\napproximately 39.\n\nWhat's next\n-----------\n\nThis tutorial provided a minimal example of using TensorFlow.js to train a model\nin the browser. For a deeper introduction to training models with JavaScript,\nsee the TensorFlow.js [guide](/js/guide).\n\nMore ways to get started\n------------------------\n\nHere are more ways to get started with TensorFlow.js and web ML.\n\n### Watch the TensorFlow.js web ML course\n\nIf you're a web developer looking for a practical introduction to web ML, check\nout the Google Developers video course Machine Learning for Web Developers. The\ncourse shows you how to use TensorFlow.js in your websites and applications.\n\n[Go to the web ML course](https://youtube.com/playlist?list=PLOU2XLYxmsILr3HQpqjLAUkIPa5EaZiui)\n\n### Code ML programs without dealing directly with tensors\n\nIf you want to get started with machine learning without managing optimizers or\ntensor manipulation, then check out the ml5.js library.\n\nBuilt on top of TensorFlow.js, the ml5.js library provides access to machine\nlearning algorithms and models in the web browser with a concise, approachable\nAPI.\n\n[Check Out ml5.js](https://ml5js.org)\n\n### Install TensorFlow.js\n\nSee how to install TensorFlow.js for implementation in the web browser or\nNode.js.\n\n[Install TensorFlow.js](/js/tutorials/setup)\n\n### Convert pretrained models to TensorFlow.js\n\nLearn how to convert pretrained models from Python to TensorFlow.js.\n\n[Keras Model](/js/tutorials/conversion/import_keras)\n[GraphDef Model](/js/tutorials/conversion/import_saved_model)\n\n### Learn from existing TensorFlow.js code\n\nThe `tfjs-examples` repository provides small example implementations for\nvarious ML tasks using TensorFlow.js.\n\n[View tfjs-examples on GitHub](https://github.com/tensorflow/tfjs-examples)\n\n### Visualize the behavior of your TensorFlow.js model\n\n`tfjs-vis` is a small library for visualization in the web browser intended\nfor use with TensorFlow.js.\n\n[View tfjs-vis on GitHub](https://github.com/tensorflow/tfjs/tree/master/tfjs-vis)\n[See Demo](https://storage.googleapis.com/tfjs-vis/mnist/dist/index.html)\n\n### Prepare data for processing with TensorFlow.js\n\nTensorFlow.js has support for processing data using ML best practices.\n\n[View Documentation](https://js.tensorflow.org/api/latest/#Data)"]]