PES
EN English 한국어 日本語 中文 हिन्दी বাংলা اردو Español Português Français Deutsch Русский العربية Bahasa Indonesia Tiếng Việt ไทย Türkçe

How to enable notifications

Notifications are unavailable in this browser or on this address. Try a supported browser on the secure PES site.

If notifications are blocked, allow them in your browser settings.

  1. Open the site controls beside the address bar, or your browser’s site settings. Set Notifications for this site to Allow.
  2. In your device settings, allow notifications for your browser and check Do Not Disturb or Focus.
  3. Return to PES, reload the page, and use the bell to enable notifications.

On iPhone or iPad, add PES to your Home Screen, open it there, then allow notifications.

My learning progress
  1. How to use AI
  2. Terminal commands
  3. Advanced AI
  4. Vibe coding
  5. AI certification Getting ready
  6. Keyboard
See details
Hands-on AI development

Build an AI that learns from examples and corrects itself

Build a program that looks at example data and, every time its prediction is wrong, fixes the number it calculates with — by itself. It is a single Python file: no extra libraries and no paid AI service. You can also run it right here on this page.

The data to learn

This is the data our model will learn from.

InputAnswer
12
24
36
48

We can see the rule at a glance: "twice the input". Let's have the program learn that relationship from the data too. When is finished, we check its predictions with 5 and 7 — inputs it never saw while learning.

The shape of the calculation — the model

First we give the program the shape of its calculation — in other words, the .

prediction = input × w

w is the number the program changes while it learns. A value like this is called a — in this case, a .

We start with w = 0.

input 3 → prediction 0 → the answer is 6 → a big difference!

As training repeats and w gets close to 2, it looks like this:

input 3 → prediction about 6 → almost the same as the answer!

People prepare the shape of the calculation and the example data; the program finds the w that fits the data.

The code — mini_ai.py

Save the code below as mini_ai.py. On this page, press [Run] and the Python inside your browser runs it straight away. You can change the code and run it again.

Press [Run] to get the browser Python ready. The first time, it downloads about 12 MB.

# 1. Training data: (input, answer)
data = [(1, 2), (2, 4), (3, 6), (4, 8)]

# 2. Start without knowing the rule.
weight = 0.0
learning_rate = 0.01

# 3. Look at all the data and train 100 times.
for step in range(100):
    gradient = 0.0

    for x, answer in data:
        prediction = weight * x       # predict with the current weight
        error = prediction - answer   # how far from the answer
        gradient += 2 * error * x     # direction and size of the fix

    # Gather the results of all the data and change the weight a little.
    weight -= learning_rate * gradient / len(data)

    if step + 1 in [1, 10, 50, 100]:
        print(f"Training {step + 1}: w = {weight:.4f}")

# 4. Predict inputs that were not used for training.
print(f"Prediction for input 5: {weight * 5:.4f}")
print(f"Prediction for input 7: {weight * 7:.4f}")

Output

Press [Run] and the result will appear here.

Run it on your own computer

In the folder where you saved the file, run it with this command:

python3 mini_ai.py

The result

This is the result we got by actually running it. Numbers are shown to four decimal places.

Training 1: w = 0.3000
Training 10: w = 1.6063
Training 50: w = 1.9994
Training 100: w = 2.0000
Prediction for input 5: 10.0000
Prediction for input 7: 14.0000

The started at 0 and became about 2 through . Using this weight to calculate the answer for a new input is called prediction, or .

Three key ideas in the code

error How wrong was it?
The prediction minus the answer. During , the is adjusted so that the average of these errors squared gets smaller.
gradient Which way should the weight change?
It calculates the direction and size of the change needed to reduce the error. This method is called gradient descent. 2 * error * x is what you get by differentiating this 's squared error. You can experiment before you fully understand differentiation.
learning_rate How much to change at a time
Here it is 0.01. Too small and learning is slow; too large and the can swing wildly or learning can fail.

Experiment for yourself

Change one thing at a time and run it again. Press [Load and run] to put the changed code into the editor above and run it right away.

  1. Change the data to "three times"

    data = [(1, 3), (2, 6), (3, 9), (4, 12)]

    Leave the rest of the code as it is and run it. This time, check that the weight is learned as about 3.

    Show the result we got
    Training 1: w = 0.4500
    Training 10: w = 2.4094
    Training 50: w = 2.9991
    Training 100: w = 3.0000
    Prediction for input 5: 15.0000
    Prediction for input 7: 21.0000
  2. Train fewer times

    for step in range(10):

    Compare how much less accurate the predictions are than after 100 rounds of training.

    Show the result we got
    Training 1: w = 0.3000
    Training 10: w = 1.6063
    Prediction for input 5: 8.0313
    Prediction for input 7: 11.2438
  3. Take away the perfect rule

    data = [(1, 2.1), (2, 3.9), (3, 6.2), (4, 7.8)]

    Even when the data is a little off, see whether it finds a weight that fits well overall.

    Show the result we got
    Training 1: w = 0.2985
    Training 10: w = 1.5982
    Training 50: w = 1.9894
    Training 100: w = 1.9900
    Prediction for input 5: 9.9500
    Prediction for input 7: 13.9300

Next challenge — input × w + b

This can only express a proportional relationship of the form input × w. To learn input × 2 + 3, for example, you extend the model to input × w + b and learn two numbers, w and b. b is called the bias.

Once you understand the experiments above, try changing the code yourself first. If you get stuck, open the solution below.

Show the solution

The training data is (1, 5), (2, 7), (3, 9), (4, 11), made with input × 2 + 3. Because it learns two numbers, we raise the learning rate to 0.05 and train 1000 times.

# 1. Training data: (input, answer) made with input × 2 + 3
data = [(1, 5), (2, 7), (3, 9), (4, 11)]

# 2. Start both the weight w and the bias b at 0.
weight = 0.0
bias = 0.0
learning_rate = 0.05

# 3. Look at all the data and train 1000 times.
for step in range(1000):
    grad_w = 0.0
    grad_b = 0.0

    for x, answer in data:
        prediction = weight * x + bias    # predict with the current w and b
        error = prediction - answer       # how far from the answer
        grad_w += 2 * error * x           # direction and size to change w
        grad_b += 2 * error               # direction and size to change b

    # Change w and b a little, together.
    weight -= learning_rate * grad_w / len(data)
    bias -= learning_rate * grad_b / len(data)

    if step + 1 in [1, 10, 100, 500, 1000]:
        print(f"Training {step + 1}: w = {weight:.4f}, b = {bias:.4f}")

# 4. Predict inputs that were not used for training.
print(f"Prediction for input 5: {weight * 5 + bias:.4f}")
print(f"Prediction for input 7: {weight * 7 + bias:.4f}")
Training 1: w = 2.2500, b = 0.8000
Training 10: w = 2.6082, b = 1.2119
Training 100: w = 2.1565, b = 2.5399
Training 500: w = 2.0004, b = 2.9989
Training 1000: w = 2.0000, b = 3.0000
Prediction for input 5: 13.0000
Prediction for input 7: 17.0000

w first goes past 2, then comes back to 2 as b grows, ending at w = 2 and b = 3.

Guidelines

AI

AI Certification

Study

Vibe Coding

Game

Practice

Tutors

Chat

Forum

Create

Community

More

Glossary

Pick a word you don't know. You'll see a simple explanation and related terms.

No matching term yet.

Pick a term to see its explanation.