How AI Language Models Work · Training a Language Model
A pre-trained model only continues text. To make an assistant, people compare answers and a reward model learns what they prefer. That reward then steers the chat model.
import math
# A pre-trained model just continues text. To make it helpful, people compare two answers,
# and a reward model learns to score answers the way they would.
answers = ["Paris is the capital of France.", "idk lol"]
features = [[1.0, 1.0], [0.0, 0.0]] # [polite, answers the question]
weights = [0.0, 0.0] # the reward model starts knowing nothing
lr = 0.5 # a person preferred the first answer
for epoch in range(3):
rewards = [round(weights[0] * f[0] + weights[1] * f[1], 2) for f in features]
agree = round(math.exp(rewards[0]) / (math.exp(rewards[0]) + math.exp(rewards[1])), 2)
weights = [round(w + lr * (1 - agree) * (features[0][i] - features[1][i]), 2) for i, w in enumerate(weights)]
print("epoch", epoch, "rewards", rewards, "agrees with the person:", agree)
best = answers[rewards.index(max(rewards))]
print("the chat model is then trained towards answers like:", best)math module, for functions like math.exp and math.sqrt.epoch is created with the value 0.epoch 0 rewards [0.0, 0.0] agrees with the person: 0.5.epoch changes from 0 to 1.weights now points to a different list: [0.44, 0.44].epoch 1 rewards [0.5, 0.0] agrees with the person: 0.62.epoch changes from 1 to 2.weights now points to a different list: [0.58, 0.58].epoch 2 rewards [0.88, 0.0] agrees with the person: 0.71.for epoch in range(3).