<p>I suggest you to flatten and use <a href="https://github.com/facebookresearch/faiss/wiki/Getting-started" rel="nofollow noreferrer">FAISS</a>. I had the same problem but using faiss it was made simple it is creating indexing which is faster to compare. Using faiss indexing i am able to compare 1 vector with 21 million vector in 6 milliseconds.</p>

CC BY-SA 4.0
2022-09-30T19:05:29.810Z
Thank you for your comment. I tried FAISS based on you suggestion, but it does not seem to support incremental addition of training samples. Based on your experience, is there a possible workaround for such a scenario or should I look for something different?
Vilainor
CC BY-SA 4.0
2022-09-29T06:48:43.787Z
2022-09-29T06:48:43.787Z
Hiren Namera
<p>I have this scenario where I need to measure the similarity between a 2d tensor t1: (100,8) and 61 tensors of the same shape(100,8). 100 represent time-steps and 8 is the no. of options.</p>
<p>I first tried flattening all tensors so that I can use the cosine similarity measure, but it slows down performance significantly.</p>
<p>I used pairwise comparisons without flattening like the approach below, but it made executing mathematical operations more complicated (e.g., diagonal operations):
<a href="https://ai.stackexchange.com/questions/36191/how-to-calculate-a-meaningful-distance-between-multidimensional-tensors">How to calculate a meaningful distance between multidimensional tensors</a>.</p>
<p>What should I do to speed-up ops on flattened tensors/or are there alternatives to flattening?</p>

<deep-learning><pytorch>
Similarities between 2d-vectors. (to flatten or to not)
Vilainor
# Similarities between 2d-vectors. (to flatten or to not)

(Asked by: username_0 on 2022-09-29)

I have this scenario where I need to measure the similarity between a 2d tensor t1: (100,8) and 61 tensors of the same shape(100,8). 100 represent time-steps and 8 is the no. of options.

I first tried flattening all tensors so that I can use the cosine similarity measure, but it slows down performance significantly.

I used pairwise comparisons without flattening like the approach below, but it made executing mathematical operations more complicated (e.g., diagonal operations):
[How to calculate a meaningful distance between multidimensional tensors](https://ai.stackexchange.com/questions/36191/how-to-calculate-a-meaningful-distance-between-multidimensional-tensors).

What should I do to speed-up ops on flattened tensors/or are there alternatives to flattening?

**Tags:** deep-learning, pytorch


---

## 1 answers

### Answer by username_1 on 2022-09-29. Score: 2

I suggest you to flatten and use [FAISS](https://github.com/facebookresearch/faiss/wiki/Getting-started). I had the same problem but using faiss it was made simple it is creating indexing which is faster to compare. Using faiss indexing i am able to compare 1 vector with 21 million vector in 6 milliseconds.

> **Comments:**
> **username_0**: Thank you for your comment. I tried FAISS based on you suggestion, but it does not seem to support incremental addition of training samples. Based on your experience, is there a possible workaround for such a scenario or should I look for something different?  

---

