Reputation: 31
Python throws an error when calling numpy sum function on a matrix.
probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
The error
probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
TypeError: sum() got an unexpected keyword argument 'keepdims'
Context: Calculate the loss function for a softmax classifier. Numerator is the exponential of the score function for the correct class and denominator is the sum of all the exponentials for all possible classes.
Upvotes: 2
Views: 2081
Reputation: 14699
The argument is valid in the latest version of numpy as explained here. Here is the full list of argument for numpy.sum:
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)
This was added since version 1.7 as you can see in the source code here. So, you need to upgrade your numpy installation.
Upvotes: 2
Reputation: 152765
The keepdims
argument was added in NumPy 1.7. At least the docstring of np.sum
(1.6) hasn't listed it as one of the arguments:
numpy.sum(a, axis=None, dtype=None, out=None)
However the 1.7 docstring already listed it:
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)
Given that NumPy 1.6 was released in 2012 you probably should update your NumPy package.
However you could also use np.expand_dims
in case you can't (or don't want to) update NumPy:
np.expand_dims(np.sum(exp_scores, axis=1), axis=1)
Upvotes: 2