-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (97 loc) · 3.45 KB
/
main.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
126
import numpy as np
import matplotlib.pyplot as plt
import torch
from mdn import MDN
# global variables
n_samples = 1000
tes_samples = 1000
n_gaussian = 5
def singleGaussian(x, mu, sigma):
a = (2*np.pi*sigma)**0.5
b = np.exp(-1*(x-mu)**2/(2*sigma))
return b/a
def mdn_loss(y, mu, sigma, pi):
m = torch.distributions.Normal(loc=mu, scale=sigma)
loss = torch.exp(m.log_prob(y))
loss = torch.sum(loss * pi, dim=1)
loss = -torch.log(loss)
return torch.mean(loss)
def main():
# paraper the data
epsilon = torch.randn(n_samples)
x_data = torch.linspace(-10, 10, n_samples)
y_data = 7 * torch.sin(0.75 * x_data) + 0.5 * x_data + 1.*epsilon
y_data, x_data = x_data.view(-1, 1), y_data.view(-1, 1) # inverse the x and y
x_test = torch.linspace(-15, 15, tes_samples).view(-1, 1)
model = MDN(n_hidden=20, n_gaussian=n_gaussian)
model.train()
optimizer = torch.optim.Adam(model.parameters())
for epoch in range(15000):
pi, mu, sigma = model(x_data)
loss = mdn_loss(y_data, mu, sigma, pi)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 500 == 0:
print(epoch, loss.data.tolist())
validate(model, x_test, x_data, y_data, epoch//500, loss.data.numpy(),
savefig=False)
def validate(model, x_test, x_data, y_data, epoch, loss, savefig=False):
model.eval()
pi, mu, sigma = model(x_test)
# print(pi, mu, sigma)
k = torch.multinomial(pi, 1).view(-1) # select one with probability
y_pred = torch.normal(mu, sigma)[np.arange(tes_samples), k].data
# print(k)
# plot the figures
color_list = ['r-', 'b-', 'g-', 'k-', 'y-', 'm-', 'c-', 'r--']
plt.figure(figsize=(12, 7))
plt.subplot(231)
plt.scatter(x_data, y_data, alpha=0.4)
plt.xlabel('Training Data', fontsize=15)
plt.subplot(232)
plt.title('Epoch: %.1fk, Loss:%.4f' % (epoch / 2., loss), fontsize=16)
plt.scatter(x_data, y_data, alpha=0.4)
plt.scatter(x_test, y_pred, alpha=0.4, color='red')
plt.xlabel('Predicted Results', fontsize=15)
plt.subplot(233)
for i in range(n_gaussian):
plt.plot(30.*np.arange(tes_samples)/float(tes_samples)-15.,
pi.data.numpy()[:, i], color_list[i])
plt.xlabel(r'$\alpha(i)$', fontsize=15)
xx = np.linspace(-15, 15, n_samples)
plt.subplot(234)
pi, mu, sigma = model(torch.Tensor([-10.]))
for i in range(n_gaussian):
p = pi.data.numpy()[i]
m = mu.data.numpy()[i]
s = sigma.data.numpy()[i]
out = singleGaussian(xx, m, s)
plt.plot(xx, out, color_list[i])
plt.xlabel('x=-10', fontsize=15)
plt.subplot(235)
pi, mu, sigma = model(torch.Tensor([0.]))
for i in range(n_gaussian):
p = pi.data.numpy()[i]
m = mu.data.numpy()[i]
s = sigma.data.numpy()[i]
out = singleGaussian(xx, m, s)
plt.plot(xx, out, color_list[i])
plt.xlabel('x=0', fontsize=15)
plt.subplot(236)
pi, mu, sigma = model(torch.Tensor([10.]))
for i in range(n_gaussian):
p = pi.data.numpy()[i]
m = mu.data.numpy()[i]
s = sigma.data.numpy()[i]
out = singleGaussian(xx, m, s)
plt.plot(xx, out, color_list[i])
plt.xlabel('x=10', fontsize=15)
plt.tight_layout()
if savefig:
plt.savefig('epoch:%.1fk.png' % (epoch/2.), dpi=200)
plt.close()
else:
plt.show()
if __name__=='__main__':
main()