Computing tSNE gradient using autograd

116 views Asked by At

I am trying to implement the tSNE algorithm in python. I am using the autograd package for computing the gradients, instead of the analytic gradient usually used.

But I am unable to compute the gradients as required. I am new to ML and trying my hand with autograd and other frameworks.

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

1

There are 1 answers

0
sascha On

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)

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!

So you probably should check your brackets.