search
HomeTechnology peripheralsAITensorFlow.js can handle machine learning on the browser!

Today, with the rapid development of machine learning, various machine learning platforms emerge in endlessly. In order to meet the needs of different business scenarios, machine learning models can be deployed to Android, iOS, and Web browsers respectively, so that the models can be used on the terminal can perform deductions to unleash the potential of the model. TensorFlow.js is the JavaScript version of TensorFlow, supports GPU hardware acceleration, and can run in Node.js or browser environments. It not only supports developing, training, and deploying models from scratch based entirely on JavaScript, but can also be used to run existing Python versions of TensorFlow models, or continue training based on existing models.

TensorFlow.js Advantages

TensorFlow.js allows users to load TensorFlow models in the browser, allowing users to perform machine learning deductions through local CPU/GPU resources. . Machine learning in the browser will have the following four advantages compared to the server side:

1. No need to install software or drivers (you can use it by opening the browser) ;

2. More convenient human-computer interaction can be performed through the browser;

3. The mobile phone hardware can be called through the mobile browser Various sensors (such as: GPS, acceleration sensor, camera, etc.);

4. The user's data can be completed locally without uploading to the server.

TensorFlow.js Architecture

The advantages of TensorFlow.js are introduced above. Here let us take a look at the architecture of TensorFlow.js. As shown in Figure 1, the TensorFlow.js architecture includes Core API and Layers API (upper part of the figure). The Layers API provides higher-level interfaces, such as grammatical structures similar to KerasAPI. The purpose of these grammatical structures is to allow developers to use JavaScript to easily develop machine learning through higher-granularity abstraction. The CoreAPI mainly includes the core functions provided by TensorFlow.js, such as Tensor creation, data operations, memory management, etc. At the same time, CoreAPI also provides tools to convert machine learning models in Python into JSON format that can be used by browsers, making it easier to reuse existing models in JavaScript. Therefore, CoreAPI can run on the browser side and can use WebGL for GPU acceleration. Of course, it can also run on Node.js, depending on the specific operating environment for acceleration through GPU and TPU.

TensorFlow.js can handle machine learning on the browser!

Figure 1 TensorFlow.js architecture

TensorFlow.js case for linear regression

I mentioned the advantages and architecture of TensorFlow.js earlier. In order for everyone to have a deeper understanding of TensorFlow.js, let’s take a simple linear regression example to see how machine learning training and implementation are implemented on the browser side. Deduced.

Suppose we need to build a linear model of y = ax1 bx2 c, as shown in Figure 2, which requires the following steps:

1. Download the TensorFlow.js file

2. Training data and test data

3. Build the model

4. Training model

5. Model application

TensorFlow.js can handle machine learning on the browser!

Figure 2 TensorFlow.js Building a linear regression model

It can be seen from these 5 steps that the basic process is the same as building a model in Python, except that the first step requires downloading the TensorFlow.js file.

As shown in Figure 3, in order to load the TensorFlow.js file, we need to introduce script in the head tag of the page, where the file tf.min.js has been deployed to TensorFlow's CDN server , we just need to reference the file.

TensorFlow.js can handle machine learning on the browser!

Figure 3 Reference to TensorFlow.js file

In order to ensure that the TensorFlow.js file is imported correctly, As shown in Figure 4, open the browser and enable the developer tools, enter tf.version in the Console to obtain the tfjs-core, tfjs-backend-cpu and other information corresponding to TensorFlow, indicating that the file has been introduced successfully. Since the TensorFlow.js file contains the TensorFlow operation library, you need to ensure that the file is loaded correctly.

TensorFlow.js can handle machine learning on the browser!

Figure 4 Confirm that the TensorFlow.js file is correctly introduced

After loading the TensorFlow.js file, we You can write machine learning code in html. As shown in Figure 5, write the following code in the script tag. The doTraining method of async is used to train the model. The epoch is 500 times. The purpose of using async here is not to block other operations of the web page. The fit method in the model is called inside the function to fit the model. The input parameters are xs and ys. The fitting results are output in the callback function callbacks, and the loss function of loss is printed.

The next step is to construct the model. Here we use tf.sequential(); to construct the model. In order to construct the y = ax1 bx2 c model, we need to construct a neuron. This neuron The element has two inputs and one output.

So, add a dense layer through model.add, define units: 1, which is a neuron, inputShape: [2], and the input is a two-dimensional one. After having the model, compile the model through model.complie. The loss function of meanSquareError and the optimizer of sgd are used here. Finally, the entire neuron network is printed out through the model's summary method. Immediately in the dataset link, we prepared xs and ys as input, and testData_x as test data. Finally, call doTraining(model) to train the model, and use the predict method to predict the results.

TensorFlow.js can handle machine learning on the browser!

Figure 5 Training the model in the browser

Save the above file as an html file and re- Open it, and you can see the result in Figure 6 after about 1-2 seconds. On the right is the loss result obtained in each epoch printed in the developer tools. It can be seen that the loss function becomes smaller and smaller as the training progresses. At the same time, the prediction result of Tensor was finally obtained as 15.5082932.

TensorFlow.js can handle machine learning on the browser!

Figure 6 Running results

TensorFlow.js model reuse

Yes Based on the above simple example, we can easily inspect the machine learning model on the browser side, but the training method of the model requires resources and a long training time. So, can we take the trained model directly to the browser for prediction and deduction? The answer is yes.

Generally speaking, there are two ways to reuse models. The first is to use the model created by the developer himself in Python, and save the model into tfjs format and use it in the browser. The other is to directly call the model provided by TensorFlow.

TensorFlow.js can handle machine learning on the browser!

Figure 7 Model reuse

Develop your own defined model

such as As shown in Figure 8, we build, train and save the model in python. The steps of building models, neuron networks, setting optimizers, loss functions, and data preparation will not be described here. After the model training is completed, save the model through the save_model method.

TensorFlow.js can handle machine learning on the browser!

Figure 8 Develop your own model

With the model, you need to use TensorFlow.js The provided tools convert the model so that it can be used in the browser.

Here, use the following command to install the TensorFlow.js tool.

pip install tensorflowjs
tensorflwjs_converter --input_format=keras_saved_model ./saved_model/ ./model/

The tensorflwjs_converter command is used here to convert the model. The input format is keras_saved_model, the source file address is ./saved_model/, and the target file address is ./ model/, after pressing Enter, you can see the converted file at the target file address.

You only need to reference this converted model file in the browser, as shown in Figure 9. The run method in the script directly references the model file model.json and uses loadLayersModel to load the model. After setting the input, Use the predict method to predict the model.

TensorFlow.js can handle machine learning on the browser!

Figure 9 Using the converted model

Model provided by TensorFlow

Above we demonstrated that you can use your own trained machine learning model, here you can also use https://www.php.cn/link/ff82db7535530637af7f8a96284b3459Find the model provided by TensorFlow .

As shown in Figure 10, TensorFlow has tailored some models for some business scenarios, such as: portrait depth estimation, image classification, object detection, body segmentation, posture detection, Text malware detection and more. Students who want to know how to further deploy models in production scenarios can also take the time to read the explanations of TensorFlow deployment functions and answers to frequently asked questions by Google developer experts: https: //www.php.cn/link/bb96ff7f5c9505fd971126ecd171bec2

TensorFlow.js can handle machine learning on the browser!

#Figure 10 Model provided by TensorFlow

By studying TensorFlow official online courses, I grew from a machine learning novice to an experienced machine learning veteran. From "TensorFlow Introductory Practical Course" and 《TensorFlow Introductory Course—Deployment》In the course, I learned how to save and convert machine learning models, and at the same time, I can also convert machine learning models according to different application scenarios. Deploy to Android, iOS, browsers and servers. The TensorFlow platform is like a kaleidoscope, allowing me to see colorful application projects, while also understanding the underlying logic of machine learning modeling and prediction. If you also want to improve your machine learning capabilities, you can study together"TensorFlow Introductory Course - Deployment", and leave your evaluation of the course. Sign up now and have a chance to win official exquisite gifts!

TensorFlow.js can handle machine learning on the browser!

Zhang Yunbo, an active IT internet celebrity lecturer with 310,000 students, started and released Apple Swift, Android Kotlin, WeChat applets, and blockchain technology early in China One of the lecturers. Focusing on front-end development, iOS development, Android development, Flutter development, and blockchain Dapp development, he has rich experience working in large companies and overseas.

The above is the detailed content of TensorFlow.js can handle machine learning on the browser!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
How to Run LLM Locally Using LM Studio? - Analytics VidhyaHow to Run LLM Locally Using LM Studio? - Analytics VidhyaApr 19, 2025 am 11:38 AM

Running large language models at home with ease: LM Studio User Guide In recent years, advances in software and hardware have made it possible to run large language models (LLMs) on personal computers. LM Studio is an excellent tool to make this process easy and convenient. This article will dive into how to run LLM locally using LM Studio, covering key steps, potential challenges, and the benefits of having LLM locally. Whether you are a tech enthusiast or are curious about the latest AI technologies, this guide will provide valuable insights and practical tips. Let's get started! Overview Understand the basic requirements for running LLM locally. Set up LM Studi on your computer

Guy Peri Helps Flavor McCormick's Future Through Data TransformationGuy Peri Helps Flavor McCormick's Future Through Data TransformationApr 19, 2025 am 11:35 AM

Guy Peri is McCormick’s Chief Information and Digital Officer. Though only seven months into his role, Peri is rapidly advancing a comprehensive transformation of the company’s digital capabilities. His career-long focus on data and analytics informs

What is the Chain of Emotion in Prompt Engineering? - Analytics VidhyaWhat is the Chain of Emotion in Prompt Engineering? - Analytics VidhyaApr 19, 2025 am 11:33 AM

Introduction Artificial intelligence (AI) is evolving to understand not just words, but also emotions, responding with a human touch. This sophisticated interaction is crucial in the rapidly advancing field of AI and natural language processing. Th

12 Best AI Tools for Data Science Workflow - Analytics Vidhya12 Best AI Tools for Data Science Workflow - Analytics VidhyaApr 19, 2025 am 11:31 AM

Introduction In today's data-centric world, leveraging advanced AI technologies is crucial for businesses seeking a competitive edge and enhanced efficiency. A range of powerful tools empowers data scientists, analysts, and developers to build, depl

AV Byte: OpenAI's GPT-4o Mini and Other AI InnovationsAV Byte: OpenAI's GPT-4o Mini and Other AI InnovationsApr 19, 2025 am 11:30 AM

This week's AI landscape exploded with groundbreaking releases from industry giants like OpenAI, Mistral AI, NVIDIA, DeepSeek, and Hugging Face. These new models promise increased power, affordability, and accessibility, fueled by advancements in tr

Perplexity's Android App Is Infested With Security Flaws, Report FindsPerplexity's Android App Is Infested With Security Flaws, Report FindsApr 19, 2025 am 11:24 AM

But the company’s Android app, which offers not only search capabilities but also acts as an AI assistant, is riddled with a host of security issues that could expose its users to data theft, account takeovers and impersonation attacks from malicious

Everyone's Getting Better At Using AI: Thoughts On Vibe CodingEveryone's Getting Better At Using AI: Thoughts On Vibe CodingApr 19, 2025 am 11:17 AM

You can look at what’s happening in conferences and at trade shows. You can ask engineers what they’re doing, or consult with a CEO. Everywhere you look, things are changing at breakneck speed. Engineers, and Non-Engineers What’s the difference be

Rocket Launch Simulation and Analysis using RocketPy - Analytics VidhyaRocket Launch Simulation and Analysis using RocketPy - Analytics VidhyaApr 19, 2025 am 11:12 AM

Simulate Rocket Launches with RocketPy: A Comprehensive Guide This article guides you through simulating high-power rocket launches using RocketPy, a powerful Python library. We'll cover everything from defining rocket components to analyzing simula

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)