如何运行一个2层感知器来解决XOR

How to run a 2-layer perceptron to solve XOR

XOR 无法通过使用具有标准标量积和单位阶跃函数的单个感知器来解决。

这篇文章建议用3个感知器做一个网络: http://toritris.weebly.com/perceptron-5-xor-how--why-neurons-work-together.html

我正在尝试以这种方式 运行 3 感知器网络,但它不会为 XOR 产生正确的结果:

//pseudocode
class perceptron {

  constructor(training_data) {
    this.training_data = training_data   
  }

  train() {
    iterate multiple times over training data
    to train weights
  }

  unit_step(value) {
    if (value<0) return 0
    else return 1
  }

  compute(input) {
    weights = this.train()
    sum     = scalar_product(input,weights)
    return unit_step(sum)
  }
}

上面的感知器可以正确解决NOT、AND、OR位运算。这就是我如何使用 3 个感知器来解决 XOR:

AND_perceptron = perceptron([
  {Input:[0,0],Output:0},
  {Input:[0,1],Output:0},
  {Input:[1,0],Output:0},
  {Input:[1,1],Output:1}
])

OR_perceptron = perceptron([
  {Input:[0,0],Output:0},
  {Input:[0,1],Output:1},
  {Input:[1,0],Output:1},
  {Input:[1,1],Output:1}
])

XOR_perceptron = perceptron([
  {Input:[0,0],Output:0},
  {Input:[0,1],Output:1},
  {Input:[1,0],Output:1},
  {Input:[1,1],Output:0}
])

test_x1 = 0
test_x2 = 1 

//first layer of perceptrons
and_result   = AND_perceptron.compute(test_x1,test_x2)
or_result    = OR_perceptron.compute(test_x1,test_x2)

//second layer
final_result = XOR_perceptron.compute(and_result,or_result)

上面的final_result不一致,有时是0,有时是1。看来我运行错了2层。如何 运行 这 3 个感知器在 2 层中的正确方式?

如果你想构建一个带有逻辑连接词(and, or, not)的神经网络,你必须考虑以下关于 xor 的等价:

A xor B ≡ (A ∨ B) ∧ ¬(A ∧ B) ≡ (A ∨ B) ∧ (¬A ∨ ¬B) ≡ (A ∧ ¬B) ∨ (¬A ∧ B)

所以如果我理解正确的话,如果你想使用你的感知器,你至少需要三个和-或-或-感知器和一个否定。在这篇文章中,他们使用了三个具有特殊权重的感知器来进行异或运算。这些与 and- 和 or- 感知器不同。