# 多层神经网络任务

# 加载 XOR 数据集

data.js

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, 0);
  genGauss(-2, -2, 0);
  genGauss(-2, 2, 1);
  genGauss(2, -2, 1);
  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
35
36
import * as tfvis from "@tensorflow/tfjs-vis";
import { getData } from "./data";

window.onload = () => {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "XOR 数据训练" },
    {
      values: [data.filter(p => p.label === 0), data.filter(p => p.label === 1)]
    }
  );
};
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";
import { mod } from "@tensorflow/tfjs";

window.onload = () => {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "XOR 数据训练" },
    {
      values: [data.filter(p => p.label === 0), data.filter(p => p.label === 1)]
    }
  );

  const model = tf.sequential();
  // 添加隐藏层 加入4个神经元比较稳定,
  model.add(
    tf.layers.dense({
      units: 4,
      inputShape: [2],
      activation: "relu"
    })
  );
  // 添加输出层 dense上一个的输出是下一个的输入,所以不用在写inputShape
  model.add(
    tf.layers.dense({
      units: 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
25
26
27
28
29
30
31

# 训练模型并预测

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

window.onload = async () => {
  const data = getData(400);
  tfvis.render.scatterplot(
    { name: "XOR 数据训练" },
    {
      values: [data.filter(p => p.label === 0), data.filter(p => p.label === 1)]
    }
  );

  const model = tf.sequential();
  // 添加隐藏层 加入4个神经元比较稳定,
  model.add(
    tf.layers.dense({
      units: 4,
      inputShape: [2],
      activation: "relu"
    })
  );
  // 添加输出层 dense上一个的输出是下一个的输入,所以不用在写inputShape
  model.add(
    tf.layers.dense({
      units: 1,
      activation: "sigmoid"
    })
  );

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

  const inputs = tf.tensor(data.map(p => [p.x, p.y]));
  const labels = tf.tensor(data.map(p => p.label));

  await model.fit(inputs, labels, {
    epochs: 10,
    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
46
47
48
49
50

tensorflow