Machine Learning
What is RL?
Reinforcement learning is a method where an agent learns by maximizing rewards.
What is reinforcement learning?
In reinforcement learning (RL) an agent learns by trial and error by interacting with an environment. There is no labelled correct answer; there is only a reward signal coming as a result of actions:
durum = ortam.reset()
while not bitti:
eylem = ajan.sec(durum)
durum, odul, bitti = ortam.adim(eylem)
ajan.ogren(durum, eylem, odul)
The goal is not the single-step reward but the total reward over time. Chess, robot control and fine-tuning language models with human feedback (RLHF) use RL.
A corridor environment
Throughout this group we'll use a 5-cell corridor: the agent starts at 0 and tries to reach 4 (the goal). Each step gives −0.1 reward (hurry), the goal gives +1 and the episode ends:
def adim(durum, eylem):
yeni = max(0, durum - 1) if eylem == SOL else durum + 1
if yeni == N - 1:
return yeni, 1.0, True
return yeni, -0.1, False
The Gym/Gymnasium library's env.step(action) interface returns (observation, reward, done, …) with the same logic.
Exercise Walk the corridor
Write a calistir(eylemler) function that runs an episode with the given adim function: start from 0, apply the actions in order, stop when the episode ends; return the visited states and the total reward. For [SAG, SAG, SAG, SAG] and [SAG, SOL, SAG, SAG, SAG, SAG], print the states and the total reward (round(..., 2)) on one line.
Show example solution
N = 5 # durumlar 0..4, hedef 4
SOL, SAG = 0, 1
def adim(durum, eylem):
yeni = max(0, durum - 1) if eylem == SOL else durum + 1
if yeni == N - 1:
return yeni, 1.0, True
return yeni, -0.1, False
def calistir(eylemler):
durum, toplam, yol = 0, 0.0, [0]
for e in eylemler:
durum, odul, bitti = adim(durum, e)
toplam += odul
yol.append(durum)
if bitti:
break
return yol, toplam
for dizi in [[SAG] * 4, [SAG, SOL, SAG, SAG, SAG, SAG]]:
yol, toplam = calistir(dizi)
print(yol, round(toplam, 2))
Test yourself
-
What separates RL from supervised learning?
- It can't use neural networks
- No correct answer is given; there is only a delayed reward signal
- No data is used at all
- It only does classification
Show answer
No correct answer is given; there is only a delayed reward signal — The agent must infer which action was good from the outcomes.
-
The environment step returns three things.
durum, odul, ____ = adim(durum, eylem)
Show answer
durum, odul, bitti = adim(durum, eylem)
-
Match.
- agent
- Chooses actions
- environment
- Gives state and reward
- reward
- Feedback signal
- episode
- One attempt from start to end
Next lesson: Agent