将Theano Euclidean距离转换为keras引擎格式

时间:2017-03-27 10:21:20

标签: tensorflow theano keras keras-layer

我在theano中有以下代码,以便计算L2距离

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]]))

结果:        [[0.]        [1。]]

是左集(两个矢量 - [1,0],[1,1])和右集之间的距离矩阵,包含单个矢量[1,0]。

即使X和Y具有与上面不同的暗淡,这也适用于theano。我想得到一个通用keras函数来产生相同的结果。我试过了:

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)

但以下代码未产生正确的结果:

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))

结果

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)]]

修改: 将输出添加到代码

1 个答案:

答案 0 :(得分:1)

我发现了错误。我忘了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))