-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
89 lines (68 loc) · 2.6 KB
/
example.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
78
79
80
81
82
83
84
85
86
87
88
89
import torch
from torchvision import datasets, transforms
import random
from nlgm.optimizers import RandomWalkOptimizer
from nlgm.autoencoder import GeometricAutoencoder
from nlgm.train import train_and_evaluate
from nlgm.searchspace import construct_graph_search_space
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Define the data transforms
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
)
# Load the MNIST dataset
train_dataset = datasets.MNIST(
root="./data",
train=True,
download=True,
transform=transform,
)
test_dataset = datasets.MNIST(
root="./data",
train=False,
download=True,
transform=transform,
)
subsample_percent = 0.005
# Calculate the number of samples for 5% of the training data
subsample_size = int(subsample_percent * len(train_dataset))
# Create a random subset of indices for the training data
subset_indices = torch.randperm(len(train_dataset))[:subsample_size]
# Use the subset indices to create the subsampled dataset
subsampled_train_dataset = torch.utils.data.Subset(train_dataset, subset_indices)
# Create data loaders for the subsampled training data and test data
train_loader = torch.utils.data.DataLoader(
subsampled_train_dataset, batch_size=64, shuffle=True
)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)
n_p = 5
# Construct the adjacency matrix and signatures for the graph search space
adjacency_matrix, signatures = construct_graph_search_space(n_p=n_p)
epochs = 10
# Define the objective function
def objective_function(signature):
latent_dim = len(signature) * 2
model = GeometricAutoencoder(signature, latent_dim=latent_dim)
train_losses, test_loss = train_and_evaluate(
model, train_loader, test_loader, epochs=epochs, device=device
)
return train_losses, test_loss
# Perform optimization
evaluated_signatures = []
evaluated_metrics = []
loss_trajectories = []
def callback(signature, metric):
evaluated_signatures.append(signature)
loss_trajectories.append(metric[0])
evaluated_metrics.append(metric[1])
# Create an instance of the RandomWalkOptimizer
optimizer = RandomWalkOptimizer(
adjacency_matrix, signatures, random.randint(0, len(signatures) - 1), None
)
# Optimize the objective function
optimal_signature, optimal_val_metric, optimal_train_metric = (
optimizer.optimize_with_backtracking(objective_function, 10, callback)
)
# Print the optimal signature and validation metric
print("Optimal signature:", optimal_signature)
print("Optimal validation metric:", optimal_val_metric)