使用张量流优化欧氏距离

时间:2019-05-11 22:13:40

标签: tensorflow optimization euclidean-distance mds

我正在尝试通过优化以下成本函数,使用张量流生成MDS空间here  费用= {enter image description here

成本试图生成嵌入(向量),使得向量Vi和Vj(dij)之间的欧式距离接近|| Xi-Xj ||。来自相异矩阵(恒定)

我的代码:

def pairwise_dist (A, B):  
    """
    Computes pairwise distances between each element of A and each element of B.
    Args:
    A,    [m,d] matrix
    B,    [n,d] matrix
    Returns:
    D,    [m,n] matrix of pairwise distances
    """
    with tf.variable_scope('pairwise_dist'):
        # squared norms of each row in A and B
        na = tf.reduce_sum(tf.square(A), 1)
        nb = tf.reduce_sum(tf.square(B), 1)

        # na as a row and nb as a co"lumn vectors
        na = tf.reshape(na, [-1, 1])
        nb = tf.reshape(nb, [1, -1])

        # return pairwise euclidead difference matrix
        D = tf.sqrt((tf.maximum(na - 2*tf.matmul(A, B, False, True) + nb, 0.0))+0.0001)#+eps
    return D

training_epochs = 150
count=0
tf.reset_default_graph()


embedding=tf.get_variable("embedding",initializer = np.array(dim_5_all[0],dtype=np.float32))

dissMatrix=tf.constant(np.array(dissmatrix))# 

midstep= pairwise_dist(embedding,embedding)#calculate the  eucl_dis

thirdStep=tf.reduce_sum(tf.square(midstep-dissMatrix))

fourthstep=tf.sqrt(thirdStep /tf.reduce_sum(tf.square(midstep)))

cost=thirdStep
optimizer = tf.train.AdagradOptimizer(0.001)
train_step = optimizer.minimize(cost )

init = tf.global_variables_initializer()

cost_history = np.empty(shape=[1],dtype=float)

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):

        _,mid,th,fou,vectors= sess.run([train_step,midstep,thirdStep,fourthstep,embedding])

该模型不起作用,成本从少量开始,没有降低,即使我改变学习率,它也保持不变。生成的向量之间的欧几里得距离不接近相异矩阵。

我从代码中看不到问题

感谢您的帮助

0 个答案:

没有答案