Question

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...

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])

Solutions

Expert Solution

  • Below is the detailed implementation of the above mentioned problem in python with code and it's output.
  • For better understanding read the comments mentioned in the code.
  • If x is our 1d numpy array then softmax(x[i]) is simply exponential of this x[i] divided by sum of exponentials of all x[i] where 1<=i<=n, where n is size of given 1-D numpy array.
  • If we represent softmax(x[i]) as S(x[i]) then .
  • CODE:

#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)

  • OUTPUT:
    [0.876 0.006 0.118]
  • For clarity and better understanding , below are the screenshot of the code and output attached.

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.


Related Solutions

f(x)=x^3-3x-1=0 x=[0,2] epsilon=5*10^-2 1. perform the bisection method for the root in [0,2] until your root...
f(x)=x^3-3x-1=0 x=[0,2] epsilon=5*10^-2 1. perform the bisection method for the root in [0,2] until your root is closer to the real root within epsilon. Let x_0=1.0, x_1=1.2 2. perform the secant method until your root is closer to the real root within epsilon. 3. do as in 2. with the Newton's method, with x_0=1.1
1) find all values of theta E [ 0,2 pi], where the curve r= 1- sin...
1) find all values of theta E [ 0,2 pi], where the curve r= 1- sin theta has a horizontal tangent line 2)find all values of theta E [ 0,2 pi] where the curve r=1- sin theta has a vertical tangent line 3) find the area of the region enclosed by the intersection of the curves" r= sin theta and r= cos theta
Given the joint probability density function f(x ,y )=k (xy+ 1) for 0<x <1--and--0<y<1 , find...
Given the joint probability density function f(x ,y )=k (xy+ 1) for 0<x <1--and--0<y<1 , find the correlation--ROW p (X,Y) .
FIreclay brick (ρ=2050 kg/m3, cp=960 J/kg-K, k=1.1 W/m-K) with dimensions 0.06 m x 0.09 m x...
FIreclay brick (ρ=2050 kg/m3, cp=960 J/kg-K, k=1.1 W/m-K) with dimensions 0.06 m x 0.09 m x 0.20 m is removed from a kiln at 1600K and cooled in air at 40C with h = 30 W/m2-K. What is the temperature at the corner of the brick after 50 minutes of cooling? Give your answer in degrees C.
A firm has the following long run production function x = a(K^1/2)(L^1/2)(P^1/4), where a > 0...
A firm has the following long run production function x = a(K^1/2)(L^1/2)(P^1/4), where a > 0 is a constant and K, L , P are inputs of the three factors. The prices of K, L , P are Rs. 1 , Rs. 9 and Rs. 8 respectively. a) Derive the firm’s long run total cost function , long run average cost function and long run marginal cost function. Show the workings in detail b) In the short run P is...
Find the extreme values of the function and where they occur. y= (x+1)/(x^2+3x+3)
Find the extreme values of the function and where they occur. y= (x+1)/(x^2+3x+3)
Q5. A monopolist has the cost function C(Q) = m*Q +k*Q2 where m and k are...
Q5. A monopolist has the cost function C(Q) = m*Q +k*Q2 where m and k are parameters. It faces two types of consumers, A and B, with the following demand curves for its product: PA =60–3*QA PB =80–2*QB For (a)—(c) below, assume Assume m=30 and k=0. (a) [4] What price will the monopolist charge under uniform pricing? (b) [4] What prices will the monopolist charge if it can price discriminate? (c) [4] How much higher is the monopolist’s profit in...
. We have the following algorithm. Algorithm Novel(X[0..m-1, 0..m-1]) //Input: A matrix X[0..m-1, 0..m-1] of real...
. We have the following algorithm. Algorithm Novel(X[0..m-1, 0..m-1]) //Input: A matrix X[0..m-1, 0..m-1] of real numbers for i←0 to m-2 do for j←i+1 to m-1 do    if X[i,j] ≠X[j, i]        return false return true a.   What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm?
Find absolute maximum and minimum values of the function f(x,y)=x^2+2y^2 on the curve ((x^2)/81)+((y^2)/100)=1 where x>=0,...
Find absolute maximum and minimum values of the function f(x,y)=x^2+2y^2 on the curve ((x^2)/81)+((y^2)/100)=1 where x>=0, y>=0
A random variable X has a distribution p(X=k) = A / (k(k+1)), k = 1,2,...,4, where...
A random variable X has a distribution p(X=k) = A / (k(k+1)), k = 1,2,...,4, where A is an constant. Then compute the value of p(1<=X<=3) The answer will be either: 2/3, 3/4, 5/6, or 15/16 A discrete random variable X is uniformly distributed among −1,0,...,12. Then, what is its PMF for k=−1,0,...,12 The answer will be either: p(X = k) = 1/12, 1/13, 1/14, or 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT