-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathclassification.py
77 lines (52 loc) · 1.96 KB
/
classification.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Example of classifying data using topological layers."""
from torch_topological.datasets import SphereVsTorus
from torch_topological.nn.data import make_tensor
from torch_topological.nn import VietorisRipsComplex
from torch_topological.nn.layers import StructureElementLayer
from torch.utils.data import DataLoader
from tqdm import tqdm
import torch
class TopologicalModel(torch.nn.Module):
def __init__(self, n_elements, latent_dim=64, output_dim=2):
super().__init__()
self.n_elements = n_elements
self.latent_dim = latent_dim
self.model = torch.nn.Sequential(
StructureElementLayer(self.n_elements),
torch.nn.Linear(self.n_elements, self.latent_dim),
torch.nn.ReLU(),
torch.nn.Linear(self.latent_dim, output_dim),
)
self.vr = VietorisRipsComplex(dim=0)
def forward(self, x):
pers_info = self.vr(x)
pers_info = make_tensor(pers_info)
return self.model(pers_info)
if __name__ == "__main__":
batch_size = 32
n_epochs = 50
n_elements = 10
data_set = SphereVsTorus(n_point_clouds=2 * batch_size)
loader = DataLoader(
data_set,
batch_size=batch_size,
shuffle=True,
drop_last=False,
)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = TopologicalModel(n_elements).to(device)
loss_fn = torch.nn.CrossEntropyLoss()
opt = torch.optim.Adam(model.parameters(), lr=1e-4)
progress = tqdm(range(n_epochs))
for epoch in progress:
for batch, (x, y) in enumerate(loader):
x = x.to(device)
y = y.to(device)
output = model(x)
loss = loss_fn(output, y)
opt.zero_grad()
loss.backward()
opt.step()
pred = torch.argmax(output, dim=1)
acc = (pred == y).sum() / len(y)
progress.set_postfix(loss=f"{loss.item():.08f}", acc=f"{acc:.02f}")