GratefulDeadpool
GratefulDeadpool

Reputation: 21

Why aren't these numpy operations equivalent, and how can I fix that?

Given the following arrays:

X = np.array([[1, 27, 3], [4, -1, 6]])
W = np.array([2, 4, 3])

These two equations are equivalent:

sum_a = 0
for l in range(len(X)):
    sum_a += np.sum(W * X[l])   # 141
sum_b = np.sum(np.sum(W * X))   # 141

But these two are not:

sum_a = 0
for l in range(len(X)):
    sum_a += np.exp(np.sum(W * X[l]))   # 4.797813327299302e+51
sum_b = np.sum(np.exp(np.sum(W * X)))   # 1.7199742630376623e+61 

Why is that, and how can I make sum_b equivalent to sum_a for the second case? I specifically want to equate the sum using numpy so I can vectorize a bigger equation.

Upvotes: 2

Views: 55

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

Those are not the same operations. You are hoping that e**119 + e**22 == e**141, but that's not how exponentiation works. You can MAKE it the same, but it's a multiplicative operation, not an additive one:

sum_a = 1
for l in range(len(X)):
    sum_a *= np.exp(np.sum(W * X[l]))
sum_b = np.sum(np.exp(np.sum(W * X)))

And, by the way, the outer np.sum in that last line is useless. The inner np.sum returns a single value.

FOLLOWUP

Ah, so you WANT the summation version. You can do that by only summing over one axis:

sum_b = np.sum(np.exp(np.sum(W * X, axis=1)))

In this particular case, it's silly, because e**119 is so much larger than e**22 that the latter doesn't contribute.

Upvotes: 5

Related Questions