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 C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools