-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoencoder.py
More file actions
59 lines (51 loc) · 1.78 KB
/
autoencoder.py
File metadata and controls
59 lines (51 loc) · 1.78 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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import numpy
from torchvision import datasets
from torchvision import transforms
import matplotlib.pyplot as plt
dataset = datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=False)
loader = DataLoader(dataset, batch_size=32)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class AutoEncoder(nn.Module):
def __init__(self, input_size, hidden_size):
super(AutoEncoder,self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.encoder = nn.Sequential(
nn.Linear(self.input_size, self.hidden_size),
nn.ReLU(),
nn.Linear(self.hidden_size, 3)
)
self.decoder = nn.Sequential(
nn.Linear(3, self.hidden_size),
nn.ReLU(),
nn.Linear(self.hidden_size, self.input_size),
nn.Sigmoid()
)
def forward(self, x):
out = self.encoder(x)
out = self.decoder(out)
return out
model = AutoEncoder(784, 128).to(device=device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(),lr=1e-3,weight_decay=1e-5)
for i in range(3):
for (image, label) in loader:
image=image.to(device=device)
label=label.to(device=device)
output = model(image.reshape(-1,28*28))
loss = criterion(output,image.reshape(-1,784))
loss.backward()
optimizer.step()
optimizer.zero_grad()
print(loss.item())
output=output.cpu().detach().numpy()
image=image.cpu().detach().numpy()
for i in range(6):
plt.subplot(2,6,i+1)
plt.imshow(output.reshape(-1,28,28)[i,:,:])
plt.subplot(2, 6, i+7)
plt.imshow(image.reshape(-1, 28, 28)[i, :, :])
plt.show()