1

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?

5
  • Have you tried to debug it? Commented Feb 29, 2016 at 16:38
  • The error is very clear... Commented Feb 29, 2016 at 16:39
  • @MSeifert What do you mean debug it? Commented Feb 29, 2016 at 16:41
  • If you look at the value you are passing to softmax, the error becomes very obvious Commented Feb 29, 2016 at 16:42
  • @SharanDuggirala - See for example Wikipedia Commented Feb 29, 2016 at 16:45

4 Answers 4

2

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)

4
  • 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? Commented Feb 29, 2016 at 16:53
  • edited the answer. I think you used vstack where hstack would be more appropriate. Commented Feb 29, 2016 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. Commented Feb 29, 2016 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. Commented Feb 29, 2016 at 17:14
1

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.

0

This should work perfectly and fast

scores = [3.0, 1.0, 0.2]

import numpy as np


def softmax(x):

    num = np.exp(x)
    score_len = len(x)

    y = np.zeros(score_len, object) # or => np.asarray([None]*score_len)
    sum_n = np.sum(num)

    for i in range(score_len):
        y[i] = num[i] / sum_n

    return y


print(softmax(scores))

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

printout = softmax(scores).T

print(printout)

Output:

[0.8360188027814407 0.11314284146556011 0.050838355752999158]

[ array([  3.26123038e-05,   3.60421698e-05,   3.98327578e-05,
         4.40220056e-05,   4.86518403e-05,   5.37685990e-05,
         5.94234919e-05,   6.56731151e-05,   7.25800169e-05,
         8.02133239e-05,   8.86494329e-05,   9.79727751e-05,
         1.08276662e-04,   1.19664218e-04,   1.32249413e-04,
         1.46158206e-04,   1.61529798e-04,   1.78518035e-04,
         1.97292941e-04,   2.18042421e-04,   2.40974142e-04,
         2.66317614e-04,   2.94326482e-04,   3.25281069e-04,
         3.59491177e-04,   3.97299194e-04,   4.39083515e-04,
         4.85262332e-04,   5.36297817e-04,   5.92700751e-04,
         6.55035633e-04,   7.23926331e-04,   8.00062328e-04,
         8.84205618e-04,   9.77198335e-04,   1.07997118e-03,
         1.19355274e-03,   1.31907978e-03,   1.45780861e-03,
         1.61112768e-03,   1.78057146e-03,   1.96783579e-03,
         2.17479489e-03,   2.40352006e-03,   2.65630048e-03,
         2.93566604e-03,   3.24441273e-03,   3.58563059e-03,
         3.96273465e-03,   4.37949910e-03,   4.84009504e-03,
         5.34913227e-03,   5.91170543e-03,   6.53344491e-03,
         7.22057331e-03,   7.97996764e-03,   8.81922816e-03,
         9.74675448e-03,   1.07718296e-02,   1.19047128e-02,
         1.31567424e-02,   1.45404491e-02,   1.60696814e-02,
         1.77597446e-02,   1.96275532e-02,   2.16918010e-02,
         2.39731477e-02,   2.64944256e-02,   2.92808687e-02,
         3.23603645e-02,   3.57637337e-02,   3.95250385e-02,
         4.36819230e-02,   4.82759910e-02,   5.33532213e-02,
         5.89644285e-02,   6.51657716e-02,   7.20193157e-02,
         7.95936532e-02,   8.79645908e-02])
 array([ 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])
 array([ 0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433,
        0.00029433,  0.00029433,  0.00029433,  0.00029433,  0.00029433])]
0
0

inconsistencies in array construction may cause that kind of problem e.g.

[[1,2,3,4], [2,3], [1],[1,2,3,4]]

this is a bad examplary array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.