SampleMultivariateGaussian   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A grad() 0 2 1
A perform() 0 4 1
A make_node() 0 6 3
A R_op() 0 4 2
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
import numpy as np
4
from theano import gof, tensor
5
import theano
6
import theano.tensor as T
7
8
9
class SampleMultivariateGaussian(gof.Op):
10
    __props__ = ()
11
12
    def make_node(self, x, cov):
13
        if x.type.ndim != 1:
14
            raise TypeError('x must be a 1-d vector')
15
        if cov.type.ndim != 2:
16
            raise TypeError('cov must be a 2-d matrix')
17
        return gof.Apply(self, [x, cov], [T.vector(dtype="float32")])
18
19
    def perform(self, node, inp, out):
20
        x, cov = inp
21
        z, = out
22
        z[0] = np.random.multivariate_normal(x, cov, 1)[0].astype("float32")
23
24
    def grad(self, inputs, outputs):
25
        return [outputs[0], T.zeros_like(inputs[1])]
26
27
    def R_op(self, inputs, eval_points):
28
        if eval_points[0] is None:
29
            return eval_points
30
        return self.grad(inputs, eval_points)