search
HomeWeb Front-endJS TutorialHow to create a basic AI model with TensorFlow.js?

How to create a basic AI model with TensorFlow.js?

Nov 10, 2020 pm 05:54 PM
javascripttensorflowfront end

How to create a basic AI model with TensorFlow.js?

In this article we look at how to use TensorFlow.js to create basic AI models and use more complex models to achieve some interesting functions. I have just started to come into contact with artificial intelligence. Although in-depth knowledge of artificial intelligence is not required, I still need to understand some concepts.

What is a model?

The real world is very complex, and we need to simplify it to understand it. We can simplify it through models. There are many types of models: such as world maps, or charts, etc.

How to create a basic AI model with TensorFlow.js?

For example, if you want to build a model to express the relationship between house rental price and house area: First, you need to collect some data:

##3131000 31250004235000##45

Then, display these data on a two-dimensional graph, and treat each parameter (price, number of rooms) as 1 dimension:

How to create a basic AI model with TensorFlow.js?

Then we can Draw a line and predict the rental price of a house with more rooms. This model is called linear regression, and it is one of the simplest models in machine learning. But this model is not good enough:

  1. There are only 5 data, so it is not reliable enough.
  2. There are only 2 parameters (price, room), but there are more factors that may affect the price: such as area, decoration, etc.

The first problem can be solved by adding more data, say a million. For the second question, more dimensions can be added. In a two-dimensional chart it is easy to understand the data and draw a line, in a three-dimensional chart you can use a plane:

How to create a basic AI model with TensorFlow.js?

But what about when the dimensions of the data are three dimensions, four dimensions or even 1000000 When the dimension exceeds three dimensions, the brain has no way to visualize it on a chart, but the hyperplane can be calculated mathematically when the dimension exceeds three dimensions, and neural networks were born to solve this problem.

What is a neural network?

To understand what a neural network is, you need to know what a neuron is. A real neuron looks like this:

How to create a basic AI model with TensorFlow.js?

A neuron is composed of the following parts:

  • Dendrite : This is the input end of the data.
  • Axon: This is the output end.
  • Synapse (not represented in the diagram): This structure allows communication between one neuron and another. It is responsible for transmitting electrical signals between the nerve endings of axons and the dendrites of nearby neurons. These synapses are key to learning because they increase or decrease electrical activity depending on their use.

Neurons in machine learning (simplified):

How to create a basic AI model with TensorFlow.js?

  • Inputs (inputs) : Inputs parameter.
  • Weights: Like synapses, used to better establish linear regression by adjusting neurons.
  • Linear function: Each neuron is like a linear regression function. For a linear regression model, only one neuron is enough.
  • Activation function: Some activation functions can be used to change the output from a scalar to another non-linear function. Common ones are sigmoid, RELU and tanh.
  • Output (output) : The calculated output after applying the activation function.

The activation function is very useful, and the power of neural networks is mainly attributed to it. Without any activation function, it is impossible to get an intelligent neuron network. Because even though you have multiple neurons in your neural network, the output of your neural network will always be a linear regression. Therefore, some mechanism is needed to transform each linear regression into nonlinear to solve nonlinear problems. These linear functions can be converted into nonlinear functions through the activation function:

How to create a basic AI model with TensorFlow.js?

#Training model

As described in the 2D linear regression example, just Draw a line in the graph to predict new data. Still, the idea of ​​"deep learning" is to have our neural network learn to draw this line. For a simple line, you can use a very simple neural network with only one neuron, but for a model that wants to do more complex things, such as classifying two sets of data, the network needs to be "trained" Learn how to get the following:

How to create a basic AI model with TensorFlow.js?

The process is not complicated because it is two-dimensional. Each model is used to describe a world, but the concept of "training" is very similar across all models. The first step is to draw a random line and improve it iteratively in the algorithm, correcting errors in the process during each iteration. This optimization algorithm is called Gradient Descent (algorithms with the same concept are also more complex SGD or ADAM, etc.). Each algorithm (linear regression, logarithmic regression, etc.) has a different cost function to measure the error, and the cost function will always converge to a certain point. It can be a convex or concave function, but it will eventually converge to a point with 0% error. Our goal is to achieve this.

How to create a basic AI model with TensorFlow.js?

When using the gradient descent algorithm, we start from some random point in its cost function, but we don't know where it is! It's like being blindfolded and thrown on a mountain. If you want to go down the mountain, you have to go to the lowest point step by step. If the terrain is irregular (such as a concave function), the descent will be more complicated.

I won’t explain the “gradient descent” algorithm in depth here. It’s enough to remember that this is an optimization algorithm for minimizing prediction errors in the process of training AI models. This algorithm requires a lot of time and GPU for matrix multiplication. It is usually difficult to reach this convergence point on the first execution, so some hyperparameters need to be modified, such as the learning rate or adding regularization. After gradient descent iterations, the convergence point is approached when the error approaches 0%. This creates a model that can then be used to make predictions.

How to create a basic AI model with TensorFlow.js?

Training models with TensorFlow.js

TensorFlow.js provides an easy way to create neural networks. First create a LinearModel class using the trainModel method. We will use a sequential model. A sequential model is a model in which the output of one layer is the input to the next layer, i.e. when the model topology is a simple hierarchy with no branches or skips. Define the layers inside the trainModel method (we use only one layer as it is enough to solve the linear regression problem):

import * as tf from '@tensorflow/tfjs';

/**
* 线性模型类
*/
export default class LinearModel {
  /**
 * 训练模型
 */
  async trainModel(xs, ys){
    const layers = tf.layers.dense({
      units: 1, // 输出空间的纬度
      inputShape: [1], // 只有一个参数
    });
    const lossAndOptimizer = {
      loss: 'meanSquaredError',
      optimizer: 'sgd', // 随机梯度下降
    };

    this.linearModel = tf.sequential();
    this.linearModel.add(layers); // 添加一层
    this.linearModel.compile(lossAndOptimizer);

    // 开始模型训练
    await this.linearModel.fit(
      tf.tensor1d(xs),
      tf.tensor1d(ys),
    );
  }

  //...
}

Use this class for training:

const model = new LinearModel()

// xs 与 ys 是 数组成员(x-axis 与 y-axis)
await model.trainModel(xs, ys)

End of training Then you can start making predictions.

Prediction with TensorFlow.js

Although some hyperparameters need to be defined in advance when training the model, making general predictions is still easy. It is enough to pass the following code:

import * as tf from '@tensorflow/tfjs';

export default class LinearModel {
  ... //前面训练模型的代码

  predict(value){
    return Array.from(
      this.linearModel
      .predict(tf.tensor2d([value], [1, 1]))
      .dataSync()
    )
  }
}

Now you can predict:

const prediction = model.predict(500) // 预测数字 500
console.log(prediction) // => 420.423

How to create a basic AI model with TensorFlow.js?

Using a pre-trained model in TensorFlow.js

Training the model is the hardest part. First, the data is standardized for training, and all hyperparameters need to be set correctly, etc. For us beginners, we can directly use those pre-trained models. TensorFlow.js can use many pretrained models and can also import external models created with TensorFlow or Keras. For example, you can directly use the posenet model (real-time human posture assessment) to do some interesting projects:

How to create a basic AI model with TensorFlow.js?

The code of this Demo: https://github.com/aralroca/posenet- d3

It is easy to use:

import * as posenet from '@tensorflow-models/posenet'

// 设置一些常数
const imageScaleFactor = 0.5
const outputStride = 16
const flipHorizontal = true
const weight = 0.5

// 加载模型
const net = await posenet.load(weight)

// 进行预测
const poses = await net.estimateSinglePose(
  imageElement,
  imageScaleFactor,
  flipHorizontal,
  outputStride
)

This JSON is pose Variable:

{
  "score": 0.32371445304906,
  "keypoints": [
    {
      "position": {
        "y": 76.291801452637,
        "x": 253.36747741699
      },
      "part": "nose",
      "score": 0.99539834260941
    },
    {
      "position": {
        "y": 71.10383605957,
        "x": 253.54365539551
      },
      "part": "leftEye",
      "score": 0.98781454563141
    }
    // 后面还有: rightEye, leftEar, rightEar, leftShoulder, rightShoulder
    // leftElbow, rightElbow, leftWrist, rightWrist, leftHip, rightHip,
    // leftKnee, rightKnee, leftAnkle, rightAnkle...
  ]
}

You can see it from the official demo, use this model There are many interesting projects that can be developed.

How to create a basic AI model with TensorFlow.js?

Source code of this project: https://github.com/aralroca/fishFollow-posenet-tfjs

Import Keras model

External models can be imported into TensorFlow.js. Below is a program for number recognition using Keras model (h5 format). First, use tfjs_converter to convert the format of the model.

pip install tensorflowjs

Use the converter:

tensorflowjs_converter --input_format keras keras/cnn.h5 src/assets

Finally, import the model into JS code:

// 载入模型
const model = await tf.loadModel('./assets/model.json')

// 准备图片
let img = tf.fromPixels(imageData, 1)
img = img.reshape([1, 28, 28, 1])
img = tf.cast(img, 'float32')

// 进行预测
const output = model.predict(img)

It only takes a few lines of code to complete. Of course, you can add more logic to the code to achieve more functions. For example, you can write numbers on canvas and then get their images for prediction.

How to create a basic AI model with TensorFlow.js?

Source code of this project: https://github.com/aralroca/MNIST_React_TensorFlowJS

Why should it be used in the browser?

Due to different devices, the efficiency may be very low when training the model in the browser. Using TensorFlow.js to use WebGL to train the model in the background is 1.5 to 2 times slower than using the Python version of TensorFlow.

But before the emergence of TensorFlow.js, there was no API that could directly use machine learning models in the browser. Now, models can be trained and used offline in browser applications. And predictions are faster because there are no requests to the server. Another benefit is low cost since all these calculations are done on the client side.

Summary

  • A model is a simplified way of representing the real world that can be used to make predictions.
  • You can use neural networks to create models.
  • TensorFlow.js is a simple tool for creating neural networks.

English original address: https://aralroca.com/blog/first-steps-with-tensorflowjs

Author: Aral Roca

For more programming-related knowledge, please visit: Programming Courses! !

Number of rooms Price
265000
535000

The above is the detailed content of How to create a basic AI model with TensorFlow.js?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.