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 matricesself.BHid
is a list of vectorsself.WHidback
is a list of lists of matricesforward_pass
is a list of lists where each of the inner lists contains three objects: alist of ndarrays
, asingle ndarray
, and anotherndarray
.
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
.