diff options
| author | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2015-11-29 17:03:22 -0500 |
|---|---|---|
| committer | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2015-11-29 17:03:22 -0500 |
| commit | 7322c00eafcde38dadbf9d4f05a1572d627355bf (patch) | |
| tree | 45db3a6e4d71b0a17be2a87b49a225d0d430cb1b /simulation/vi_theano.py | |
| parent | 041aa021657a3c290952b222e3141449638bad19 (diff) | |
| download | cascades-7322c00eafcde38dadbf9d4f05a1572d627355bf.tar.gz | |
active learning for mle + variational inf. (not bug-free)
Diffstat (limited to 'simulation/vi_theano.py')
| -rw-r--r-- | simulation/vi_theano.py | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/simulation/vi_theano.py b/simulation/vi_theano.py deleted file mode 100644 index cfd1dcf..0000000 --- a/simulation/vi_theano.py +++ /dev/null @@ -1,110 +0,0 @@ -import main as mn -import theano -from theano import tensor as tsr -import theano.tensor.shared_randomstreams -import numpy as np - -n_cascades = 1000 -n_nodes = 3 -n_samples = 100 -srng = tsr.shared_randomstreams.RandomStreams(seed=123) -lr = 1e-1 -n_epochs = 1 - -###############Variational Inference#################### - -# Declare Theano variables -mu = theano.shared(.5 + .2 * np.random.normal(size=(1, n_nodes, n_nodes)), - name="mu", broadcastable=(True, False, False)) -sig = theano.shared(.1 + .04 * np.random.normal(size=(1, n_nodes, n_nodes)), - name="sig", broadcastable=(True, False, False)) -mu0 = theano.shared(.5 + .2 * np.random.normal(size=(1, n_nodes, n_nodes)), - name="mu", broadcastable=(True, False, False)) -sig0 = theano.shared(.1 + .04 * np.random.normal(size=(1, n_nodes, n_nodes)), - name="sig", broadcastable=(True, False, False)) -x = tsr.matrix(name='x', dtype='int8') -s = tsr.matrix(name='s', dtype='int8') - -# Construct Theano graph -theta = srng.normal((n_samples, n_nodes, n_nodes)) * sig + mu -y = tsr.maximum(tsr.dot(x, theta), 1e-3) -infect = tsr.log(1. - tsr.exp(-y[0:-1])).dimshuffle(1, 0, 2) -lkl_pos = tsr.sum(infect * (x[1:] & s[1:])) / n_samples -lkl_neg = tsr.sum(-y[0:-1].dimshuffle(1, 0, 2) * (~x[1:] & s[1:])) / n_samples -lkl = lkl_pos + lkl_neg -kl = tsr.sum(tsr.log(sig / sig0) + (sig0**2 + (mu0 - mu)**2)/(2*sig)**2) -res = lkl + kl - -gmu, gsig = theano.gradient.grad(lkl, [mu, sig]) -gmukl, gsigkl = theano.grad(kl, [mu, sig]) - -# Compile into functions -loglkl_full = theano.function([x, s], lkl) -train = theano.function(inputs=[x, s], outputs=res, - updates=((mu, tsr.clip(mu + lr * gmu, 0, 1)), - (sig, tsr.clip(sig + lr * gsig, 1e-3, 1)))) -train_kl = theano.function(inputs=[], outputs=[], - updates=((mu, tsr.clip(mu + lr * gmukl, 0, 1)), - (sig, tsr.clip(sig + lr * gsigkl, 1e-3, 1)))) - - -###############Maximum Likelihood##################### - -x = tsr.matrix(name='x', dtype='int8') -s = tsr.matrix(name='s', dtype='int8') -params = theano.shared(.5 + .01*np.random.normal(size=(n_nodes, n_nodes)), - name='params') -y = tsr.maximum(tsr.dot(x, params), 1e-5) -infect = tsr.log(1. - tsr.exp(-y[0:-1])) -lkl_pos = tsr.sum(infect * (x[1:] & s[1:])) -lkl_neg = tsr.sum(-y[0:-1] * (~x[1:] & s[1:])) -lkl_mle = lkl_pos + lkl_neg -gparams = theano.gradient.grad(lkl_mle, params) -train_mle = theano.function(inputs=[x, s], outputs=lkl_mle, updates=[(params, - tsr.clip(params + lr * gparams, 0, 1))]) - - -if __name__ == "__main__": - graph = .5 * np.random.binomial(2, p=.5, size=(n_nodes, n_nodes)) - for k in range(len(graph)): - graph[k, k] = 0 - p = 0.5 - graph = np.log(1. / (1 - p * graph)) - - 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(n_cascades, graph), - collapse=True) - x_obs, s_obs = cascades[0], cascades[1] - - #mle - lkl_plot = [] - if 1: - for i in range(n_epochs): - for k in xrange(len(x_obs)/100): - xt = x_obs[k*100:(k+1)*100] - st = s_obs[k*100:(k+1)*100] - lkl = train_mle(xt, st) - lkl_plot.append(lkl) - print(params.get_value()) - print(graph) - w = params.get_value() - for k in range(len(w)): - w[k, k] = 0 - print(w) - #import matplotlib.pyplot as plt - #plt.plot(lkl_plot) - #plt.show() - - #variational inference - if 0: - for i in range(n_epochs): - train_kl() - for k in xrange(len(x_obs)/100): - cost = train(x_obs[k*100:(k+1)*100], s_obs[k*100:(k+1)*100]) - print(cost) - print(graph) - print(mu.get_value()) - print(sig.get_value()) |
