I am trying to go through the assignment 1 for Stanford cs244n class. Problem 1b highly recommend optimization for the Softmax function. I managed to get the Softmax of the N dimensional vector. I also got the Softmax of the MxN dimensional matrix but used a for loop through the columns. I have the following code:
def softmax(x):
orig_shape = x.shape
# Matrix
if len(x.shape) > 1:
softmax = np.zeros(orig_shape)
for i,col in enumerate(x):
softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col - np.max(col)))
# Vector
else:
softmax = np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
return softmax
Can I implement a more optimized Matrix implementation?
Using
NumPy broadcasting
on relevantufuncs
and that covers ndarrays of generic number of dimensions -You can try to use
np.apply_along_axis
, where you have to specify which axis to execute your code (in your caseaxis=1
). Here's a working example: