Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

First off, here's my code:

"""Softmax."""

scores = [3.0, 1.0, 0.2]

import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    num = np.exp(x)
    score_len = len(x)
    y = np.array([0]*score_len)
    sum_n = np.sum(num)
    #print sum_n
    for index in range(1,score_len):
        y[index] = (num[index])/sum_n
    return y

print(softmax(scores))

The error comes up at the line:

y[index] = (num[index])/sum_n

I run the code with:

# Plot softmax curves
import matplotlib.pyplot as plt
x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])

plt.plot(x, softmax(scores).T, linewidth=2)
plt.show()

What exactly is going wrong here?

share|improve this question
    
Have you tried to debug it? – MSeifert Feb 29 '16 at 16:38
    
The error is very clear... – Idos Feb 29 '16 at 16:39
    
@MSeifert What do you mean debug it? – Sharan Duggirala Feb 29 '16 at 16:41
    
If you look at the value you are passing to softmax, the error becomes very obvious – user632657 Feb 29 '16 at 16:42
    
@SharanDuggirala - See for example Wikipedia – MSeifert Feb 29 '16 at 16:45
up vote 1 down vote accepted

Just editing a print statement as "debugger" reveals what is happening:

import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    num = np.exp(x)
    score_len = len(x)
    y = np.array([0]*score_len)
    sum_n = np.sum(num)
    #print sum_n
    for index in range(1,score_len):
        print((num[index])/sum_n)
        y[index] = (num[index])/sum_n
    return y

x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])
softmax(scores).T

this prints

[ 0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504  0.00065504
  0.00065504  0.00065504]

so you are trying to assign this array to one element of another array. Which is not allowed!

There are several ways to do it so that it is working. Just changing

y = np.array([0]*score_len)

to a multidimensional array would work:

y = np.zeros(score.shape)

That should do the trick but I'm not sure if it's what you intended.


EDIT:

It seems you did not want multidimensional input so you just need to change:

scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])

to

scores = np.hstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])

verify the shape of these arrays by printing scores.shape really helps you find such errors by yourself. The first one stacks along the first axis (vstack) and the hstack by the zeroth-axis (which is what you want)

share|improve this answer
    
I just wanted a singular number. num[index] is supposed to be a single number and so is sum_n. Why is the division leading to such a huge array? – Sharan Duggirala Feb 29 '16 at 16:53
    
edited the answer. I think you used vstack where hstack would be more appropriate. – MSeifert Feb 29 '16 at 16:56
    
What exactly does np.exp() do to the list at the very start? I want a list that returns the exp() of every element of the list. – Sharan Duggirala Feb 29 '16 at 17:11
    
I hinted several times and I'll do it once again: use print statements and find out by yourself! Also it is a bit frowned upon to expand the nature of the question in an iterative process. Please open another question (but first seach google) if you have any other questions/problems. – MSeifert Feb 29 '16 at 17:14

This a bad way of initializing an array:

y = np.array([0]*score_len)

better to do something like

y = np.zeros((n,m))

where n and m are the 2 dimensions of the final product. I assume from your other question that you want y to be 2d (after all you do a .T on it after).

Pay attention to the shape of scores that you pass to the function. And when iterating, include :. It can be optional, but you need it to keep the dimensions straight in your own mind:

y[index,:] = (num[index,:])/sum_n

In sum - focus on understanding how to work with multi-dimensional arrays - how to create them, and how index them, how to work with them without iteration, and how to iterate correctly if needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.