1

I'm having an issue using numpy in python3 at this instruction:

res = ( np.multiply(error, v_sigmop ))

I'm trying to multiply element-wise but I'm having this weird error :

res = ( np.multiply(error, v_sigmop ))
ValueError: operands could not be broadcast together with shapes (10,10000) (10000,10)

This operation isn't illegal since the amount of columns matches the amount of rows of the second array...

Any idea?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Irchad
  • 37
  • 1
  • 5
  • Use np.matmul. multiply is element-wise multiplication, where the arrays have to be the same or broadcastable sizes. – Mad Physicist Dec 16 '18 at 01:26
  • @MadPhysicist : matmut do not do the same than multiply. I tried to multiply 2 3 by 3 matrix of ones and I obtained this: `[[3. 3. 3.] [3. 3. 3.] [3. 3. 3.]] (3, 3) #matmul [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] (3, 3) #multiply ` – Irchad Dec 16 '18 at 01:48
  • Right, it doesn't. Your last sentence implies that you want matrix multiplication, not element-wise. – Mad Physicist Dec 16 '18 at 03:09

1 Answers1

0

I think you are maybe trying to multiple 2 matrixes with shape (r1,c) and (c,r2)

You can use A.dot(B) for your problem, that will multiple 2 matrixes.

This is example:

>>> a = np.arange(12).reshape((3,4))
>>> b = np.arange(8).reshape((4,2))
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
>>> a.dot(b)
array([[ 28,  34],
       [ 76,  98],
       [124, 162]])

Hope it will help you!

Edit

Because you don't want multiple 2 matrixes, you want to multiple as scalar, but multiple scalar is not your operation, that mean you can't multiple 2 matrixes such as:

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

multiple with

array([[0, 1],
       [2, 3],
       [4, 5]])

That is not valid operation for multiple 2 scalar.

You only have operations:

>>> a = np.arange(12).reshape((3,4))
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])


# Multiple all elements with a scalar
>>> np.multiply(a,0)
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])


# Multiple each column with a column
>>> b = np.arange(3).reshape((3,1))
>>> b
array([[0],
       [1],
       [2]])
>>> np.multiply(a,b)
array([[ 0,  0,  0,  0],
       [ 4,  5,  6,  7],
       [16, 18, 20, 22]])


# Multiple each row with a row
>>> b = np.arange(4).reshape((1,4))
>>> b
array([[0, 1, 2, 3]])
>>> np.multiply(a,b)
array([[ 0,  1,  4,  9],
       [ 0,  5, 12, 21],
       [ 0,  9, 20, 33]])


# Multiple each element with the same shape
>>> b = np.arange(12).reshape((3,4))
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> np.multiply(a,b)
array([[  0,   1,   4,   9],
       [ 16,  25,  36,  49],
       [ 64,  81, 100, 121]])
Ha. Huynh
  • 1,772
  • 11
  • 27
  • dot is for scalar product right? `import numpy as np foo = np.ones( (3,3) ) bar = np.ones( (3,3) ) a = np.dot(foo, bar) b = np.multiply(foo, bar) print(a, a.shape) print(b, b.shape) ` For this code I'm getting the following output: `[[3. 3. 3.] [3. 3. 3.] [3. 3. 3.]] (3, 3) [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] (3, 3) ` Which isn't the operation I expected... – Irchad Dec 16 '18 at 01:44
  • @Irchad, I've edited my answer, hope it will help you :) – Ha. Huynh Dec 16 '18 at 02:02