MCPcopy Create free account
hub / github.com/PyGCL/PyGCL / multiinstance_mixup

Function multiinstance_mixup

GCL/augmentors/functional.py:64–86  ·  view source on GitHub ↗

Randomly mixup node embeddings or features with nodes from other views. Args: x1: The latent embedding or node feature from one view. x2: The latent embedding or node feature from the other view. alpha: The mixup coefficient `\lambda` follows `Beta(\alpha, \alpha)`.

(x1: torch.Tensor, x2: torch.Tensor,
                        alpha: float, shuffle=False)

Source from the content-addressed store, hash-verified

62
63
64def multiinstance_mixup(x1: torch.Tensor, x2: torch.Tensor,
65 alpha: float, shuffle=False) -> (torch.Tensor, torch.Tensor):
66 """
67 Randomly mixup node embeddings or features with nodes from other views.
68
69 Args:
70 x1: The latent embedding or node feature from one view.
71 x2: The latent embedding or node feature from the other view.
72 alpha: The mixup coefficient `\lambda` follows `Beta(\alpha, \alpha)`.
73 shuffle: Whether to use fixed negative samples.
74
75 Returns:
76 (torch.Tensor, torch.Tensor): Spurious positive samples and the mixup coefficient.
77 """
78 device = x1.device
79 lambda_ = Beta(alpha, alpha).sample([1]).to(device)
80 if shuffle:
81 mixup_idx = get_mixup_idx(x1).to(device)
82 else:
83 mixup_idx = x1.size(0) - torch.arange(x1.size(0)) - 1
84 x_spurious = (1 - lambda_) * x1 + lambda_ * x2[mixup_idx]
85
86 return x_spurious, lambda_
87
88
89def drop_feature(x: torch.Tensor, drop_prob: float) -> torch.Tensor:

Callers

nothing calls this directly

Calls 2

get_mixup_idxFunction · 0.85
sampleMethod · 0.45

Tested by

no test coverage detected