In: Computer Science
Write a function softMax(x) to return a 1darray of softmax function values ??=???−?∑?−1?=0???−?fi=exi−m∑j=0K−1exj−m for ?=0,2,⋯,?−1i=0,2,⋯,K−1, where ?x is a 1darray of ?K floats, ??xi is the (?+1)(i+1)-th element of array ?x, and ?m is the largest element in array ?x. Sample: if z = np.array([10, 5, 8]), then softMax(z) returns array([0.876, 0.006, 0.118]).
in python program and the output should be array([0.876, 0.006, 0.118])
#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)
#test input
#1-d numpy array
z = np.array([10, 5, 8])
#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)
[0.876 0.006 0.118]
CODE and OUTPUT
So if you have any dount regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.