-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
156 lines (117 loc) · 4.05 KB
/
main.py
File metadata and controls
156 lines (117 loc) · 4.05 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from utils import Model, Encoder, Decoder,View, show_image, sampling
from torchvision.datasets import MNIST
import torchvision.transforms as transforms
import os.path
import numpy as np
import torch as pt
pt.manual_seed(2022)
def model_acc(loader):
# mlp.eval()
correct = 0
total = 0
with pt.no_grad():
for x, y in loader:
x = x.view(batch_size, x_dim)
_, mu, log_var = model(x)
latent = sampling(mu, pt.exp(0.5 * log_var), samples)
outputs = mlp(latent)
_, predicted = pt.max(outputs.data, 1)
total += y.size(0)
correct += (predicted == y.to(DEVICE)).sum().item()
if loader == train_loader:
print(f"Accuracy on train data: {100 * correct // total} %")
else:
print(f"Accuracy on test data: {100 * correct // total} %")
dataset_path = "~/datasets"
cuda = True
DEVICE = pt.device("cuda" if cuda else "cpu")
batch_size = 1
x_dim = 784
hidden_dim = 400
latent_dim = 2
lr = 0.001
epochs = 1000
samples = 10
mnist_transform = transforms.Compose(
[
transforms.ToTensor(),
]
)
kwargs = {"num_workers": 1, "pin_memory": True}
train_dataset = MNIST(
dataset_path, transform=mnist_transform, train=True, download=True
)
test_dataset = MNIST(
dataset_path, transform=mnist_transform, train=False, download=True
)
# randomly select 100 samples from the dataset
# Please comment out below two lines if you want to train the model with full mnist
# train_dataset.data = train_dataset.data[pt.randperm(len(train_dataset))[0:100]]
# test_dataset.data = test_dataset.data[pt.randperm(len(test_dataset))[0:50]]
train_loader = pt.utils.data.DataLoader(
dataset=train_dataset, batch_size=batch_size, shuffle=True, **kwargs
)
test_loader = pt.utils.data.DataLoader(
dataset=test_dataset, batch_size=batch_size, shuffle=False, **kwargs
)
# Initializing models
encoder = Encoder(input_dim=x_dim, hidden_dim=hidden_dim, latent_dim=latent_dim)
decoder = Decoder(
latent_dim=latent_dim, hidden_dim=hidden_dim, output_dim=x_dim
)
model = Model(Encoder=encoder, Decoder=decoder).to(DEVICE)
# creating a mlp model
mlp = pt.nn.Sequential(
View((1, latent_dim * samples)),
pt.nn.Linear(latent_dim * samples, (latent_dim * samples) * 2),
pt.nn.LayerNorm((latent_dim * samples) * 2),
pt.nn.ReLU(),
pt.nn.Dropout(0.0),
pt.nn.Linear((latent_dim * samples) * 2, (latent_dim * samples) * 2),
pt.nn.LayerNorm((latent_dim * samples) * 2),
pt.nn.ReLU(),
pt.nn.Dropout(0.0),
pt.nn.Linear((latent_dim * samples) * 2, (latent_dim * samples) * 2),
pt.nn.LayerNorm((latent_dim * samples) * 2),
pt.nn.ReLU(),
pt.nn.Dropout(0.0),
pt.nn.Linear((latent_dim * samples) * 2, (latent_dim * samples) * 2),
pt.nn.LayerNorm((latent_dim * samples) * 2),
pt.nn.ReLU(),
pt.nn.Dropout(0.0),
pt.nn.Linear((latent_dim * samples) * 2, 10),
)
print(mlp)
# Load model
model.load_state_dict(pt.load("model.pth", map_location=DEVICE))
for param in model.parameters():
param.requires_grad = False
if os.path.exists("mlp.pth"):
mlp.load_state_dict(pt.load("mlp.pth", map_location=DEVICE))
# optimizer = pt.optim.Adam(mlp.parameters(), lr=lr)
optimizer = pt.optim.SGD(mlp.parameters(), lr=lr)
loss_function = pt.nn.CrossEntropyLoss()
model.eval()
mlp.train()
losses = []
for e in range(epochs):
for batch_idx, (x, y) in enumerate(train_loader):
x = x.view(batch_size, x_dim)
x = x.to(DEVICE)
y = y.to(DEVICE)
with pt.no_grad():
_, mu, log_var = model(x)
optimizer.zero_grad()
# sampling n latents
latent = sampling(mu, pt.exp(0.5 * log_var), samples)
output = mlp(latent).to(DEVICE)
loss = loss_function(output, y)
loss.backward()
losses.append(loss.item())
optimizer.step()
if e % 50 == 0:
print("epoch: ", str(e), "Avg Loss: ", pt.tensor(losses).mean())
pt.save(mlp.state_dict(), "mlp.pth")
# Prediction
model_acc(train_loader)
model_acc(test_loader)