ปัญญาประดิษฐ์

บ้าน AI คืออะไร? ความฉลาดของมนุษย์ ประวัติศาสตร์ภาษา ประวัติของตัวเลข ประวัติคอมพิวเตอร์ หุ่นยนต์ เปลี่ยนงาน ตัวอย่างของ AI ทฤษฎีความคิด การเขียนโปรแกรม JavaScript AI ในเบราว์เซอร์

คณิตศาสตร์

คณิตศาสตร์ ฟังก์ชันเชิงเส้น พีชคณิตเชิงเส้น เวกเตอร์ เมทริกซ์ เทนเซอร์

สถิติ

ความน่าจะเป็น สถิติ การกระจาย

กราฟิก

AI Plotter AI กราฟเชิงเส้น แผน AI กระจาย

AI Science

วิทยาศาสตร์ การรวบรวมข้อมูล การจัดกลุ่ม การถดถอย การเรียนรู้ของเครื่อง โครงข่ายประสาทเทียม

การเรียนรู้ของเครื่อง

Perceptrons การยอมรับ การฝึกอบรม การทดสอบ การเรียนรู้ คำศัพท์ Brain.js

TensorFlow

TFJS กวดวิชา TFJS Operations TFJS รุ่น TFJS Viewer

ตัวอย่าง 1

Ex1 Intro ข้อมูล Ex1 รุ่น Ex1 การฝึกอบรม Ex1

ตัวอย่าง 2

บทนำ Ex2 ข้อมูล Ex2 รุ่น Ex2 การฝึกอบรม Ex2

JS กราฟิก

บทนำ กราฟแคนวาส กราฟ Plotly.js กราฟ Chart.js กราฟ Google กราฟ D3.js

แบบจำลองเทนเซอร์โฟลว์


TesorFlow.js

ไลบรารี JavaScript สำหรับ

การฝึกอบรมและการปรับใช้
โมเดลการเรียนรู้ของเครื่อง
ในเบราว์เซอร์


โมเดลเทนเซอร์โฟลว์

โมเดลและเลเยอร์เป็นส่วนประกอบสำคัญใน การเรียนรู้ ของเครื่อง

สำหรับงานแมชชีนเลิร์นนิงต่างๆ คุณต้องรวมเลเยอร์ประเภทต่างๆ เข้าเป็นโมเดลที่สามารถฝึกด้วยข้อมูลเพื่อทำนายค่าในอนาคต

TensorFlow.js รองรับโมเดลประเภทต่างๆ และเลเยอร์ ประเภทต่างๆ

โมเดล TensorFlow คือโครงข่ายประสาทเทียม ที่มีอย่าง น้อยหนึ่งเลเยอร์


โครงการเทนเซอร์โฟลว์

โปรเจ็กต์ Tensorflow มีเวิร์กโฟลว์ทั่วไปดังนี้:

  • การรวบรวมข้อมูล
  • การสร้างแบบจำลอง
  • การเพิ่มเลเยอร์ให้กับโมเดล
  • รวบรวมโมเดล
  • การฝึกโมเดล
  • การใช้โมเดล

ตัวอย่าง

Suppose you knew a function that defined a strait line:

Y = 1.2X + 5

Then you could calculate any y value with the JavaScript formula:

y = 1.2 * x + 5;

To demonstrate Tensorflow.js, we could train a Tensorflow.js model to predict Y values based on X inputs.

The TensorFlow model does not know the function.

// Create Training Data
const xs = tf.tensor([0, 1, 2, 3, 4]);
const ys = xs.mul(1.2).add(5);

// Define a Linear Regression Model
const model = tf.sequential();
model.add(tf.layers.dense({units:1, inputShape:[1]}));

// Specify Loss and Optimizer
model.compile({loss:'meanSquaredError', optimizer:'sgd'});

// Train the Model
model.fit(xs, ys, {epochs:500}).then(() => {myFunction()});

// Use the Model
function myFunction() {
  const xArr = [];
  const yArr = [];
  for (let x = 0; x <= 10; x++) {
    xArr.push(x);
    let result = model.predict(tf.tensor([Number(x)]));
    result.data().then(y => {
      yArr.push(Number(y));
      if (x == 10) {plot(xArr, yArr)};
    });
  }
}

The example is explained below:


Collecting Data

Create a tensor (xs) with 5 x values:

const xs = tf.tensor([0, 1, 2, 3, 4]);

Create a tensor (ys) with 5 correct y answers (multiply xs with 1.2 and add 5):

const ys = xs.mul(1.2).add(5);

Creating a Model

Create a sequential mode:.

const model = tf.sequential();

In a sequential model, the output from one layer is the input to the next layer.


Adding Layers

Add one dense layer to the model.

The layer is only one unit (tensor) and the shape is 1 (one dimentional):

model.add(tf.layers.dense({units:1, inputShape:[1]}));

in a dense the layer, every node is connected to every node in the preceding layer.


Compiling the Model

Compile the model using meanSquaredError as loss function and sgd (stochastic gradient descent) as optimizer function:

model.compile({loss:'meanSquaredError', optimizer:'sgd'});

Tensorflow Optimizers

  • Adadelta -Implements the Adadelta algorithm.
  • Adagrad - Implements the Adagrad algorithm.
  • Adam - Implements the Adam algorithm.
  • Adamax - Implements the Adamax algorithm.
  • Ftrl - Implements the FTRL algorithm.
  • Nadam - Implements the NAdam algorithm.
  • Optimizer - Base class for Keras optimizers.
  • RMSprop - Implements the RMSprop algorithm.
  • SGD - Stochastic Gradient Descent Optimizer.

Training the Model

Train the model (using xs and ys) with 500 repeats (epochs):

model.fit(xs, ys, {epochs:500}).then(() => {myFunction()});

Using the Model

After the model is trained, you can use it for many different purposes.

This example predicts 10 y values, given 10 x values, and calls a function to plot the predictions in a graph:

function myFunction() {
  const xArr = [];
  const yArr = [];
  for (let x = 0; x <= 10; x++) {
    let result = model.predict(tf.tensor([Number(x)]));
    result.data().then(y => {
      xArr.push(x);
      yArr.push(Number(y));
      if (x == 10) {display(xArr, yArr)};
    });
  }
}

This example predicts 10 y values, given 10 x values, and calls a function to display the values:

function myFunction() {
  const xArr = [];
  const yArr = [];
  for (let x = 0; x <= 10; x++) {
    let result = model.predict(tf.tensor([Number(x)]));
    result.data().then(y => {
      xArr.push(x);
      yArr.push(Number(y));
      if (x == 10) {display(xArr, yArr)};
    });
  }
}