-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1_starting.py
121 lines (105 loc) · 3.73 KB
/
1_starting.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import torch
import torchvision
from torch.utils.data import DataLoader
import matterhorn_pytorch.snn as snn
import argparse
from functions import *
from rich import print
def main():
print_title("Example 1", style = "bold blue")
print_title("Hyper Parameters")
parser = argparse.ArgumentParser()
parser.add_argument("--time-steps", type = int, default = 128, help = "Time steps.")
parser.add_argument("--batch-size", type = int, default = 64, help = "Batch size.")
parser.add_argument("--device", type = str, default = "cpu", help = "Device for running the models.")
parser.add_argument("--epochs", type = int, default = 100, help = "Training epochs.")
parser.add_argument("--learning-rate", type = float, default = 0.01, help = "Learning rate.")
parser.add_argument("--momentum", type = float, default = 0.9, help = "Momentum for optimizer.")
parser.add_argument("--tau-m", type = float, default = 2.0, help = "Membrane constant.")
args = parser.parse_args()
time_steps = args.time_steps
batch_size = args.batch_size
device = torch.device(args.device)
dtype = torch.float
epochs = args.epochs
learning_rate = args.learning_rate
momentum = args.momentum
tau = args.tau_m
print_params({
"Time Steps": time_steps,
"Batch Size": batch_size,
"Epochs": epochs,
"Learning Rate": learning_rate,
"Momentum": momentum,
"Tau m": tau
})
print_title("Model")
model = snn.Sequential(
snn.PoissonEncoder(
time_steps = time_steps,
),
snn.Flatten(),
snn.Linear(28 * 28, 80, bias = False),
snn.LIF(),
snn.Linear(80, 10, bias = False),
snn.LIF(),
snn.AvgSpikeDecoder()
).multi_step_mode_()
model = model.to(device = device, dtype = dtype)
print_model(model)
print_title("Dataset")
train_dataset = torchvision.datasets.MNIST(
root = "./examples/data",
train = True,
transform = torchvision.transforms.ToTensor(),
download=True
)
test_dataset = torchvision.datasets.MNIST(
root = "./examples/data",
train = False,
transform = torchvision.transforms.ToTensor(),
download = True
)
train_data_loader = DataLoader(
dataset = train_dataset,
batch_size = batch_size,
shuffle = True,
drop_last = True,
pin_memory = True
)
test_data_loader = DataLoader(
dataset = test_dataset,
batch_size = batch_size,
shuffle = True,
drop_last = True,
pin_memory = True
)
print_dataset(test_dataset)
print_title("Preparations for Training")
def loss_fn(o: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
return torch.nn.functional.cross_entropy(o.float(), y.long())
optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate)
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer = optimizer, T_max = epochs)
log_dir = "./examples/logs"
sub_dir = "1_starting" + "_" + datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
log_dir = os.path.join(log_dir, sub_dir)
init_logs(
log_dir = log_dir,
args = args,
model = model
)
print_title("Training")
train_and_test(
epochs = epochs,
model = model,
train_data_loader = train_data_loader,
test_data_loader = test_data_loader,
loss_fn = loss_fn,
optimizer = optimizer,
scheduler = lr_scheduler,
log_dir = log_dir,
device = device,
dtype = dtype
)
if __name__ == "__main__":
main()