AI Beyond Language Models · Learning from Rewards
Nobody tells the robot the right moves. It only gets a reward at the goal, and over a few tries the value of that reward flows backwards along the corridor.
# A robot in a corridor: cell 0, 1, 2, then the goal. Reaching the goal is worth 10.
# It learns how good "move right" is from each cell, by trying it.
cells = ["cell 0", "cell 1", "cell 2", "goal"]
values = [0.0, 0.0, 0.0, 0.0]
alpha, gamma = 0.5, 0.9 # learning rate, and how much a future reward counts
for episode in range(3):
cell = 0
while cell < 3:
values[cell] = round(values[cell] + alpha * ((10 if cell == 2 else 0) + gamma * values[cell + 1] - values[cell]), 2)
cell += 1
print("episode", episode, values)cell < 3 to decide whether the loop keeps going.cell changes from 0 to 1.cell < 3 to decide whether the loop keeps going.values[cell] = round(values[cell] + alpha * ((10 if cell == 2 else 0) + gamma * values[cell + 1] - values[cell]), 2).cell changes from 1 to 2.cell < 3 to decide whether the loop keeps going.cell changes from 2 to 3.cell < 3 to decide whether the loop keeps going.episode 0 [0.0, 0.0, 5.0, 0.0].cell changes from 3 to 0.cell < 3 to decide whether the loop keeps going.values[cell] = round(values[cell] + alpha * ((10 if cell == 2 else 0) + gamma * values[cell + 1] - values[cell]), 2).cell changes from 0 to 1.cell < 3 to decide whether the loop keeps going.cell changes from 1 to 2.cell < 3 to decide whether the loop keeps going.cell changes from 2 to 3.cell < 3 to decide whether the loop keeps going.episode 1 [0.0, 2.25, 7.5, 0.0].cell changes from 3 to 0.cell < 3 to decide whether the loop keeps going.cell changes from 0 to 1.cell < 3 to decide whether the loop keeps going.cell changes from 1 to 2.cell < 3 to decide whether the loop keeps going.cell changes from 2 to 3.cell < 3 to decide whether the loop keeps going.episode 2 [1.01, 4.5, 8.75, 0.0].