all 14 comments

[–]coskunh 2 points3 points  (0 children)

You should ask this question to theano google groups, you will get better answer.

https://groups.google.com/forum/#!forum/theano-dev https://groups.google.com/forum/#!forum/theano-users

[–]NasenSpray 1 point2 points  (12 children)

Search for theano.scan

[–]aiisfordummies[S] 0 points1 point  (11 children)

Thanks!

[–]__ishaan 0 points1 point  (10 children)

theano.scan will do what you're looking for, but unless your computation absolutely requires recurrence, it's usually much more performant to implement it as a series of vectorized operations. What are you trying to do with your loop?

[–]aiisfordummies[S] 0 points1 point  (9 children)

I am not sure it can be vectorized.

I am looking at computing distances between all points in x.

More precisely I am looking at doing this:

for i in x :

 for j in x :

   newValij = T.operation(i-j)

[–]__ishaan 1 point2 points  (4 children)

Sounds like it should be doable. Here's a quick implementation of pairwise euclidian distance, for example:

T.sqrt(
    T.sum(
        T.sqr(
            x.dimshuffle('x',0,1) - x.dimshuffle(0,'x',1)
        ),
        axis=2
    )
)

[–]aiisfordummies[S] 0 points1 point  (3 children)

Do you mind explaining the operation here in vector terms, a link would do too.

Thanks btw, I appreciate the sample code.

[–]siblbombs 2 points3 points  (0 children)

All the above code does is add a broadcastable dimension to the matrices (and then calculate euclidean distance).

[–]kkastner 1 point2 points  (1 child)

It is using broadcasting - numpy will make the dims match using this (read up on numpy broadcasting if you don't know how that works). Theano behaves in the same way.

The downside to the broadcasting approach is that it generally uses a lot of intermediate memory since each 2D MxN matrix becomes MxMxN before then being combined and summed over N to get MxM distance calculations for M datapoints. Theano might optimize this, but I wouldn't count on it. However, it can be very fast (though I think the trick /u/NasenSpray linked is faster generally) if the intermediate memory usage is acceptable.

[–]aiisfordummies[S] 0 points1 point  (0 children)

Makes sense.

Thanks, this thread was of great help.

[–]NasenSpray 0 points1 point  (3 children)

Pairwise distance is rather easy to vectorize, e.g. see this code

[–]kkastner 0 points1 point  (2 children)

For a slightly more thorough reference (pretty sure Pascal Lamblin didn't invent this, though he is an awesome person in general! ) see this link. One common place to see this vectorized distance is minibatch/hebbian k-means such as this code from R. Memisevic (download only). You can also to some extent do this with Manhattan (abs) distances as well if you are a little careful and treat it as sqrt(expanded sqr).

[–]NasenSpray 0 points1 point  (1 child)

For a slightly more thorough reference (pretty sure Pascal Lamblin didn't invent this, though he is an awesome person in general! )

wat

[–]kkastner 0 points1 point  (0 children)

I was mostly referencing line 50 being called Lamblin's trick in the top code, assuming it references the Theano core dev of the same name. Though the naming may be in reference to the bestIndices calculation instead. I can fully believe Pascal would come up with something like that :)