2

I have the following code in theano in order to calculate L2 distance

def distance(square=False):
    X = T.fmatrix('X')
    Y = T.fmatrix('Y')
    squared_euclidean_distances = (X ** 2).sum(1).reshape((X.shape[0], 1)) + (Y ** 2).sum(1).reshape \
    ((1, Y.shape[0])) - 2 * X.dot(Y.T)
    if square:
       return theano.function([X, Y], T.sqrt(squared_euclidean_distances))
    else:
       return theano.function([X, Y], squared_euclidean_distances)

source

print(distance()([[1, 0], [1, 1]], [[1, 0]]))

result with: [[ 0.] [ 1.]]

which is the distance matrix between the the left set(two vectors - [1, 0], [1, 1]) and the right set which contains single vector [1,0].

This works well with theano even if X and Y has different dim as above. I would like to get a general keras function to produce the same result. I tried:

def distance_matrix(vects):
    x, y = vects
    # <x,x> + <y,y> - 2<x,y>
    x_shape = K.int_shape(x)

    y_shape = K.int_shape(y)

    return K.reshape(K.sum(K.square(x), axis=1), (x_shape[0], 1)) + \
       K.reshape(K.sum(K.square(y), axis=1), (1, y_shape[0])) - \
       2 * K.dot(x, y)

but the following code does not produce the right result:

x = K.variable(np.array([[1, 0], [1, 1]]))
y = K.variable(np.array([[1, 0]]))
obj = distance_matrix
objective_output = obj((x, y))
print (K.eval(objective_output))

result with

ValueError: Shape mismatch: x has 2 cols (and 4 rows) but y has 4 rows (and 2 cols)
Apply node that caused the error: Dot22Scalar(/variable, /variable, TensorConstant{2.0})
Toposort index: 0
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix), TensorType(float32, scalar)]
Inputs shapes: [(4, 2), (4, 2), ()]
Inputs strides: [(8, 4), (8, 4), ()]
Inputs values: ['not shown', 'not shown', array(2.0, dtype=float32)]
Outputs clients: [[Elemwise{Composite{((i0 + i1) - i2)}}[(0, 2)](InplaceDimShuffle{0,x}.0, InplaceDimShuffle{x,0}.0, Dot22Scalar.0)]]

Edit: added outputs to code

Community
  • 1
  • 1
oak
  • 2,898
  • 2
  • 32
  • 65

1 Answers1

1

I found the mistake. I forgot to transpose Y

def distance_matrix(vects):

x, y = vects
# <x,x> + <y,y> - 2<x,y>
x_shape = K.int_shape(x)

y_shape = K.int_shape(y)

return K.reshape(K.sum(K.square(x), axis=1), (x_shape[0], 1)) +\
    K.reshape(K.sum(K.square(y), axis=1), (1, y_shape[0])) - \
2 * K.dot(x,K.transpose(y))
oak
  • 2,898
  • 2
  • 32
  • 65