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
|
import time
import main as mn
import autograd.numpy as np
from autograd import grad
def g(m):
assert (m > 0).all()
return np.log(1 - np.exp(-m))
def h(m):
return -m
def ll(x, s, theta):
"""
x : infected
s : susceptible
"""
res = 0
for t in range(1, x.shape[0]):
w = np.dot(x[t-1], theta)
res += g(w)[x[t]].sum() + h(w)[~x[t] & s[t]].sum()
return res
def sample(params):
mu, v = params
size = mu.shape
return np.maximum(np.random.normal(size=size) * v + mu, 1e-3)
def ll_full(params, x, s, nsamples=50):
return np.mean([ll(x, s, sample(params)) for _ in xrange(nsamples)])
grad_ll_full = grad(ll_full)
def kl(params1, params0):
mu0, sig0 = params0
mu1, sig1 = params1
return np.sum(np.log(sig1/sig0) + (sig0**2 + (mu0 - mu1)**2)/(2*sig1)**2)
grad_kl = grad(kl)
def sgd(mu1, sig1, mu0, sig0, cascades, n_e=100, lr=lambda t: 1e-2):
g_mu1, g_sig1 = grad_kl((mu1, sig1), (mu0, sig0))
for t in xrange(n_e):
lrt = lr(t) # learning rate
mu1, sig1 = mu1 + lrt * g_mu1, sig1 + lrt * g_sig1
for x, s in zip(*cascades):
g_mu1, g_sig1 = grad_ll_full((mu1, sig1), x, s)
mu1 = np.maximum(mu1 + lrt * g_mu1, 0)
sig1 = np.maximum(sig1 + lrt * g_sig1, 1e-3)
res = np.sum(ll_full((mu1, sig1), x, s) for x, s in zip(*cascades)) + \
kl((mu1, sig1), (mu0, sig0))
print("Epoch: {}\t LB: {}\t Time: {}".format(t, res, time.time()))
print mu1
print sig1
if __name__ == '__main__':
graph = np.array([[0, 0, 1], [0, 0, 0.5], [0, 0, 0]])
p = 0.5
graph = np.log(1. / (1 - p * graph))
cascades = mn.build_cascade_list(mn.simulate_cascades(1000, graph))
mu0, sig0 = (1. + .2 * np.random.normal(size=graph.shape),
1 + .2 * np.random.normal(size=graph.shape))
mu1, sig1 = (1. + .2 * np.random.normal(size=graph.shape),
1 + .2 * np.random.normal(size=graph.shape))
sgd(mu1, sig1, mu0, sig0, cascades, n_e=30)
|