AI Perceptrons
Train a linear threshold unit on a small separable task.
How it works
A perceptron combines weighted inputs and a bias, then applies a threshold. Its update rule changes weights when a training label is misclassified. A single perceptron cannot represent every pattern: XOR is not linearly separable. This bounded training loop learns the two-input AND relation, not a realistic recognition task.
Run the code
Try in playground loads this complete standard-library Python example without executing it. Choose Run when ready. Local review uses Python 3.12.14; the existing browser editor uses Pyodide 0.27.7. These small fictional examples need no account, external data, model-provider key or database service.
Example
examples = [([0,0],0),([0,1],0),([1,0],0),([1,1],1)]
weights, bias = [0.0,0.0], 0.0
for epoch in range(20):
errors = 0
for x,target in examples:
predicted = int(sum(w*v for w,v in zip(weights,x,strict=True))+bias >= 0)
error = target-predicted
if error:
weights = [w+error*v for w,v in zip(weights,x,strict=True)]
bias += error
errors += 1
if errors == 0:
break
print([int(sum(w*v for w,v in zip(weights,x,strict=True))+bias >= 0) for x,_ in examples])
Expected result
[0, 0, 0, 1]
The epoch limit prevents an unbounded loop. Convergence on these four separable points checks the update implementation. It does not establish that the same unit can learn arbitrary images or that every dataset is separable.