def sample(a=None, temperature=<NUM_LIT:1.0>):
if a is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>b = np.copy(a)<EOL>try:<EOL><INDENT>if temperature == <NUM_LIT:1>:<EOL><INDENT>return np.argmax(np.random.multinomial(<NUM_LIT:1>, a, <NUM_LIT:1>))<EOL><DEDENT>if temperature is None:<EOL><INDENT>return np.argmax(a)<EOL><DEDENT>else:<EOL><INDENT>a = np.log(a) / temperature<EOL>a = np.exp(a) / np.sum(np.exp(a))<EOL>return np.argmax(np.random.multinomial(<NUM_LIT:1>, a, <NUM_LIT:1>))<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>message = "<STR_LIT>"<EOL>warnings.warn(message, Warning)<EOL>return np.argmax(np.random.multinomial(<NUM_LIT:1>, b, <NUM_LIT:1>))<EOL><DEDENT>
Sample an index from a probability array.

    Parameters
    ----------
    a : list of float
        List of probabilities.
    temperature : float or None
        The higher the more uniform. When a = [0.1, 0.2, 0.7],
            - temperature = 0.7, the distribution will be sharpen [0.05048273,  0.13588945,  0.81362782]
            - temperature = 1.0, the distribution will be the same [0.1,    0.2,    0.7]
            - temperature = 1.5, the distribution will be filtered [0.16008435,  0.25411807,  0.58579758]
            - If None, it will be ``np.argmax(a)``

    Notes
    ------
    - No matter what is the temperature and input list, the sum of all probabilities will be one. Even if input list = [1, 100, 200], the sum of all probabilities will still be one.
    - For large vocabulary size, choice a higher temperature or ``tl.nlp.sample_top`` to avoid error.