Task: Perceptron





Java: import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class Perceptron extends JPanel {

    class Trainer {
        double[] inputs;
        int answer;

        Trainer(double x, double y, int a) {
            inputs = new double[]{x, y, 1};
            answer = a;
        }
    }

    Trainer[] training = new Trainer[2000];
    double[] weights;
    double c = 0.00001;
    int count;

    public Perceptron(int n) {
        Random r = new Random();
        Dimension dim = new Dimension(640, 360);
        setPreferredSize(dim);
        setBackground(Color.white);

        weights = new double[n];
        for (int i = 0; i < weights.length; i++) {
            weights[i] = r.nextDouble() * 2 - 1;
        }

        for (int i = 0; i < training.length; i++) {
            double x = r.nextDouble() * dim.width;
            double y = r.nextDouble() * dim.height;

            int answer = y < f(x) ? -1 : 1;

            training[i] = new Trainer(x, y, answer);
        }

        new Timer(10, (ActionEvent e) -> {
            repaint();
        }).start();
    }

    private double f(double x) {
        return x * 0.7 + 40;
    }

    int feedForward(double[] inputs) {
        assert inputs.length == weights.length : "weights and input length mismatch";

        double sum = 0;
        for (int i = 0; i < weights.length; i++) {
            sum += inputs[i] * weights[i];
        }
        return activate(sum);
    }

    int activate(double s) {
        return s > 0 ? 1 : -1;
    }

    void train(double[] inputs, int desired) {
        int guess = feedForward(inputs);
        double error = desired - guess;
        for (int i = 0; i < weights.length; i++) {
            weights[i] += c * error * inputs[i];
        }
    }

    @Override
    public void paintComponent(Graphics gg) {
        super.paintComponent(gg);
        Graphics2D g = (Graphics2D) gg;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        
        int x = getWidth();
        int y = (int) f(x);
        g.setStroke(new BasicStroke(2));
        g.setColor(Color.orange);
        g.drawLine(0, (int) f(0), x, y);

        train(training[count].inputs, training[count].answer);
        count = (count + 1) % training.length;

        g.setStroke(new BasicStroke(1));
        g.setColor(Color.black);
        for (int i = 0; i < count; i++) {
            int guess = feedForward(training[i].inputs);

            x = (int) training[i].inputs[0] - 4;
            y = (int) training[i].inputs[1] - 4;

            if (guess > 0)
                g.drawOval(x, y, 8, 8);
            else
                g.fillOval(x, y, 8, 8);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("Perceptron");
            f.setResizable(false);
            f.add(new Perceptron(3), BorderLayout.CENTER);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}



Python: import random

TRAINING_LENGTH = 2000

class Perceptron:
    
    def __init__(self,n):
        self.c = .01
        self.weights = [random.uniform(-1.0, 1.0) for _ in range(n)]

    def feed_forward(self, inputs):
        vars = []
        for i in range(len(inputs)):
            vars.append(inputs[i] * self.weights[i])
        return self.activate(sum(vars))

    def activate(self, value):
        return 1 if value > 0 else -1

    def train(self, inputs, desired):
        guess = self.feed_forward(inputs)
        error = desired - guess
        for i in range(len(inputs)):
            self.weights[i] += self.c * error * inputs[i]
        
class Trainer():
    
    def __init__(self, x, y, a):
        self.inputs = [x, y, 1]
        self.answer = a

def F(x):
    return 2 * x + 1

if __name__ == "__main__":
    ptron = Perceptron(3)
    training = []
    for i in range(TRAINING_LENGTH):
        x = random.uniform(-10,10)
        y = random.uniform(-10,10)
        answer = 1
        if y < F(x): answer = -1
        training.append(Trainer(x,y,answer))
    result = []
    for y in range(-10,10):
        temp = []
        for x in range(-10,10):
            if ptron.feed_forward([x,y,1]) == 1:
                temp.append('^')
            else:
                temp.append('.')
        result.append(temp)
    
    print('Untrained')
    for row in result:
        print(''.join(v for v in row))

    for t in training:
        ptron.train(t.inputs, t.answer)
    
    result = []
    for y in range(-10,10):
        temp = []
        for x in range(-10,10):
            if ptron.feed_forward([x,y,1]) == 1:
                temp.append('^')
            else:
                temp.append('.')
        result.append(temp)
    
    print('Trained')
    for row in result:
        print(''.join(v for v in row))