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


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


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


For input (training for XOR):

1000, [1 0], 1, 0.3

The output should be:


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:

Try it Online!


  1. The inputs and outputs of the perceptron are fixed to: 2 and 1 respectively.
  2. The output needs to be close to the desired output (see example).
  3. You can use any activation function you want, just mention it.
  • 2
    \$\begingroup\$ To make the challenge self-contained, can you give examples of activation functions and list the constraints such a function must follow? \$\endgroup\$
    – JayCe
    Aug 27, 2018 at 13:40
  • 4
    \$\begingroup\$ It seems that the identity function is an activation function according to Wikipedia; I suspect it would be golfiest to just use that. \$\endgroup\$
    – Giuseppe
    Aug 27, 2018 at 13:43
  • 1
    \$\begingroup\$ Also the constraint "should be very close" is too vague IMO... maybe restrict to a list of activation functions? and I was about to say the same thing as @Giuseppe... \$\endgroup\$
    – JayCe
    Aug 27, 2018 at 13:44
  • 2
    \$\begingroup\$ Sorry everyone, this is my first attempt. \$\endgroup\$
    – DimChtz
    Aug 27, 2018 at 14:19
  • 1
    \$\begingroup\$ @DimChtz no worries! We typically suggest posting challenges in The Sandbox for a while so they can get some feedback. My suggestions for now (unless you want to delete this temporarily and try it there first) are to specify a list of activation functions we're allowed to use, and remove Rule #2 since the final output should be the result of the selected Activation function and the weights. \$\endgroup\$
    – Giuseppe
    Aug 27, 2018 at 14:21

2 Answers 2


JavaScript (Node.js), 121 110 bytes

Same as TFled. A "golfed" version of the example.

-11 bytes from @Arnauld


Try it online!

  • 2
    \$\begingroup\$ 110 bytes. Most bytes are saved by going recursive and splitting the 2nd argument right away. The with(Math)f= trick only saves 1 byte. \$\endgroup\$
    – Arnauld
    Aug 27, 2018 at 15:02

Python 2, 136 bytes

from random import*
import math
def f(n,(i,I),o,r):w,W=random(),random();exec"d=1/(1+math.exp(-i*w-I*W));c=r*(o-d);w+=c;W+=c;"*n;print d

Try it online!

Basically just a golfed version of the example


Not the answer you're looking for? Browse other questions tagged or ask your own question.