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.

7
  • 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. Commented Feb 29, 2016 at 16:02
  • @MartijnPieters stackoverflow.com/questions/1514553/… Aren't lists the same as arrays in Python? Commented Feb 29, 2016 at 16:04
  • 1
    No, they are not. That question is incredibly confusing in that regard. Commented Feb 29, 2016 at 16:06
  • 1
    Anyway, when in doubt, look at the documentation and you'll see that there is no .T attribute documented. Commented Feb 29, 2016 at 16:15
  • 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. Commented Feb 29, 2016 at 16:23

2 Answers 2

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.

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

Comments

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.