I’m currently working through some concepts in a computer-science textbook. Linear algebra is heavily used, and the examples they show in the textbook all use Numpy.
One expression in particular has me totally confused, because it seems to be a completely useless expression. Copied verbatim from the textbook, it says:
normalisers = sum(exp(outputs),axis=1)*ones((1,shape(outputs)[0]))
So, I’ll remove the exp for the sake of simplification (it’s not relevant to the issue here), which gives us:
sum(outputs,axis=1)*ones((1,shape(outputs)[0]))
where outputs is a 2-D Numpy array (matrix).
As far as I can tell, this is just summing all the rows in the outputs matrix, and then multiplying the resulting vector element-wise by a vector of all ones. So… what’s the point of multiplying by all ones here? It’s not going to change the values at all.
Is this an error in the textbook, or am I just not seeing how multiplying by all ones could possibly have any effect on the values here? I’m only somewhat familiar with Numpy at this point, so I’m not sure if I’m simply misunderstanding some of the implications of this expression.
As mutzmatron writes in the comment, when
outputsis an array, this multiplication is a highly contrived way of changing the shape of the result ofsumfrom(n,)to(1,n). The fast and idiomatic way to do that isIn contrast to the way presented in your textbook, this is both readable and scalable, because
reshapetakes constant rather than linear time and memory.However, if
outputsis not an array but an object of the dreaded typenp.matrix, the result is entirely different:(But then still, it’s a contrived way of expressing a different operation.)