简体   繁体   English

使用Autograd计算tSNE梯度

[英]Computing tSNE gradient using autograd

I am trying to implement the tSNE algorithm in python. 我正在尝试在python中实现tSNE算法。 I am using the autograd package for computing the gradients, instead of the analytic gradient usually used. 我正在使用autograd软件包来计算梯度,而不是通常使用的分析梯度。

But I am unable to compute the gradients as required. 但是我无法根据需要计算梯度。 I am new to ML and trying my hand with autograd and other frameworks. 我是ML的新手,并尝试使用autograd和其他框架。

So, this is my approach. 所以,这是我的方法。 I first compute the similarity matrix P. Then I compute the low-dimensional affinity matrix Q on-the-go while computing the loss. 我首先计算相似度矩阵P。然后在计算损失的同时不断计算低维相似度矩阵Q。 This is my code - 这是我的代码-

def compute_kl_loss(Y, P, n):
    loss = 0
    for i in range(n):
        qij = 1 / (1 + np.sum((Y[i,:] - Y)**2),1)
        for j in range(n):
            loss += P[i,j]* np.log(P[i,j]) - P[i,j]*np.log(qij)
    return loss

def get_grad(Y, P):
    n = Y.shape[0]
    loss_kld = lambda Y: compute_kl_loss(Y, P, n)
    gradY = grad(loss_kld) 
    dY = gradY(Y).

But this approach doesn't seem to work. 但是这种方法似乎行不通。 I get the following error - 我收到以下错误-

  File "tsne.py", line 130, in compute_kl_loss
    qij = 1 / (1 + np.sum((Y[i,:] - Y)**2),1)
TypeError: unsupported operand type(s) for /: 'int' and 'tuple'

Kindly show me how to rectify this. 请教我如何纠正此问题。 And is my approach the right one?, Or is there a better way to do it? 我的方法正确吗?或者有更好的方法吗?

Thank you. 谢谢。

In this line: 在这一行:

qij = 1 / (1 + np.sum((Y[i,:] - Y)**2),1)

you are creating a tuple, consisting of: 您正在创建一个元组,其中包括:

left_part: 1 + np.sum((Y[i,:] - Y)**2)
right_part: 1

the tuple being: my_tuple = (left_part, right_part) 元组为: my_tuple = (left_part, right_part)

which is pretty much explained in your error. 这在您的错误中有很多解释。

The operation 1 / (x, y) ( (x, y) being a tuple) is invalid in python as explained in the error! 操作1 / (x, y)(x, y)是一个元组)在python中无效,如错误所述!

So you probably should check your brackets. 因此,您可能应该检查一下括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM