concept state in category deep learning

appears as: state, The state, states, state, A state, States
Deep Reinforcement Learning in Action

This is an excerpt from Manning's book Deep Reinforcement Learning in Action.

  • The state is a snapshot of the environment that the agent has access to and uses to make decisions. The environment is often a set of constantly changing conditions, but we can sample from the environment, and these samples at particular times are the state information of the environment we give to the agent.
  • A state in a game (or in a reinforcement learning problem more generally) is the set of information available in the environment that can be used to make decisions.

    Figure 2.6. A computational graph for a simple 10-armed contextual bandit. The get_state() function returns a state value, which is transformed into a one-hot vector that becomes the input data for a two-layer neural network. The output of the neural network is the predicted reward for each possible action, which is a dense vector that is run through a softmax to sample an action from the resulting probability distribution over the actions. The chosen action will return a reward and updates the state of the environment. θ1 and θ2 represent the weight parameters for each layer. The , , and symbols denote the natural numbers (0, 1, 2, 3, ...), the real numbers (a floating-point number, for our purposes), and a probability, respectively. The superscript indicates the length of the vector, so 10 represents a 10-element vector where each element is a probability (such that all the elements sum to 1).

    Initially our neural network will produce a random vector such as [1.4, 50, 4.3, 0.31, 0.43, 11, 121, 90, 8.9, 1.1] when in state 0. We will run softmax over this vector and sample an action, most likely action 6 (from actions 0 through 9), since that is the biggest number in the example vector. Choosing action 6 will generate a reward of say 8. Then we train our neural network to produce the vector [1.4, 50, 4.3, 0.31, 0.43, 11, 8, 90, 8.9, 1.1], since that is the true reward we received for action 6, leaving the rest of the values unchanged. The next time when the neural network sees state 0, it will produce a reward prediction for action 6 closer to 8. As we continually do this over many states and actions, the neural network will eventually learn to predict accurate rewards for each action given a state. Thus, our algorithm will be able to choose the best action each time, maximizing our rewards.

    We’ll run the softmax function over the model’s output to generate a probability distribution over the actions. We’ll then select an action using the environment’s choose_arm(...) function, which will return the reward generated for taking that action; it will also update the environment’s current state. We’ll turn the reward (which is a non-negative integer) into a one-hot vector that we can use as our training data. We’ll then run one step of backpropagation with this reward vector, given the state we gave the model. Since we’re using a neural network model as our action-value function, we no longer have any sort of action-value array storing “memories;” everything is being encoded in the neural network’s weight parameters. The whole train function is shown in the following listing.

    Listing 2.10. The main training loop
    def train(env, epochs=5000, learning_rate=1e-2):
        cur_state = torch.Tensor(one_hot(arms,env.get_state()))             #1
        optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
        rewards = []
        for i in range(epochs):
            y_pred = model(cur_state)                                       #2
            av_softmax = softmax(y_pred.data.numpy(), tau=2.0)              #3
            av_softmax /= av_softmax.sum()                                  #4
            choice = np.random.choice(arms, p=av_softmax)                   #5
            cur_reward = env.choose_arm(choice)                             #6
            one_hot_reward = y_pred.data.numpy().copy()                     #7
            one_hot_reward[choice] = cur_reward                             #8
            reward = torch.Tensor(one_hot_reward)
            rewards.append(cur_reward)
            loss = loss_fn(y_pred, reward)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            cur_state = torch.Tensor(one_hot(arms,env.get_state()))         #9
        return np.array(rewards)

    Go ahead and run this function. When we train this network for 5,000 epochs, we can plot the moving average of rewards earned over the training time (see figure 2.7; we omitted the code to produce such a graph). Our neural network indeed learns a fairly good understanding of the relationship between states, actions and rewards for this contextual bandit. The maximum reward payout for any play is 10, and our average is topping off around 8.5, which is close to the mathematical optimum for this particular bandit. Our first deep reinforcement learning algorithm works! Okay, it’s not a very deep network, but still!

    Value functions are functions that map a state or a state-action pair to the expected value (the expected reward) of being in some state or taking some action in some state. You may recall from statistics that the expected reward is just the long-term average of rewards received after being in some state or taking some action. When we speak of the value function, we usually mean a state-value function.

    Table 2.7. The state-value function (view table figure)

    Math

    English

    Vπ: s → E(R|s,π), A value function, Vπ, is a function that maps a state, s, to the expected rewards, given that we start in state s and follow some policy, π.

    This is a function that accepts a state, s, and returns the expected reward of starting in that state and taking actions according to our policy, π. It may not be immediately obvious why the value function depends on the policy. Consider that in our contextual bandit problem, if our policy was to choose entirely random actions (i.e., sample actions from a uniform distribution), the value (expected reward) of a state would probably be pretty low, since we’re definitely not choosing the best possible actions. Instead, we want to use a policy that is not a uniform distribution over the actions, but is the probability distribution that would produce the maximum rewards when sampled. That is, the policy is what determines observed rewards, and the value function is a reflection of observed rewards.