np:带broadcasting的softmax

转:https://stackoverflow.com/questions/34968722/how-to-implement-the-softmax-function-in-python

# my (correct) solution:
def softmax(z):
    assert len(z.shape) == 2
    s = np.max(z, axis=1)
    s = s[:, np.newaxis] # necessary step to do broadcasting
    e_x = np.exp(z - s)
    div = np.sum(e_x, axis=1)
    div = div[:, np.newaxis] # dito
    return e_x / div

你可能感兴趣的:(numpy)