Reinforcement Learning
Reinforcement Learning: Deep Q-Network (DQN)
2023-12-29
When MDP is known, state value functions are obtained using DP, meanwhile when MDP is unknown, state value functions are obtained based on experience using MC or TD. So far, the state value functions are based on a lookup table so that the value q(s,a) for the state and action can be pulled easily.
However, if the problem is very large or if the state or action is a continuous function, the lookup table is no longer suitable to handle it, and in this case, q(s, a) can be approximated as a continuous function of s and a, where artificial neural networks play the role. That is the DQN concept.
When approximating q(s,a), the q-table from Q-learning will serve as the training dataset, and the following considerations should be taken into account.
(1) Cost function
The TD target used in Q-learning is the expected function for the action in that state, so the cost function is defined as follows.
\(Loss=\sum _{episode}^{\ }(\text{TDtarget}(s,a)-\hat{q}(s,a))^2\)
where \(\text{TD target}(s,a)=r'+\gamma \max _{a'}^{ } q(a')\)
\(\hat{q}(s,a)\) is the predicted action value function at the state, s and action, a by the neural network
The total Loss for the optimization is the sum of all the error across the episodes. Note that Q-learning already provides the TD target as a function of the state s and action, a. Our goal is to replace the lookup table with a neural network.
(2) Activation functions for the neural network
The activation function for the hidden layer is the hyperbolic tangent, which includes both + and - values, and for the output layer, a linear function is used as the activation function as it includes all numbers between -∞ and +∞.
(3) Structure of the neural network
The example being adapted for DQN learning is the following 3x3 Grid World problem. For each episode, an agent starts at 0 and ends at 8 to complete.

At first, run Q-learning algorithm as discussed in the blog to generate TD target as a function of the state, s and action a.

Update the next state (s') according to the action (a) and the current state (s) as follows:
- action=1, (East) s' = if(mod(s,3)=3, s, s+1)
- action=2, (West) s' = if(mod(s,3)=0, s, s-1)
- action=3, (North) s' = if(floor(s/3,1)=0, s, s-3)
- action=4, (South) s' = if(floor(s/6,1)=0, s+3, s)
After learning through enough episodes, the action value function q(s,a) gets converged as shown in the table below.


The entities necessary for the training of DQN are s, a, and TD target. In the current Excel worksheet, the TD target is structured in the s' column to set the TD target for s, so adjustment is needed upwards row by row. After that, the following neural network is used.

The 8 nodes in the input layer represent the one-hot encoded state (s), and the 4 output nodes in the output layer represent the action value q(s,a) for each action (a), comparing the q(s,a*) of the action a* corresponding to the TD target to calculate the error.
The prediction y' by the neural network as discussed in the NN blog is presented as a series of the inner products between the coefficients, w, V, b, and bv and the output from the nodes of each layers.
\(y'=\phi_v\lbrace V_j\phi_j(X_iw_{ij}-b_j)-b_v\rbrace \)
(3.1) Input layer for X
The each node of the input layers represents the position element of the state which is one-hot encoded as below
S0 = 1 0 0 0 0 0 0 0
S1 = 0 1 0 0 0 0 0 0
...
S7 = 0 0 0 0 0 0 0 1

(3.2) Hidden layer
The coefficient matrix of the hidden layer constitutes with the weights for the 8 input node by the 8 hidden node plus the 8 thresholds for the hidden node, which needs total 64+8 cell allotment as shown below.

For the forward propagation procedure, the sum of the hidden layer is
\(X_iw_{ij}-b_j\) which is computed by the MMULT(input nodes, weights of the hidden layer) - biases of the hidden layer.

The output of the hidden layer below uses the hyperbolic tangent as the activation function.
\(\phi_j(X_iw_{ij}-b_j)\) = \(\tanh (X_iw_{ij}-b_j)\)

(3.3) Output layer
The output layer needs the weights for the 8 hidden layer node multiplied by the 4 output layer node and additional 4 biases for the output node, which becomes total 32+4 coefficients.

The sum of the output layer
\(V_j\phi_j(X_iw_{ij}-b_j)-b_v\)
is computed in Excel by the MMULT(outputs of the hidden layer, weights of the output layer) - biases of the output layer. And since the output layer uses the linear function as the activation function, the output of the output layer equals to the sum of the output layer.

(3.4) Cost function
The TD Target obtained during Q-learning is the measurements and the neural network output as the action value function qθ(s,a*) corresponding the action a* for the TD Target become the prediction.

The sum square error of the two values is the objective for the optimization. In Excel, the SUMXMY2(TD Target, Qθ(s,a*)) function will report the total cost.

(3.5) Run the Solver
Run the Solver to find the optimal coefficients. Note that since the coefficients are no longer limited to positive value, make sure the Non-negative box must be unchecked.

After the completion of the optimization, the predicted qθ(s,a) from the neural network is tabulated as follows. The Agent will choose the action at each state to produce most expected return which a is highlighted in green per state, and the results are summarized at the bottom table.

| S | S | S |
| S | E | S |
| E | E | Terminal State |