-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.py
More file actions
115 lines (89 loc) · 3.28 KB
/
Solver.py
File metadata and controls
115 lines (89 loc) · 3.28 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
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision import transforms
from torchvision.datasets import MNIST
from torch.utils.data import DataLoader
import tqdm
import autoencoders
def train(model, train_loader, num_epochs,
criterion, optimizer, DEVICE):
"""Train the model in an Unsupervised manner.
Args:
model: model to train.
train_loader: train data loader.
epoch:
num_epochs: number of epochs.
criterion: loss function to optimize.
optimizer: optimizer for weight update.
DEVICE: device to run the computation on.
Returns:
A list of loss value for each epoch
"""
model.train()
train_loss_history = []
for epoch in range(num_epochs):
batch_loss_history = []
progress_bar = tqdm.tqdm(train_loader)
for x, _ in progress_bar:
optimizer.zero_grad()
x = x.to(DEVICE)
y_hat = model(x)
loss = criterion(y_hat, x)
loss.backward()
optimizer.step()
batch_loss_history.append(loss.item())
progress_bar.set_description(
'Epoch {}/{}'.format(epoch + 1, num_epochs))
progress_bar.set_postfix(loss=np.mean(batch_loss_history))
train_loss_history.append(np.mean(batch_loss_history))
return train_loss_history
def test(model, test_loader, criterion, DEVICE):
"""Compute model performance over test data.
Args:
model: model to train.
test_loader: test data loader.
criterion: loss function.
DEVICE: device to run the computation on.
Returns:
Loss value over the test data.
"""
model.eval()
loss_history = []
for x, _ in test_loader:
x = x.to(DEVICE)
output = model(x)
loss = criterion(output, x)
loss_history.append(loss.item())
return np.mean(loss_history)
def main():
num_epochs = 100
batch_size = 128
learning_rate = 1e-3
train_dataset = MNIST('./data',
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]),
download=True,
train=True)
train_dataloader = DataLoader(
train_dataset, batch_size=batch_size, shuffle=True)
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = autoencoders.AutoEncoder().to(DEVICE)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(),
lr=learning_rate,
weight_decay=1e-5)
train_loss_history = train(model, train_dataloader,
num_epochs, criterion, optimizer, DEVICE)
test_dataset = MNIST('./data',
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]),
download=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size)
test_loss = test(model, test_loader, criterion, DEVICE)
print(np.mean(test_loss))
torch.save(model.state_dict(), './saved_models/MNIST_ConvAutoEncoder.pth')
if __name__ == '__main__':
main()