3

First off, here is 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 = [0] * score_len
    for index in range(1,score_len):
        y[index] = (num[index])/(sum(num))
    return y

print(softmax(scores))

# 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()

Now looking at this question, I can tell that T is the transpose of my list. However, I seem to be getting the error:

AttributeError: 'list' object has no attribute 'T'

I do not understand what's going on here. Is my understanding of this entire situation wrong. I'm trying to get through the Google Deep Learning course and I thought that I could Python by implementing the programs, but I could be wrong. I currently know a lot of other languages like C and Java, but new syntax always confuses me.

Community
  • 1
  • 1
SDG
  • 2,260
  • 8
  • 35
  • 77
  • 2
    You have a regular Python `list` object, not a `numpy.ndarray` or `numpy.matrix`. The latter may support a `.T` attribute, but Python lists don't. – Martijn Pieters Feb 29 '16 at 16:02
  • @MartijnPieters http://stackoverflow.com/questions/1514553/how-to-declare-an-array-in-python Aren't lists the same as arrays in Python? – SDG Feb 29 '16 at 16:04
  • 1
    No, they are not. That question is incredibly confusing in that regard. – Martijn Pieters Feb 29 '16 at 16:06
  • Python `list` objects are sequences of arbitrary length that can store any object. `array`s (as defined in C) are fixed length and can only store values of one type. Python does have a [`array` module](https://docs.python.org/2/library/array.html) where the type is a lot like the C concept. `numpy` defines another, with a lot in common with the Python standard library `array` module. You want the `numpy` definition here. – Martijn Pieters Feb 29 '16 at 16:07
  • 1
    Anyway, when in doubt, [look at the documentation](https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) and you'll see that there is no `.T` attribute documented. – Martijn Pieters Feb 29 '16 at 16:15
  • @MartijnPieters Thanks, I just put up an answer, could you make sure it's all accurate? – SDG Feb 29 '16 at 16:21
  • 1
    I don't actually do much work with numpy, I don't know if it is accurate. All I did was take a quick peek at the docs to confirm Numpy has objects with a `.T` attribute. – Martijn Pieters Feb 29 '16 at 16:23

2 Answers2

4

As described in the comments, it is essential that the output of softmax(scores) be an array, as lists do not have the .T attribute. Therefore if we replace the relevant bits in the question with the code below, we can access the .T attribute again.

num = np.exp(x)
score_len = len(x)
y = np.array([0]*score_len)

It must be noted that we need to use the np.array as non numpy libraries do not usually work with ordinary python libraries.

SDG
  • 2,260
  • 8
  • 35
  • 77
1

Look at the type and shape of variables in your code

x is a 1d array; scores is 2d (3 rows):

In [535]: x.shape
Out[535]: (80,)
In [536]: scores.shape
Out[536]: (3, 80)

softmax produces a list of 3 items; the first is the number 0, the rest are arrays with shape like x.

In [537]: s=softmax(scores)
In [538]: len(s)
Out[538]: 3
In [539]: s[0]
Out[539]: 0
In [540]: s[1].shape
Out[540]: (80,)
In [541]: s[2].shape
Out[541]: (80,)

Were you expecting softmax to produce an array with the same shape as its input, in this case a (3,80).

num=np.exp(scores)
res = np.zeros(scores.shape)
for i in range(1,3):
    res[i,:]= num[i,:]/sum(num)

creates a 2d array that can be transposed and plotted.

But you shouldn't have to do this row by row. Do you really want the 1st row of res to be 0?

res = np.exp(scores)
res = res/sum(res)
res[0,:] = 0    # reset 1st row to 0?

Why were you doing a vectorized operation on each row of scores, but not on the whole thing?

hpaulj
  • 221,503
  • 14
  • 230
  • 353