def GeneralizedAdvantageEstimator(critic, state_batch, reward, gamma, lamb):
    """
    Compute the Generalized Advantage Estimator.

    See https://danieltakeshi.github.io/2017/04/02/notes-on-the-generalized-advantage-estimation-paper/

    :param critic: Critic network.
    :param state_batch: A numpy array of batched input that can be feed into the critic network directly. Note that it must also contain the ending state, i.e. has batch_size + 1 entries.
    :param reward: numpy array of shape (batch_size,)
    :param gamma: Tuning parameter gamma.
    :param lamb: Tuning parameter lambda.
    :return: Python List of GAE values in ascending time order (matched with parameter reward)
    """
    n = len(state_batch) - 1
    assert reward.shape == (n,)
    value = critic.predict_on_batch(state_batch).flatten()
    delta = reward + gamma * value[1:, ] - value[:-1, ]
    # No premature optimization for now...
    result = [0]
    r = gamma * lamb
    for i in range(0, n):
        result.append(result[-1] * r + delta[n-1-i])
    return result[:0:-1]