0

The matrix a has shape (4,3) and z has shape (4,). My intent is I want to divide every 3 dim vector in a with scalar in z. Consider the below example:

Input:

a = [[1,1,1],
 [2,2,2],
 [2,2,2],
 [5,5,5]]

z = [10,10,10,5]

Expected Output:

[[.1,.1,.1],
 [.2,.2,.2],
 [.2,.2,.2],
 [1,1,1]]

Below is my attempt to do the same using keras Lambda layer where K.sum(xin[0], axis=1) would be a and xin[1] would be z

 x = Lambda(lambda xin: K.sum(xin[0], axis=1) / xin[1], name='mean')([x1,x2])

But when I run this code, I am getting the following error:

InvalidArgumentError (see above for traceback): Incompatible shapes: [4,3] vs. [4]

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
claudius
  • 1,112
  • 1
  • 10
  • 23

1 Answers1

1
x = Lambda(
    lambda xin: K.sum(xin[0], axis=1) / K.expand_dims(xin[1]), name='mean'
)([x1,x2])

The function expand_dims will turn (4,) into (4,1), making both shapes compatible.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214