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 Top answer 1 of 6
28
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]
2 of 6
3
if you don't want to leave cuda, a workaround could be:
t1 = torch.tensor([1, 9, 12, 5, 24], device = 'cuda')
t2 = torch.tensor([1, 24], device = 'cuda')
indices = torch.ones_like(t1, dtype = torch.uint8, device = 'cuda')
for elem in t2:
indices = indices & (t1 != elem)
intersection = t1[indices]
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
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...
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()), ...
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]¶