-
Notifications
You must be signed in to change notification settings - Fork 28
/
sf_sinewave.py
180 lines (116 loc) · 4.3 KB
/
sf_sinewave.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 19 23:44:18 2018
@author: chinwei
"""
import numpy as np
import torch
import torch.optim as optim
from torch.autograd import Variable
from torch.nn.parameter import Parameter
from torchkit import nn as nn_, flows, utils
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
class model_1d(object):
def __init__(self, target_energy):
nd=24
self.sf = flows.SigmoidFlow(num_ds_dim=nd)
self.params = Parameter(torch.FloatTensor(1, 1, 3*nd).normal_())
self.optim = optim.Adam([self.params,], lr=0.01,
betas=(0.9, 0.999))
self.target_energy = target_energy
def sample(self, n):
spl = Variable(torch.FloatTensor(n,1).normal_())
lgd = Variable(torch.from_numpy(
np.zeros(n).astype('float32')))
h, logdet = self.sf.forward(spl, lgd, self.params)
out = nn_.sigmoid_(h) * 2.0
logdet = logdet + nn_.logsigmoid(h) + nn_.logsigmoid(-h) + np.log(2.0)
return out, logdet
def train(self):
total = 20000
for it in range(total):
self.optim.zero_grad()
w = min(1.0,0.2+it/float((total*0.80)))
spl, logdet = self.sample(320)
losses = - w * self.target_energy(spl) - logdet
loss = losses.mean()
loss.backward()
self.optim.step()
if ((it + 1) % 1000) == 0:
print 'Iteration: [%4d/%4d] loss: %.8f' % \
(it+1, total, loss.data[0])
# =============================================================================
# =============================================================================
# sine wave
class Sinewave(object):
def __init__(self, a, f, phi):
self.a = a
self.f = f
self.phi = phi
def evaluate(self, t):
# y(t) = a sin(2pi f t + phi)
return self.a * np.sin(2*np.pi*self.f*t + self.phi)
xx = np.linspace(0,2,1000)
fig = plt.figure()
ax = plt.subplot(111)
sw1 = Sinewave(1.0, 0.6, 0.0)
yy1 = sw1.evaluate(xx)
plt.plot(xx,yy1,':')
sw2 = Sinewave(1.0, 1.2, 0.0)
yy2 = sw2.evaluate(xx)
plt.plot(xx,yy2,'--')
sw3 = Sinewave(1.0, 1.8, 0.0)
yy3 = sw3.evaluate(xx)
plt.plot(xx,yy3)
a0 = 1.0
f0 = 0.6
b0 = 0.0
x0 = utils.varify(np.array([[0.0],[5/6.],[10/6.]]).astype('float32'))
y0 = torch.mul(torch.sin(x0*2.0*np.pi*f0+b0), a0)
plt.scatter(x0.data.numpy()[:,0],y0.data.numpy()[:,0], color=(0.8,0.4,0.4),
marker='x',s=100)
plt.rc('font', family='serif')
plt.title(r'$y(t) = \sin(2\pi f t)$', fontsize=18)
leg = plt.legend(['$f$=0.6','$f$=1.2','$f$=1.8'],
loc=4, fontsize=20)
plt.xlabel('t', fontsize=15)
plt.ylabel('y(t)', fontsize=15)
plt.tight_layout()
plt.savefig('sinewave_fs.pdf',format='pdf')
# =============================================================================
# =============================================================================
# Define energy function
# inferring q(f|(xi,yi)_i=1^3)
zero = Variable(torch.FloatTensor(1).zero_())
def energy1(f):
mu = torch.mul(torch.sin(x0.permute(1,0)*2.0*np.pi*f+b0), a0)
return - ((mu-y0.permute(1,0))**2 * (1/0.25)).sum(1)
ll = utils.log_normal(y0.permute(1,0),mu,zero).sum(1)
return ll
# =============================================================================
# =============================================================================
# build and train
mdl = model_1d(energy1)
mdl.train()
# =============================================================================
# =============================================================================
n = 10000
# plot figure
fig = plt.figure()
ax = fig.add_subplot(111)
spl = mdl.sample(n)[0]
plt.hist(spl.data.numpy()[:,0],100)
plt.xlabel('f', fontsize=15)
ax.set_yticklabels([])
plt.title('$f\sim q(f)$', fontsize=18)
plt.grid()
#plt.savefig('sinewave_qf.pdf',format='pdf')
spl = spl.data.numpy()
mdl_density = gaussian_kde(spl[:,0],0.05)
xx = np.linspace(0,2,1000)
plt.plot(xx,300*mdl_density(xx),'r')
plt.tight_layout()
plt.legend(['kde', 'counts'], loc=2, fontsize=20)
plt.savefig('sinewave_qf+kde.pdf',format='pdf')