Broadcasting and the softmax function in Python

mac2022-07-05  28

def softmax(x): x_exp = np.exp(x) x_sum = np.sum(x_exp,axis=1,keepdims=True) s = x_exp/x_sum return s x = np.array([ [9, 2, 5, 0, 0], [7, 5, 0, 0 ,0]]) print("softmax(x) = " + str(softmax(x)))

OUT:

softmax(x) = [[ 9.80897665e-01 8.94462891e-04 1.79657674e-02 1.21052389e-04 1.21052389e-04] [ 8.78679856e-01 1.18916387e-01 8.01252314e-04 8.01252314e-04 8.01252314e-04]]
最新回复(0)