Brushline
Brushline

Reputation: 47

Numpy Rowwise Addition with a (Nx1) Matrix and a Vector with Length N

I am trying to update the weights in a neural network with this line:

self.l1weights[0] = self.l1weights[0] + self.learning_rate * l1error

And this results in a value error:

ValueError: could not broadcast input array from shape (7,7) into shape (7)

Printing the learning_rate*error and the weights returns something like this:

[[-0.00657573]
 [-0.01430752]
 [-0.01739463]
 [-0.00038115]
 [-0.01563393]
 [-0.02060908]
 [-0.01559269]]
[  4.17022005e-01   7.20324493e-01   1.14374817e-04   3.02332573e-01
   1.46755891e-01   9.23385948e-02   1.86260211e-01]

It is clear the weights are initialized as a vector of length 7 in this example and the error is initialized as a 7x1 matrix. I would expect addition to return a 7x1 matrix or a vector as well, but instead it generates a 7x7 matrix like this:

[[  4.10446271e-01   7.13748760e-01  -6.46135890e-03   2.95756839e-01
    1.40180157e-01   8.57628611e-02   1.79684478e-01]
 [  4.02714481e-01   7.06016970e-01  -1.41931487e-02   2.88025049e-01
    1.32448367e-01   7.80310713e-02   1.71952688e-01]
 [  3.99627379e-01   7.02929868e-01  -1.72802505e-02   2.84937947e-01
    1.29361266e-01   7.49439695e-02   1.68865586e-01]
 [  4.16640855e-01   7.19943343e-01  -2.66775370e-04   3.01951422e-01
    1.46374741e-01   9.19574446e-02   1.85879061e-01]
 [  4.01388075e-01   7.04690564e-01  -1.55195551e-02   2.86698643e-01
    1.31121961e-01   7.67046648e-02   1.70626281e-01]
 [  3.96412924e-01   6.99715412e-01  -2.04947062e-02   2.81723492e-01
    1.26146810e-01   7.17295137e-02   1.65651130e-01]
 [  4.01429313e-01   7.04731801e-01  -1.54783174e-02   2.86739880e-01
    1.31163199e-01   7.67459026e-02   1.70667519e-01]]

Numpy.sum also returns the same 7x7 matrix. Is there a way to solve this without explicit reshaping? Output size is variable and this is an issue specific to when the output size is one.

Upvotes: 0

Views: 278

Answers (1)

Blownhither Ma
Blownhither Ma

Reputation: 1471

When adding (7,) array (named a) with (1, 7) array (named b), broadcasting happens and generates (7, 7) array. If your just want to do element-by-element addition, keep them in the same shape.
a + b.flatten() gives (7,). flatten makes all the dimensions collapse into one. This keeps the result as a row.
a.reshape(-1, 1) + b gives (1, 7). -1 in reshape requires numpy to decide how many elements are there given other dimensions. This keeps the result as a column.

a = np.arange(7)                          # row
b = a.reshape(-1, 1)                      # column

print((a + b).shape)                      # (7, 7)
print((a + b.flatten()).shape)            # (7,)
print((a.reshape(-1, 1) + b).shape)       # (7, 1)

In your case, a and b would be self.l1weights[0] and self.learning_rate * l1error respectively.

Upvotes: 2

Related Questions