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 训练和部署机器学习模型。
了解开始的更多方式,请参阅下面的部分。
在不直接处理张量的情况下编写 ML 程序
想要开始机器学习,同时不用担心任何类似张量或优化器的低级细节吗?
ml5.js 库构建在 TensorFlow.js 之上,通过简洁的、可利用的 API,可以在浏览器中访问机器学习算法和模型。
Check out ml5.js
安装 TensorFlow.js
TensorFlow.js 与 Tensors (张量)、Layers (图层)、Optimizers (优化器) 和损失函数等概念兼容(或希望与它们兼容)吗?TensorFlow.js 为 JavaScript 中神经网络编程提供了灵活的构建块。
请参阅如何在浏览器或 Node.js 中启动和运行 TensorFlow.js 代码。
Get Setup
将预训练模型转换到 TensorFlow.js
学习如何将预训练模型从 python 转换为 TensorFlow.js
Keras Model
GraphDef Model
从现有 TensorFlow.js 代码中学习
tfjs-examples 提供了使用 TensorFlow.js 实现各种 ML 任务的简短代码示例。
See it on GitHub
可视化您的 TensorFlow.js 模型的状态
tfjs-vis 是一个用于在浏览器内实现可视化的小型库,用于TensorFlow.js。
See it on GitHub
See Demo
准备好用 TensorFlow.js 处理数据
TensorFlow.js 支持使用 ML 最佳实践处理数据。
See docs
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2020-08-18。
[null,null,["最后更新时间 (UTC):2020-08-18。"],[],[],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)"]]