모든 챌린지
어려움 0명 해결 정답률 0%

Train a Neural Network to Solve XOR

Combine everything: train a 2-2-1 sigmoid network with backpropagation until it learns XOR.

배우게 될 내용

  • Train a small network end to end
  • See why XOR needs a hidden layer
  • Put forward and backward passes together

문제

번역본 보기

XOR (exclusive or) is the classic problem a single neuron cannot solve, but a network with one hidden layer can. Train a network with 2 inputs, 2 hidden neurons and 1 output, all sigmoid (σ(z) = 1 / (1 + e^(-z))), using the loss L = (y - target)².

The training data, always used in this order in every epoch:

inputtarget
[0, 0]0
[0, 1]1
[1, 0]1
[1, 1]0

For each example:

  1. Forward: a_j = σ(x₁ × hidden_weights[j][0] + x₂ × hidden_weights[j][1] + hidden_biases[j]), y = σ(a₁ × output_weights[0] + a₂ × output_weights[1] + output_bias)
  2. delta_out = 2 × (y - target) × y × (1 - y), and delta_j = delta_out × output_weights[j] × a_j × (1 - a_j), using the weights before this example's update
  3. Update every parameter with learning rate lr: output_weights[j] -= lr × delta_out × a_j, output_bias -= lr × delta_out, hidden_weights[j][i] -= lr × delta_j × x_i, hidden_biases[j] -= lr × delta_j

Implement train_xor(hidden_weights, hidden_biases, output_weights, output_bias, learning_rate, epochs).

Output

After training, return the network's predictions y for the inputs [0, 0], [0, 1], [1, 0] and [1, 1], in that order.

제약 조건

0 ≤ epochs ≤ 5,000

0 < learning_rate ≤ 5

-2 ≤ each starting weight and bias ≤ 2

예시

예시 1
입력:
epochs = 0
output_bias = 0.0
hidden_biases = [0.0, 0.3]
learning_rate = 1.5
hidden_weights = [
    [0.2, 0.8],
    [0.9, -0.7]
]
output_weights = [-0.6, 0.4]

예상 출력:
expected = [0.482451, 0.436972, 0.494378, 0.452729]
입력:
long long epochs = 0;
double output_bias = 0.0;
std::vector<double> hidden_biases = {0.0, 0.3};
double learning_rate = 1.5;
std::vector<std::vector<double>> hidden_weights = {
    {0.2, 0.8},
    {0.9, -0.7}
};
std::vector<double> output_weights = {-0.6, 0.4};

예상 출력:
std::vector<double> expected = {0.482451, 0.436972, 0.494378, 0.452729};
입력:
long epochs = 0;
double output_bias = 0.0;
double[] hidden_biases = new double[] {0.0, 0.3};
double learning_rate = 1.5;
double[][] hidden_weights = new double[][] {
    new double[] {0.2, 0.8},
    new double[] {0.9, -0.7}
};
double[] output_weights = new double[] {-0.6, 0.4};

예상 출력:
double[] expected = new double[] {0.482451, 0.436972, 0.494378, 0.452729};

설명

With 0 epochs nothing is trained, so the result is just the forward pass with the starting weights: about [0.482, 0.437, 0.494, 0.453]. The untrained network gives every input nearly the same answer.

예시 2
입력:
epochs = 2000
output_bias = 0.0
hidden_biases = [0.0, 0.3]
learning_rate = 1.5
hidden_weights = [
    [0.2, 0.8],
    [0.9, -0.7]
]
output_weights = [-0.6, 0.4]

예상 출력:
expected = [0.015727, 0.982119, 0.985515, 0.013682]
입력:
long long epochs = 2000;
double output_bias = 0.0;
std::vector<double> hidden_biases = {0.0, 0.3};
double learning_rate = 1.5;
std::vector<std::vector<double>> hidden_weights = {
    {0.2, 0.8},
    {0.9, -0.7}
};
std::vector<double> output_weights = {-0.6, 0.4};

예상 출력:
std::vector<double> expected = {0.015727, 0.982119, 0.985515, 0.013682};
입력:
long epochs = 2000;
double output_bias = 0.0;
double[] hidden_biases = new double[] {0.0, 0.3};
double learning_rate = 1.5;
double[][] hidden_weights = new double[][] {
    new double[] {0.2, 0.8},
    new double[] {0.9, -0.7}
};
double[] output_weights = new double[] {-0.6, 0.4};

예상 출력:
double[] expected = new double[] {0.015727, 0.982119, 0.985515, 0.013682};

설명

After 2,000 epochs the predictions are about [0.016, 0.982, 0.986, 0.014]: low for [0, 0] and [1, 1], high for [0, 1] and [1, 0]. The network has learned XOR.

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.