annotate stat_ops.py @ 330:20e08c52c98c

merge
author Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
date Mon, 16 Jun 2008 16:11:36 -0400
parents fe57b96f33d4
children
rev   line source
326
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
1
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
2 import theano
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
3 from theano import gof
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
4 from theano import tensor
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
5 import numpy
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
6
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
7
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
8 class ExampleWiseMean(gof.Op):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
9
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
10 def __init__(self):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
11 self.destroy_map = {0: [1, 2]}
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
12
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
13 def make_node(self, x):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
14 return gof.Apply(self,
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
15 [x, tensor.value(float('nan')), tensor.value(0)],
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
16 [tensor.Tensor(dtype = 'float64',
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
17 broadcastable = x.type.broadcastable)()])
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
18
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
19 def perform(self, node, (x, sum, n), (out,)):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
20 if numpy.isnan(sum).any():
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
21 sum.resize(x.shape, refcheck=0)
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
22 sum[:] = x
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
23 else:
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
24 sum += x
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
25 n += 1
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
26 out[0] = sum / n
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
27
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
28 def c_code(self, name, node, (x, sum, n), (out, ), sub):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
29 return """
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
30 PyObject* multi;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
31 int nelems;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
32 if (isnan(((double*)(%(sum)s->data))[0])) {
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
33 PyArray_Dims dims;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
34 dims.len = %(x)s->nd;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
35 dims.ptr = %(x)s->dimensions;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
36 PyArray_Resize(%(sum)s, &dims, 0, PyArray_CORDER);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
37 multi = PyArray_MultiIterNew(2, %(sum)s, %(x)s);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
38 nelems = PyArray_SIZE(%(sum)s);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
39 while (nelems--) {
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
40 // Copy %(x)s in %(sum)s
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
41 *(double*)PyArray_MultiIter_DATA(multi, 0) = *(double*)PyArray_MultiIter_DATA(multi, 1);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
42 PyArray_MultiIter_NEXT(multi);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
43 }
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
44 }
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
45 else {
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
46 // Add some error checking on the size of x
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
47 multi = PyArray_MultiIterNew(2, %(sum)s, %(x)s);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
48 nelems = PyArray_SIZE(%(sum)s);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
49 while (nelems--) {
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
50 // Add %(x)s to %(sum)s
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
51 *(double*)PyArray_MultiIter_DATA(multi, 0) += *(double*)PyArray_MultiIter_DATA(multi, 1);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
52 PyArray_MultiIter_NEXT(multi);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
53 }
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
54 }
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
55 ((npy_int64*)(%(n)s->data))[0]++;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
56 int n = ((npy_int64*)(%(n)s->data))[0];
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
57 if (%(out)s == NULL) {
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
58 %(out)s = (PyArrayObject*)PyArray_EMPTY(%(sum)s->nd, %(sum)s->dimensions, NPY_FLOAT64, 0);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
59 }
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
60 multi = PyArray_MultiIterNew(2, %(sum)s, %(out)s);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
61 nelems = PyArray_SIZE(%(sum)s);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
62 while (nelems--) {
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
63 // %(out)s <- %(sum)s / %(n)s
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
64 *(double*)PyArray_MultiIter_DATA(multi, 1) = *(double*)PyArray_MultiIter_DATA(multi, 0) / n;
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
65 PyArray_MultiIter_NEXT(multi);
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
66 }
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
67 """ % dict(locals(), **sub)
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
68
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
69
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
70
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
71 if __name__ == '__main__':
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
72
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
73 vectors = numpy.random.RandomState(666).rand(10, 2)
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
74
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
75 x = tensor.dvector()
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
76 e = ExampleWiseMean()(x)
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
77
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
78 # f = theano.function([x], [e], linker = 'py')
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
79
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
80 # for i, v in enumerate(vectors):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
81 # print v, "->", f(v), numpy.mean(vectors[:i+1], axis=0)
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
82
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
83 # print
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
84
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
85 f = theano.function([x], [e], linker = 'c|py')
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
86
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
87 for i, v in enumerate(vectors):
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
88 print v, "->", f(v), numpy.mean(vectors[:i+1], axis=0)
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
89
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
90
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
91
fe57b96f33d4 made ExampleWiseMean Op
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
92