-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoencoder.py
103 lines (86 loc) · 2.71 KB
/
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
import torch
from torch._C import device
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
# device = torch.device("cuda:0")
transform = transforms.ToTensor()
mnist_data = datasets.MNIST(
root=".\data", train=True, download=True, transform=transform
)
data_loader = torch.utils.data.DataLoader(
dataset=mnist_data, batch_size=5000, shuffle=True
)
# train_data -> data
# train_labels -> targets
# mnist_data.data = mnist_data.data.to(device)
# mnist_data.targets = mnist_data.targets.to(device)
# dataiter = iter(data_loader)
# images, labels = dataiter.next()
# # 了解取值范围,方便后续使用正确的激活函数,
# print(torch.min(images), torch.max(images))
class Autoencoder(nn.Module):
def __init__(self) -> None:
super().__init__()
# N(batch_size), 784
self.encoder = nn.Sequential(
nn.Linear(28 * 28, 128), # N,784->128
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 12),
nn.ReLU(),
nn.Linear(12, 3), # N,3
)
self.decoder = nn.Sequential(
nn.Linear(3, 12),
nn.ReLU(),
nn.Linear(12, 64),
nn.ReLU(),
nn.Linear(64, 128),
nn.ReLU(),
nn.Linear(128, 28 * 28),
nn.Sigmoid(), # N,3->N,784
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
model = Autoencoder()
# model.to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
num_epochs = 20
outputs = []
for epoch in range(num_epochs):
# for img in data_loader.dataset.data.cpu():
for (img, _) in data_loader:
# img = img.reshape(-1, 28 * 28).to(device)
img = img.reshape(-1, 28 * 28)
recon = model(img)
loss = criterion(recon, img)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss:{loss.item():.4f}")
outputs.append((epoch, img, recon))
for k in range(0, num_epochs, 4):
plt.figure(figsize=(9, 2))
plt.gray()
imgs = outputs[k][1].detach().numpy()
recon = outputs[k][2].detach().numpy()
for i, item in enumerate(imgs):
if i >= 9:
break
plt.subplot(2, 9, i + 1)
item = item.reshape(-1, 28, 28)
# plt.imshow(item[0])
for i, item in enumerate(recon):
if i >= 9:
break
plt.subplot(2, 9, 9 + i + 1)
item = item.reshape(-1, 28, 28)
# plt.imshow(item[0])
myfig = plt.gcf()
myfig.savefig(".\figure\figure.png", dpi=300)