Challenge
Train a single perceptron with 2 inputs and 1 output.
Step 1: Initialize the weights
Step 2: Calculate the output
For inputs: [i1, i2, ..., in] and weights: [w1, w2, ..., wn] the output is:
i1 * w1 + i2 * w2 + ... + in * wn
Step 3: Apply activation function on the output (i.e sigmoid)
Step 4: Update the weights
w(t + 1) = w(t) + r * (desired_output - actual_output)
Where r: learning rate
Step 5 Repeat steps 2, 3 and 4
Input
iterations: how many times you repeat steps 2, 3 and 4
input: a list with 2 input values i.e. [1, 0]
output: the desired output
learning_rate: the learning rate i.e.0.3
Output
It should print the last calculated output. Keep in mind this should be very close to the desired output i.e 0.96564545
for desired output 1
Example
For input (training for XOR):
1000, [1 0], 1, 0.3
The output should be:
0.9966304251639512
Note The output will never be the same even for identical test cases due to random weights initialization.
Here's some non-golfed code in Python for this test case:
Rules
- The inputs and outputs of the perceptron are fixed to: 2 and 1 respectively.
- The output needs to be close to the desired output (see example).
- You can use any activation function you want, just mention it.