0

I have two arrays, which a printout claims are both of the same size ((20L,)). I want to multiply them element-wise. Using A*B, or np.multiply(A,B) give the same error:

ValueError: non-broadcastable output operand with shape (20,) doesn't match the broadcast shape (20,20)

Specifically, I've tried:

for k in xrange(1,self.HidNum):
    self.WHLayers[k-1]-=learning_rate*(\
        self.WHidback[k][j].dot(forward_pass[-(i+1)][2]).reshape(self.HidDim,1)*\
        (forward_pass[-(i+1)-j][0][k] * (1- forward_pass[-(i+1)-j][0][k])).reshape(self.HidDim,1) *\
        forward_pass[-(i+1)-j][0][k-1]
        )
    self.BHid[k-1]-= learning_rate*\
        self.WHidback[k][j].dot(forward_pass[-(i+1)][2].reshape(self.HidDim,1) *\
        np.multiply(forward_pass[-(i+1)-j][0][k], (1 - forward_pass[-(i+1)-j][0][k])))

which gives me this error message:

Traceback (most recent call last):
    File "<pyshell#83>", line 1, in <module>
vectrain(bob,1)
line 178, in vectrain
cur_cost=net.update(inputs,exp_y,learning_rate)
line 105, in update
    np.multiply(forward_pass[-(i+1)-j][0][k],(1 - forward_pass[-(i+1)-j][0][k])
ValueError: non-broadcastable output operand with shape (20,) doesn't match the broadcast shape (20,20)

and:

for k in xrange(1,self.HidNum):
    self.WHLayers[k-1]-=learning_rate*(\
        self.WHidback[k][j].dot(forward_pass[-(i+1)][2]).reshape(self.HidDim,1)*\
        (forward_pass[-(i+1)-j][0][k] * (1- forward_pass[-(i+1)-j][0][k])).reshape(self.HidDim,1) *\
        forward_pass[-(i+1)-j][0][k-1]
        )
    self.BHid[k-1]-= learning_rate*\
        self.WHidback[k][j].dot(forward_pass[-(i+1)][2].reshape(self.HidDim,1) *\
        (forward_pass[-(i+1)-j][0][k] * (1 - forward_pass[-(i+1)-j][0][k])))

which gets me:

Traceback (most recent call last):
File "<pyshell#85>", line 1, in <module>
vectrain(bob,1)
line 178, in vectrain
cur_cost=net.update(inputs,exp_y,learning_rate)
line 106, in update
(1 - forward_pass[-(i+1)-j][0][k])
ValueError: non-broadcastable output operand with shape (20,) doesn't match the broadcast shape (20,20)

I list the self.WHLayers update because it hasn't run into problems, and it's almost exactly the same. The last line of the self.BHid update is the problem, and if I break up each line as much as possible, I run into the error here:

(1 - forward_pass[-(i+1)-j][0][k])

The cited for loops are nested in two other for loops (thus the i, and j indices). self.HidNum, learning_rate, and self.HidDim are all nonzero positive integers.

  • self.WHLayers is a list of matrices
  • self.BHid is a list of vectors
  • self.WHidback is a list of lists of matrices
  • forward_pass is a list of lists where each of the inner lists contains three objects: a list of ndarrays, a single ndarray, and another ndarray.

A printout immediately before the cited for loop shows that

forward_pass[-(i+1)-j][0][k].shape, 
(1 - forward_pass[-(i+1)-j][0][k]).shape, 
(forward_pass[-(i+1)-j][0][k] *(1 - forward_pass[-(i+1)-j][0][k])).shape

all have the same shape: (20L,)

I have no idea why the broadcast shape is (20,20) here, and not in the self.WHLayers update.

stovfl
  • 14,998
  • 7
  • 24
  • 51
Dan
  • 11
  • 2
  • Show the complete error message, including stack trace. The parts about an output operand seem to be describing different code than what you've shown us. – user2357112 Mar 22 '17 at 17:04
  • The error messages seem to indicate you've actually passed a third argument to `np.multiply`. The code in the error messages is visibly different from the code you've posted; as far as I can tell, the code you've posted only ever passes two arguments to `np.multiply`. – user2357112 Mar 22 '17 at 18:06
  • Right. Added the complete error message, with stack trace. – Dan Mar 22 '17 at 18:07
  • Which third argument are you seeing? In the error message I have `np.multiply(forward_pass[-(i+1)-j][0][k],(1 - forward_pass[-(i+1)-j][0][k])` with the only two arguments being `(forward_pass[-(i+1)-j][0][k]` and `(1 - forward_pass[-(i+1)-j][0][k])` – Dan Mar 22 '17 at 18:15
  • I'm not seeing a third argument, but I *am* seeing less parentheses at the end of the line than I would expect, and less parentheses than exist in the source code this error message was supposed to correspond to. The parentheses I'm seeing are consistent with an additional argument on the next line. – user2357112 Mar 22 '17 at 18:17
  • Ah. I have the last parenthesis on a new line in my original code. The visual separation helped keep me sane. In the second error message, I put new lines between each argument to try to find which part was tripping me up. – Dan Mar 22 '17 at 18:21
  • Putting in more newlines doesn't usually help, because the Python interpreter isn't good at determining which physical line of a particular logical line an error originated on. You need to introduce intermediate variables and separate your statement into multiple statements if you want to do that. – user2357112 Mar 22 '17 at 18:24
  • Anyway, the error message pretty clearly indicates that some function is receiving an `out` parameter with the wrong shape. The most obvious way that could be happening is if you're passing an extra argument somewhere you don't think you are. – user2357112 Mar 22 '17 at 18:25
  • Could you explain what you mean by an `out` parameter? I'll check to see if I'm passing any arguments beyond the ones I want though. – Dan Mar 22 '17 at 18:32
  • Look at the [docs](https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html) for `numpy.multiply`. See that third, optional parameter named `out`? That's what I'm talking about. If provided, it's interpreted as an array to place the results in. All NumPy ufuncs take such a parameter, as do some other functions, like `numpy.dot`. – user2357112 Mar 22 '17 at 18:35
  • Got it! Thanks!!!! – Dan Mar 22 '17 at 18:46

0 Answers0