# 逻辑回归任务

# 准备数据集

export function getData(numSamples) {
  let points = [];

  function genGauss(cx, cy, label) {
    for (let i = 0; i < numSamples / 2; i++) {
      let x = normalRandom(cx);
      let y = normalRandom(cy);
      points.push({ x, y, label });
    }
  }

  genGauss(2, 2, 1);
  genGauss(-2, -2, 0);
  return points;
}

/**
 * Samples from a normal distribution. Uses the seedrandom library as the
 * random generator.
 *
 * @param mean The mean. Default is 0.
 * @param variance The variance. Default is 1.
 */
function normalRandom(mean = 0, variance = 1) {
  let v1, v2, s;
  do {
    v1 = 2 * Math.random() - 1;
    v2 = 2 * Math.random() - 1;
    s = v1 * v1 + v2 * v2;
  } while (s > 1);

  let result = Math.sqrt((-2 * Math.log(s)) / s) * v1;
  return mean + Math.sqrt(variance) * result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 加载二分类数据

import * as tfvis from "@tensorflow/tfjs-vis";
import { getData } from "./data";

window.onload = function() {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "逻辑回归训练数据" },
    {
      values: [data.filter(p => p.label === 1), data.filter(p => p.label === 0)]
    }
  );
};
1
2
3
4
5
6
7
8
9
10
11
12

tensorflow

# 定义模型结构:带有激活函数的单个神经元

import * as tf from "@tensorflow/tfjs";
import * as tfvis from "@tensorflow/tfjs-vis";
import { getData } from "./data";

window.onload = function() {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "逻辑回归训练数据" },
    {
      values: [data.filter(p => p.label === 1), data.filter(p => p.label === 0)]
    }
  );
  // ----------------------------------
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      units: 1,
      inputShape: [2],
      // 激活函数,目的是把值压缩到0,1之间,sigmoid是一个0,1之间的函数
      activation: "sigmoid"
    })
  );
  // ----------------------------------
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 损失函数:对数损失(logLoss)

import * as tf from "@tensorflow/tfjs";
import * as tfvis from "@tensorflow/tfjs-vis";
import { getData } from "./data";

window.onload = function() {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "逻辑回归训练数据" },
    {
      values: [data.filter(p => p.label === 1), data.filter(p => p.label === 0)]
    }
  );
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      units: 1,
      inputShape: [2],
      // 激活函数,目的是把值压缩到0,1之间,sigmoid是一个0,1之间的函数
      activation: "sigmoid"
    })
  );
  ////------------------------------------------
  // 损失器采用 logLoss,是对数型的,符合0,1之间   优化器选用adam,可以自动调节
  model.compile({ loss: tf.losses.logLoss, optimizer: tf.train.adam(0.1) });
  ////------------------------------------------
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 训练模型可视化操作步骤

import * as tf from "@tensorflow/tfjs";
import * as tfvis from "@tensorflow/tfjs-vis";
import { getData } from "./data";

window.onload = async function() {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "逻辑回归训练数据" },
    {
      values: [data.filter(p => p.label === 1), data.filter(p => p.label === 0)]
    }
  );
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      units: 1,
      inputShape: [2],
      // 激活函数,目的是把值压缩到0,1之间,sigmoid是一个0,1之间的函数
      activation: "sigmoid"
    })
  );

  model.compile({ loss: tf.losses.logLoss, optimizer: tf.train.adam(0.1) });

  // --------------------------------------------------------------
  // 输入数据
  const input = tf.tensor(data.map(p => [p.x, p.y]));
  const labels = tf.tensor(data.map(p => p.label));

  // 训练模型
  await model.fit(input, labels, {
    batchSize: 40,
    epochs: 50,
    callbacks: tfvis.show.fitCallbacks({ name: "训练过程" }, ["loss"])
  });
  // ----------------------------------------------------------------
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 在页面输入预测的点预测

index.html

<script src="./script.js"></script>

<form action="" onsubmit="predict(this);return false;">
    x: <input type="text" name="x">
    y: <input type="text" name="y">
    <button type="submit">预测</button>
</form>
1
2
3
4
5
6
7
import * as tf from "@tensorflow/tfjs";
import * as tfvis from "@tensorflow/tfjs-vis";
import { getData } from "./data";
import { model } from "@tensorflow/tfjs";

window.onload = async function() {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "逻辑回归训练数据" },
    {
      values: [data.filter(p => p.label === 1), data.filter(p => p.label === 0)]
    }
  );
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      units: 1,
      inputShape: [2],
      // 激活函数,目的是把值压缩到0,1之间,sigmoid是一个0,1之间的函数
      activation: "sigmoid"
    })
  );

  model.compile({ loss: tf.losses.logLoss, optimizer: tf.train.adam(0.1) });

  // 输入数据
  const input = tf.tensor(data.map(p => [p.x, p.y]));
  const labels = tf.tensor(data.map(p => p.label));

  await model.fit(input, labels, {
    batchSize: 40,
    epochs: 50,
    callbacks: tfvis.show.fitCallbacks({ name: "训练过程" }, ["loss"])
  });

  // ----------------------------------------------------------
  window.predict = form => {
    const pred = model.predict(
      tf.tensor([[form.x.value * 1, form.y.value * 1]])
    );

    alert(`预测结果: ${pred.dataSync()[0]}`);
  };
  // ----------------------------------------------------------
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45