Reinforcement Learning and Language Model Alignment (VTR UGE 21), day 2 of 5
Prof. Dr. Utku Kose, Süleyman Demirel University
Part A repeats a calculation of the lecture step by step: Monte Carlo against temporal differences on one sentence. Part B computes one update of Q-learning and one of SARSA (state, action, reward, state, action) by hand. Part C classifies methods as on-policy or off-policy, temporal difference, Monte Carlo or dynamic programming. Part D repeats a second calculation of the lecture step by step: one update of a small Q-network. Part E explores the deadly triad on Baird's counterexample. Part F trains a thermostat that learns when to heat with Q-learning and schedules of your choice, and computes one update of its table by hand.
The worked example of the lecture: The agent writes the same sentence of four tokens in every episode, and only the last step is rewarded. Monte Carlo moves the four estimates towards the return after each episode, and temporal difference (TD) learning moves each estimate towards the reward plus the estimate of the next state. The sliders set the step size, the final reward and the number of episodes. Values are rounded to four decimals.
Back to the lecture: Learning without a model. The lecture continues there with Section 2 of the Colab notebook: Open it from the next orange box.
The current value is Q(s, a) = 2.0. The agent receives the reward -1 and lands in state s'. The best action in s' has the value 4.0, but the agent explores and will take an action whose value is 1.0. The discount factor is 0.9 and the step size 0.5.
Back to the lecture: SARSA and Q-learning. The lecture continues there with Section 3 of the Colab notebook: Open it from the next orange box.
Classify each method.
Back to the lecture: SARSA and Q-learning, where the text continues after the box of this part.
A network with three inputs, two hidden units and two outputs, the Q-values of two actions, is small enough to follow by hand. It has the same structure as the network of the deep Q-network (DQN) of the lecture: a layer with the rectified linear unit (ReLU), a second layer, the target of Q-learning, the Huber loss, the gradients of both layers by backpropagation and one update with the Adam optimiser. The learning rate is larger than the 0.003 of the experiment, so that the change can be seen. The weights are
| input 1 | input 2 | input 3 | bias | |
|---|---|---|---|---|
| hidden unit 1 | 0.5 | −0.3 | 0.8 | 0.1 |
| hidden unit 2 | −0.4 | 0.9 | 0.2 | 0.0 |
| hidden unit 1 | hidden unit 2 | bias | |
|---|---|---|---|
| Q of action 1 | 1.2 | −0.5 | 0.0 |
| Q of action 2 | 0.4 | 0.7 | 0.1 |
The first table is \(W_1\) with \(b_1\), the second \(W_2\) with \(b_2\). The state is written as a one-hot vector: a 1 at the position of the state and 0 elsewhere.
Back to the lecture: Deep Q-networks. The lecture continues there with Section 5 of the Colab notebook: Open it from the next orange box.
Baird's counterexample has seven states, a reward of zero everywhere and therefore a true value function of zero. Each switch below removes one leg of the deadly triad: function approximation, bootstrapping or off-policy sampling. Run every combination and find the only one that diverges.
| Run | Approximation | Bootstrapping | Off-policy | Step size | Final norm | Verdict |
|---|
Back to the lecture: Going further (optional). The lecture continues there with Section 7 of the Colab notebook: Open it from the next orange box.
The thermostat of the lecture decides every half hour from 15:00 to 23:00 whether to heat. In the simulated house, it is 17 °C at 15:00, and a half hour with the heating on warms the house by 2 °C. In every half hour, the house cools by 0.5 °C while it is colder than 18 °C and by 1 °C from 18 °C up. Wind blows in a half hour with the probability 0.3 and cools it by another 0.5 °C. Each half hour with the heating on earns the reward −1. From 18:00, when the family is at home, each degree below 20 °C at the end of a half hour adds −3 to the reward. Train the thermostat with Q-learning: Choose the step size, the exploration rate, their schedules and the number of evenings. The step size can stay constant or shrink as \(N(s, a)^{-0.7}\), where \(N(s, a)\) counts the updates of an entry. The exploration rate can stay constant or fall in a straight line to 0.01 over the evenings. Every training run uses the same random numbers, so two settings can be compared fairly. Find settings with which the learned thermostat costs at most 11 points per evening after at most 1000 evenings. The controls start with the settings of the lecture. Then answer the questions below by hand.
By hand: At 17:30 the house has 19 °C. The thermostat heats, and the wind blows during this half hour. The table holds Q(17:30, 19 °C, heat) = −8.0. For 18:00 and the temperature that follows, it holds −14.0 for off and −6.5 for heat. The step size is 0.2 and \(\gamma = 1\).
Back to the lecture: Going further (optional), where the text continues after the box of this part.
Answer each question, rate your confidence and check the answer. Results stay in this browser.
Write a short answer to each question. The text is saved in this browser and is included when you export the learning log.