I came across the same problem but the proposed solutions were far too slow when using larger arrays. The following simple solution works on CPU and GPU and is significantly faster than the other proposed solutions:

combined = torch.cat((t1, t2))
uniques, counts = combined.unique(return_counts=True)
difference = uniques[counts == 1]
intersection = uniques[counts > 1]
Answer from Olivier on Stack Overflow
🌐
Quora
quora.com › Can-Pytorch-find-the-intersection-of-two-vectors
Can Pytorch find the intersection of two vectors? - Quora
Answer: I believe this might be in line of what you are looking for: jmichaux/pytorch-grasping Other than that, i largely found references to Matlab and custom Scripts that people have performed. If my memory serves me correctly - I do believe that Line intersection can be manually computed. ...
🌐
PyTorch Forums
discuss.pytorch.org › t › intersection-between-to-vectors-tensors › 50364
Intersection between to vectors/tensors - PyTorch Forums
July 11, 2019 - Can pytorch find the intersection of two vectors? Similar to the Matlab “intersection” function: https://www.mathworks.com/help/matlab/ref/double.intersect.html;jsessionid=0f4b1f0e0ff110c50bb8a0076587 What about other…
🌐
PyTorch Forums
discuss.pytorch.org › t › intersection-between-to-vectors-tensors › 50364 › 3
Intersection between to vectors/tensors - #3 by WangXin93 - PyTorch Forums
November 18, 2019 - I used np.intersect1d like: >>> import torch >>> import numpy as np >>> a = torch.tensor([1, 2, 3, 6]) >>> b = torch.tensor([0, 2, 3, 7]) >>> np.intersect1d(a, b) array([2, 3]) It seems that tensorflow 2.0 has a function named tf.sets.intersection
🌐
GitHub
github.com › vinija › AI-Coding › blob › main › SparseVectors.py
AI-Coding/SparseVectors.py at main · vinija/AI-Coding
common_indices = torch.intersect1d(self.indices, vec.indices) · # Compute the dot product for common indices · result = 0 · for i in common_indices: result += self.values[i] * vec.values[i] · return result ·
Author: vinija
🌐
GitHub
github.com › multimodallearning › pytorch-mask-rcnn › blob › master › model.py
pytorch-mask-rcnn/model.py at master · multimodallearning/pytorch-mask-rcnn
December 23, 2021 - unique_bool = torch.cat((first_element, unique_bool),dim=0) return tensor[unique_bool.data] · def intersect1d(tensor1, tensor2): aux = torch.cat((tensor1, tensor2),dim=0) aux = aux.sort()[0] return aux[:-1][(aux[1:] == aux[:-1]).data] ·
Author: multimodallearning
🌐
Heekangpark
heekangpark.github.io › ml › shorts › pytorch-intersection-of-two-tensors
[pytorch] 두 텐서 간 교집합(Intersection) 구하기 | Reinventing the Wheel
August 21, 2020 - NumPy에서 제공하는 np.intersect1d() 메소드를 이용할 수 있다. ... import torch import numpy as np def getIntersection_Method1(a, b): a = a.detach().cpu().numpy() b = b.detach().cpu().numpy() intersection = np.intersect1d(a, b) return torch.from_numpy(intersection)
🌐
Cogdl
docs.cogdl.ai › en › 0.3.0 › _modules › cogdl › datasets › strategies_data.html
cogdl.datasets.strategies_data — CogDL 0.3.0 documentation
0 is no data # or negative, 1 is ... _, node_feature_indices, _ = np.intersect1d(allowable_features_pretrain, go_labels, return_indices=True) for idx in node_feature_indices: pretrain_go_node_feature[idx] = 1 data.go_target_downstream = torch.tensor(np.array(downstream_go_no...
Find elsewhere
🌐
51CTO
blog.51cto.com › u_16175524 › 6877394
PyTorch Intersection()的实现 - 51CTO博客
July 28, 2023 - The security policy of this website has blocked you from further access. Contact the site owner and provide the Request ID shown on this page · If you are the owner of this website: Refer to the EdgeOne Web Security Analytics documentation for further instructions
🌐
51CTO
blog.51cto.com › u_16213321 › 12489392
pytorch筛选两个tensor的交集 - 51CTO博客
November 8, 2024 - The security policy of this website has blocked you from further access. Contact the site owner and provide the Request ID shown on this page · If you are the owner of this website: Refer to the EdgeOne Web Security Analytics documentation for further instructions
🌐
PyTorch Forums
discuss.pytorch.org › t › intersection-between-to-vectors-tensors › 50364 › 8
Intersection between to vectors/tensors - #8 by Deeply - PyTorch Forums
February 17, 2021 - Not sure if this would help, as the code hoovers over all t2 elements in a for-loop. Hence, would not benefit from the GPU. In fact, numpy intersect is much faster. def tensor_intersect(t1, t2): t1=t1.cuda() t2=t2.cuda() indices = torch.zeros_like(t1, dtype = torch.bool, device = 'cuda') for elem in t2: indices = indices | (t1 == elem) intersection = t1[indices] return intersection t1= np.random.randint( 1,1e9, 10000) t2= np.random.randint( 1,1e9, 10000...
🌐
Python
docs.scvi-tools.org › en › 1.1.4 › tutorials › notebooks › spatial › DestVI_tutorial.html
Multi-resolution deconvolution of spatial transcriptomics — scvi-tools
# filter genes to be the same on the spatial data intersect = np.intersect1d(sc_adata.var_names, st_adata.var_names) st_adata = st_adata[:, intersect].copy() sc_adata = sc_adata[:, intersect].copy() G = len(intersect)
🌐
Pygod
docs.pygod.org › en › latest › _modules › pygod › detector › scan.html
pygod.detector.scan - PyGOD 1.1.0 documentation
[docs] def fit(self, data, label=None): ... return self def _similarity(self, u, v): u_set = torch.unique(self._neighbors(u)) v_set = torch.unique(self._neighbors(v)) inter = np.intersect1d(v_set, u_set) if len(inter) == 0: return 0 # need to account for vertex itself, add 2(1 for ...
🌐
Hugging Face
huggingface.co › spaces › Doubiiu › DynamiCrafter › resolve › 8631f1e082952f8838b89332ea615a27342c1b3b › lvdm › models › ddpm3d.py
Huggingface
as_cumprod, t, x.shape) * noise ... noise = torch.randn_like(x_start) x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise) diffusion_row.append(x_noisy) log["diffusion_row"] = self._get_rows_from_list(diffusion_row) if sample: # get denoise row with self.ema_scope("Plotting"): samples, denoise_row = self.sample(batch_size=N, return_intermediates=True) log["samples"] = samples log["denoise_row"] = self._get_rows_from_list(denoise_row) if return_keys: if np.intersect1d(list(log.keys()), ...
🌐
GitHub
github.com › aliutkus › torchinterp1d
GitHub - aliutkus/torchinterp1d: 1D interpolation for pytorch · GitHub
This repository implements an interp1d function that overrides torch.autograd.Function, enabling linear 1D interpolation on the GPU for Pytorch.
Author: aliutkus
🌐
GitHub
github.com › vchoutas › torch-mesh-isect
GitHub - vchoutas/torch-mesh-isect · GitHub
Contribute to vchoutas/torch-mesh-isect development by creating an account on GitHub.
Author: vchoutas
🌐
Torchdrivesim
docs.torchdrivesim.org › en › latest › autoapi › torchdrivesim › _iou_utils › index.html
torchdrivesim._iou_utils - torchdrivesim 0.1.0 documentation
torchdrivesim._iou_utils.build_vertices(corners1: torch.Tensor, corners2: torch.Tensor, c1_in_2: torch.Tensor, c2_in_1: torch.Tensor, inters: torch.Tensor, mask_inter: torch.Tensor) → Tuple[torch.Tensor, torch.Tensor][source]¶