-
Notifications
You must be signed in to change notification settings - Fork 0
/
ANN_autoencoder.py
126 lines (91 loc) · 3.09 KB
/
ANN_autoencoder.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
122
123
124
125
#import required libraries
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.init as init
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
# set hyperparameters
batch_size = 256
learning_rate = 0.0002
num_epoch = 10
#Download data
mnist_train = dset.MNIST("./", train=True, transform=transforms.ToTensor(), target_transform=None, download=True)
mnist_test = dset.MNIST("./", train=False, transform=transforms.ToTensor(), target_transform=None, download=True)
#set dataLoader
train_loader = torch.utils.data.DataLoader(mnist_train,batch_size=batch_size, shuffle=True,num_workers=2,drop_last=True)
test_loader = torch.utils.data.DataLoader(mnist_test,batch_size=batch_size, shuffle=False,num_workers=2,drop_last=True)
# model
class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
self.encoder = nn.Linear(28 * 28, 20)
self.decoder = nn.Linear(20, 28 * 28)
def forward(self, x):
x = x.view(batch_size, -1)
encoded = self.encoder(x)
out = self.decoder(encoded).view(batch_size, 1, 28, 28)
return out
# Loss function & Optimizer
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
model = Autoencoder().to(device)
loss_func = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train
loss_arr = []
for i in range(num_epoch):
for j, [image, label] in enumerate(train_loader):
x = image.to(device)
optimizer.zero_grad()
output = model.forward(x)
loss = loss_func(output, x)
loss.backward()
optimizer.step()
if j % 1000 == 0:
print(loss)
loss_arr.append(loss.cpu().data.numpy()[0])
# Check with Train Image
out_img = torch.squeeze(output.cpu().data)
print(out_img.size())
for i in range(3):
plt.subplot(1,2,1)
plt.imshow(torch.squeeze(image[i]).numpy(),cmap='gray')
plt.subplot(1,2,2)
plt.imshow(out_img[i].numpy(),cmap='gray')
plt.show()
# Check With Test Image
with torch.no_grad():
for i in range(1):
for j,[image,label] in enumerate(test_loader):
x = image.to(device)
optimizer.zero_grad()
output = model.forward(x)
if j % 1000 == 0:
print(loss)
out_img = torch.squeeze(output.cpu().data)
print(out_img.size())
for i in range(2):
plt.subplot(1,2,1)
plt.imshow(torch.squeeze(image[i]).numpy(),cmap='gray')
plt.subplot(1,2,2)
plt.imshow(out_img[i].numpy(),cmap='gray')
plt.show()
with torch.no_grad():
for j, [image, label] in enumerate(test_loader):
image = image.to(device)
output = encoder(image)
output = decoder(output)
if j % 10 == 0:
print(loss)
out_img = torch.squeeze(output.cpu().data)
print(out_img.size())
for i in range(2):
plt.subplot(1, 2, 1)
plt.imshow(torch.squeeze(image[i]).cpu().numpy(), cmap='gray')
plt.subplot(1, 2, 2)
plt.imshow(out_img[i].numpy(), cmap='gray')
plt.show()