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
|
import utils
import theano
from theano import tensor as tsr
import blocks
from blocks import algorithms, main_loop
import blocks.extensions as be
import blocks.extensions.monitoring as bm
import picklable_itertools
import numpy as np
import fuel
import fuel.datasets
import collections
class LearnedDataset(fuel.datasets.Dataset):
"""
Dynamically-created dataset (for active learning)
-compatible with ConstantScheme with request corresponding to a
batch_size
"""
provides_sources = ('x', 's')
def __init__(self, node_p, graph, **kwargs):
super(LearnedDataset, self).__init__(**kwargs)
self.node_p = node_p
self.graph = graph
self.source = lambda graph: utils.random_source(graph, self.node_p)
def get_data(self, state=None, request=None):
return utils.simulate_cascades(request, self.graph, self.source)
class ActiveLearning(blocks.extensions.SimpleExtension):
"""
Extension which updates the node_p array passed to the get_data method of
LearnedDataset
"""
def __init__(self, dataset, **kwargs):
super(ActiveLearning, self).__init__(**kwargs)
self.dataset = dataset
def do(self, which_callback, *args):
out_degree = np.sum(self.dataset.graph, axis=1)
self.dataset.node_p = out_degree / np.sum(out_degree)
print(self.dataset.node_p)
class ShuffledBatchesScheme(fuel.schemes.ShuffledScheme):
"""Iteration scheme over finite dataset:
-shuffles batches but not within batch
-arguments: dataset_size (int) ; batch_size (int)"""
def get_request_iterator(self):
indices = list(self.indices) # self.indices = xrange(dataset_size)
start = np.random.randint(self.batch_size)
batches = list(map(
list,
picklable_itertools.extras.partition_all(self.batch_size,
indices[start:])
))
if indices[:start]:
batches.append(indices[:start])
batches = np.asarray(batches)
return iter(batches[np.random.permutation(len(batches))])
def create_mle_model(graph):
"""return cascade likelihood theano computation graph"""
n_nodes = len(graph)
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)).astype(theano.config.floatX),
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
lkl_mle.name = 'cost'
return x, s, params, lkl_mle
def rmse_error(graph, params):
n_nodes = graph.shape[0]
diff = (graph - params) ** 2
subarray = tsr.arange(n_nodes)
tsr.set_subtensor(diff[subarray, subarray], 0)
rmse = tsr.sum(diff) / (n_nodes ** 2)
rmse.name = 'rmse'
return rmse
def relative_error(graph, params):
n_nodes = graph.shape[0]
diff = abs(g_shared - params)
subarray = tsr.arange(n_nodes)
tsr.set_subtensor(diff[subarray, subarray], 0)
error = tsr.sum(tsr.switch(tsr.eq(graph, 0.), 0., diff / graph)) / n_nodes
error.name = 'rel_error'
return error
def create_fixed_data_stream(n_obs, graph, batch_size, shuffle=True):
"""
creates a datastream for a fixed (not learned) dataset:
-shuffle (bool): shuffle minibatches but not within minibatch, else
sequential (non-shuffled) batches are used
"""
x_obs, s_obs = utils.simulate_cascades(n_obs, graph)
data_set = fuel.datasets.base.IndexableDataset(collections.OrderedDict(
[('x', x_obs), ('s', s_obs)]
))
if shuffle:
scheme = ShuffledBatchesScheme(n_obs, batch_size=batch_size)
else:
scheme = fuel.schemes.SequentialScheme(n_obs, batch_size=batch_size)
return fuel.streams.DataStream(dataset=data_set, iteration_scheme=scheme)
def create_learned_data_stream(graph, batch_size):
node_p = np.ones(len(graph)) / len(graph)
data_set = LearnedDataset(node_p, graph)
scheme = fuel.schemes.ConstantScheme(batch_size)
return fuel.streams.DataStream(dataset=data_set, iteration_scheme=scheme)
if __name__ == "__main__":
batch_size = 100
n_obs = 1000
graph = utils.create_wheel(10)
print('GRAPH:\n', graph, '\n-------------\n')
g_shared = theano.shared(value=graph, name='graph')
x, s, params, cost = create_mle_model(graph)
rmse = rmse_error(g_shared, params)
error = relative_error(g_shared, params)
alg = algorithms.GradientDescent(
cost=-cost, parameters=[params], step_rule=blocks.algorithms.AdaDelta()
)
# data_stream = create_learned_data_stream(graph, batch_size)
data_stream = create_fixed_data_stream(n_obs, graph, batch_size)
loop = main_loop.MainLoop(
alg, data_stream,
extensions=[
be.FinishAfter(after_n_batches=10**3),
bm.TrainingDataMonitoring([cost, params,
rmse, g_shared], after_batch=True),
be.Printing(every_n_batches=10),
#ActiveLearning(data_stream.dataset),
]
)
loop.run()
|