1

So in this simple code the line

result[columnNumber] = column #this assignment fails for some reason!

fails, and specifically it simply assigns array of zeroes instead of what it is supposed to assign, and I have no idea why! So this is the full 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."""
    data=np.array(x)
    columnNumber=0
    result=data.copy()
    result=result.T

    for column in data.T:
        sumCurrentColumn=0
        try: #Since 'column' can potentially be just a double,and sum needs some iterable object
            sumCurrentColumn=sum(np.exp(column))
        except TypeError:
            sumCurrentColumn=np.exp(column)



        column=np.divide(np.exp(column),sumCurrentColumn)
        print(column)
        print('before assignment:'+str(result[columnNumber]))
        result[columnNumber] = column #this assignment fails for some reason!
        print('after assignment:'+str(result[columnNumber]))
        columnNumber+=1 

    result=result.T

    return result


scores = np.array([[1, 2, 3, 6],
                   [2, 4, 5, 6],
                   [3, 8, 7, 6]])

print(softmax(scores))

and this is its output:

   [ 0.09003057  0.24472847  0.66524096]
before assignment:[1 2 3]
after assignment:[0 0 0]
[ 0.00242826  0.01794253  0.97962921]
before assignment:[2 4 8]
after assignment:[0 0 0]
[ 0.01587624  0.11731043  0.86681333]
before assignment:[3 5 7]
after assignment:[0 0 0]
[ 0.33333333  0.33333333  0.33333333]
before assignment:[6 6 6]
after assignment:[0 0 0]
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

2 Answers 2

2

In your example, the input scores is all integers, so the data type of the data array is integer. Therefore result is also an integer array. You can't assign a floating point value into an integer array--numpy arrays have homogeneous data types which can not be dynamically changed. The line result[columnNumber] = column is truncating the values in column to integers, and since they are all between 0 and 1, the assigned values are all 0.

Try changing the creation of result to:

result = data.astype(float)

(By default, the astype method creates a copy even if data already has the specified type.)

Sign up to request clarification or add additional context in comments.

1 Comment

I feel a bit bad that I couldn't get it myself :) Thx man! By the way,since I don't know whether or not x is a multidimensional array or not, I thought I had to use that exception handling since sum() takes as an argument iterable, which a double or int is not. This feels not right though. Do you know of a more elegant way to do the same?
0

Your array result has type int so all your floats are automatically converted to int's, in this case 0. Use this result = data.astype(float).

Comments

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.