What is RL?
- Machine Learning course
- Expert
- Lesson 35/44
- 5 min
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, FalseThe Gym/Gymnasium library's env.step(action) interface returns (observation, reward, done, …) with the same logic.
Practice 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.
Starter code
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
# buraya yazSample 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))Expected output
[0, 1, 2, 3, 4] 0.7 [0, 1, 0, 1, 2, 3, 4] 0.5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What separates RL from supervised learning?
- It can't use neural networks
- No correct answer is given; there is only a delayed reward signal Correct answer
- No data is used at all
- It only does classification
Explanation: The agent must infer which action was good from the outcomes.
2 The environment step returns three things.
durum, odul, ____ = adim(durum, eylem)
durum, odul, bitti = adim(durum, eylem)
Explanation: The new state, the reward and whether the episode is over.
3 Match.
| Term | Meaning |
|---|---|
agent |
Chooses actions |
environment |
Gives state and reward |
reward |
Feedback signal |
episode |
One attempt from start to end |