-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributions.py
More file actions
59 lines (44 loc) · 1.43 KB
/
Copy pathdistributions.py
File metadata and controls
59 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# generators for a variety of difficult probability distributions
import torch
from torch.utils.data import IterableDataset
class CircleSampler(IterableDataset):
"""
sample uniformly from unit circle
"""
def __init__(self):
super().__init__()
def __iter__(self):
return self
def __next__(self) -> torch.FloatTensor:
theta = 2.0 * torch.pi * torch.rand(1)
return torch.tensor([torch.cos(theta), torch.sin(theta)])
class TwoMoonsSampler(IterableDataset):
"""
sample uniformly from the classic two-moons data set
"""
def __init__(self):
super().__init__()
self.circle_sampler = CircleSampler()
def __iter__(self):
return self
def __next__(self) -> torch.FloatTensor:
offset = torch.Tensor([0.5, 0.25])
output = next(self.circle_sampler)
if output[1] >= 0.0:
output -= offset
else:
output += offset
return output
class SpiralSampler(IterableDataset):
"""
Note: this samples from a spiral uniformly by angle, NOT by length
"""
def __init__(self, num_wraps: int = 3):
super().__init__()
self.num_wraps = num_wraps
def __iter__(self):
return self
def __next__(self) -> torch.FloatTensor:
t = torch.rand(1)
theta = self.num_wraps * 2.0 * torch.pi * t
return t * torch.tensor([torch.cos(theta), torch.sin(theta)])