4

I wrote a softmax regression function def softmax_1(x) that essentially takes in a m x n matrix, exponentiates the matrix, then sums the exponentials of each column.

x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])
#scores shape is (3, 80)

def softmax_1(x):
    """Compute softmax values for each sets of scores in x."""
    return(np.exp(x)/np.sum(np.exp(x),axis=0))

Converting it into a DataFrame I have to transpose

DF_activation_1 = pd.DataFrame(softmax_1(scores).T,index=x,columns=["x","1.0","0.2"])

So I wanted to try and make a version of the softmax function that takes in the transposed version and computes the softmax function

scores_T = scores.T
#scores_T shape is (80,3)

def softmax_2(y):
    return(np.exp(y/np.sum(np.exp(y),axis=1)))

DF_activation_2 = pd.DataFrame(softmax_2(scores_T),index=x,columns=["x","1.0","0.2"])

Then I get this error:

Traceback (most recent call last):
  File "softmax.py", line 22, in <module>
    DF_activation_2 = pd.DataFrame(softmax_2(scores_T),index=x,columns=["x","1.0","0.2"])
  File "softmax.py", line 18, in softmax_2
    return(np.exp(y/np.sum(np.exp(y),axis=1)))
ValueError: operands could not be broadcast together with shapes (80,3) (80,) 

Why doesn't this work when I transpose and switch the axis in the np.sum method?

halfer
  • 19,824
  • 17
  • 99
  • 186
O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

4

Change

np.exp(y/np.sum(np.exp(y),axis=1))

to

np.exp(y)/np.sum(np.exp(y),axis=1, keepdims=True)

This will mean that np.sum will return an array of shape (80, 1) rather than (80,), which will broadcast correctly for the division. Also note the correction to the bracket closing.

YXD
  • 31,741
  • 15
  • 75
  • 115
  • Hey thanks for the quick response, why do my rows not sum to 1 anymore after transposing? First row of `DF_activation_1` is `0.033211 0.667060 0.299729` while first row of `DF_activation_2` is `0.612139 1.278129 1.050304` – O.rka Mar 26 '16 at 22:44
  • I will mess around with it, the are still summing to > 1. I will let you know when I figure it out. – O.rka Mar 26 '16 at 22:55
  • 1
    Moved comment about brackets accidentally posted as a separate answer below into the answer above. – YXD Mar 27 '16 at 08:57