Added a public domain header to all the sources.
[pytorch.git] / denoising-ae-field.py
1 #!/usr/bin/env python
2
3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
5
6 # Written by Francois Fleuret <francois@fleuret.org>
7
8 import math
9 import matplotlib.pyplot as plt
10
11 import torch
12 from torch import nn
13
14 ######################################################################
15
16 def data_zigzag(nb):
17     a = torch.empty(nb).uniform_(0, 1).view(-1, 1)
18     # zigzag
19     x = 0.4 * ((a-0.5) * 5 * math.pi).cos()
20     y = a * 2.5 - 1.25
21     data = torch.cat((y, x), 1)
22     data = data @ torch.tensor([[1., -1.], [1., 1.]])
23     return data, 'zigzag'
24
25 def data_spiral(nb):
26     a = torch.empty(nb).uniform_(0, 1).view(-1, 1)
27     x = (a * 2.25 * math.pi).cos() * (a * 0.8 + 0.5)
28     y = (a * 2.25 * math.pi).sin() * (a * 0.8 + 0.5)
29     data = torch.cat((y, x), 1)
30     return data, 'spiral'
31
32 def data_penta(nb):
33     a = (torch.randint(5, (nb,)).float() / 5 * 2 * math.pi).view(-1, 1)
34     x = a.cos()
35     y = a.sin()
36     data = torch.cat((y, x), 1)
37     data = data + data.new(data.size()).normal_(0, 0.05)
38     return data, 'penta'
39
40 ######################################################################
41
42 def train_model(data):
43     model = nn.Sequential(
44         nn.Linear(2, 100),
45         nn.ReLU(),
46         nn.Linear(100, 2)
47     )
48
49     batch_size, nb_epochs = 100, 1000
50     optimizer = torch.optim.Adam(model.parameters(), lr = 1e-3)
51     criterion = nn.MSELoss()
52
53     for e in range(nb_epochs):
54         acc_loss = 0
55         for input in data.split(batch_size):
56             noise = input.new(input.size()).normal_(0, 0.1)
57             output = model(input + noise)
58             loss = criterion(output, input)
59             acc_loss += loss.item()
60             optimizer.zero_grad()
61             loss.backward()
62             optimizer.step()
63         if (e+1)%100 == 0: print(e+1, acc_loss)
64
65     return model
66
67 ######################################################################
68
69 def save_image(data_name, model, data):
70     a = torch.linspace(-1.5, 1.5, 30)
71     x = a.view( 1, -1, 1).expand(a.size(0), a.size(0), 1)
72     y = a.view(-1,  1, 1).expand(a.size(0), a.size(0), 1)
73     grid = torch.cat((y, x), 2).view(-1, 2)
74
75     # Take the origins of the arrows on the part of the grid closer than
76     # sqrt(0.1) to the data points
77     dist = (grid.view(-1, 1, 2) - data.view(1, -1, 2)).pow(2).sum(2).min(1)[0]
78     origins = grid[torch.arange(grid.size(0)).masked_select(dist < 0.1)]
79
80     field = model(origins).detach() - origins
81
82     fig = plt.figure()
83     ax = fig.add_subplot(1, 1, 1)
84
85     ax.axis('off')
86     ax.set_xlim(-1.6, 1.6)
87     ax.set_ylim(-1.6, 1.6)
88     ax.set_aspect(1)
89
90     plot_field = ax.quiver(
91         origins[:, 0].numpy(), origins[:, 1].numpy(),
92         field[:, 0].numpy(), field[:, 1].numpy(),
93         units = 'xy', scale = 1,
94         width = 3e-3, headwidth = 25, headlength = 25
95     )
96
97     plot_data = ax.scatter(
98         data[:, 0].numpy(), data[:, 1].numpy(),
99         s = 1, color = 'tab:blue'
100     )
101
102     filename = f'denoising_field_{data_name}.pdf'
103     print(f'Saving {filename}')
104     fig.savefig(filename, bbox_inches='tight')
105
106 ######################################################################
107
108 for data_source in [ data_zigzag, data_spiral, data_penta ]:
109     data, data_name = data_source(1000)
110     data = data - data.mean(0)
111     model = train_model(data)
112     save_image(data_name, model, data)