In: Computer Science
Write a function softMax_M(Q) to return a 2darray of softmax function values ??ℎ=???ℎ−?∑??=1???ℎ−? for ?=0,1,⋯,?−1,ℎ=0,1,⋯,?−1 , where ? is a 2darray of floats with shape ( ? , ? ), ??ℎ is the element at the (?+1) -th row and the (ℎ+1) -th column of array ? , and ? is the largest element in array ? . (Hint: operations should be performed down the rows; a returned 2darray and ? are of the same shape.) Sample: if D = np.array([[10, 9], [5, 6], [8, 4]]), then softMax_M(D) returns array([[0.876, 0.946], [0.006, 0.047], [0.118, 0.006]]).
in python program
i just want the answer in python program
please please thumbs up!!!
hope it will help uh out!!!
Code::
(IN PYTHON PROGRAMMING LANGUAGE)
------------------------------------------------------------
#import numpy module
import numpy as np
#function which computes softmax function values for the given 1-d
numpy array z.
def softMax(z):
#computing using softmax function formula
return np.exp(z) / np.sum(np.exp(z), axis=0)
#1-d numpy array
z=np.array([[10, 9], [5, 6], [8, 4]])
#call softMax() and get the result
vec=softMax(z)
#Now vec contains the result but as expected in the problem the
result should be rounded till 3 decimal places
vec=np.around(vec, decimals=3)
#after rounding print
print(vec)
------------------------------------------------------------
output::