示例#1
0
    def test_alloc_diag(self):
        rng = numpy.random.RandomState(utt.fetch_seed())
        x = theano.tensor.vector()
        g = alloc_diag(x)
        f = theano.function([x], g)

        # test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1
        for shp in [5, 0, 1]:
            m = rng.rand(shp).astype(self.floatX)
            v = numpy.diag(m)
            r = f(m)
            # The right matrix is created
            assert (r == v).all()

        # Test we accept only vectors
        xx = theano.tensor.matrix()
        ok = False
        try:
            alloc_diag(xx)
        except TypeError:
            ok = True
        assert ok

        # Test infer_shape
        f = theano.function([x], g.shape)
        topo = f.maker.fgraph.toposort()
        if config.mode != 'FAST_COMPILE':
            assert sum([node.op.__class__ == AllocDiag for node in topo]) == 0
        for shp in [5, 0, 1]:
            m = rng.rand(shp).astype(self.floatX)
            assert (f(m) == m.shape).all()
示例#2
0
def test_alloc_diag():
    rng = numpy.random.RandomState(utt.fetch_seed())
    x = theano.tensor.vector()
    g = alloc_diag(x)
    f = theano.function([x], g)

    # test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1
    for shp in [5, 0, 1]:
        m = rng.rand(shp).astype(config.floatX)
        v = numpy.diag(m)
        r = f(m)
        # The right diagonal is extracted
        assert (r == v).all()

    # Test we accept only vectors
    xx = theano.tensor.matrix()
    ok = False
    try:
        alloc_diag(xx)
    except TypeError:
        ok = True
    assert ok

    # Test infer_shape
    f = theano.function([x], g.shape)
    topo = f.maker.fgraph.toposort()
    if config.mode != 'FAST_COMPILE':
        assert sum([node.op.__class__ == AllocDiag for node in topo]) == 0
    for shp in [5, 0, 1]:
        m = rng.rand(shp).astype(config.floatX)
        assert (f(m) == m.shape).all()
示例#3
0
    def test_W_jump(self):

        " tests that W is where I think it should be "

        stats = self.stats

        cov_hs = stats.d['second_hs']
        assert cov_hs.dtype == config.floatX
        #mean_hsv[i,j] = E_D,Q h_i s_i v_j
        mean_hsv = stats.d['mean_hsv']

        regularized = cov_hs + alloc_diag(
            T.ones_like(self.model.mu) * self.model.W_eps)
        assert regularized.dtype == config.floatX

        inv = matrix_inverse(regularized)
        assert inv.dtype == config.floatX

        new_W = T.dot(mean_hsv.T, inv)

        f = function([], new_W)

        Wv = f()
        aWv = self.model.W.get_value()

        diffs = Wv - aWv
        max_diff = np.abs(diffs).max()

        if max_diff > self.tol:
            raise Exception("W deviates from its correct value by at most " +
                            str(max_diff))
示例#4
0
    def test_W_jump(self):

        " tests that W is where I think it should be "

        stats = self.stats

        cov_hs = stats.d['second_hs']
        assert cov_hs.dtype == config.floatX
        #mean_hsv[i,j] = E_D,Q h_i s_i v_j
        mean_hsv = stats.d['mean_hsv']

        regularized = cov_hs + alloc_diag(T.ones_like(self.model.mu) * self.model.W_eps)
        assert regularized.dtype == config.floatX


        inv = matrix_inverse(regularized)
        assert inv.dtype == config.floatX

        new_W = T.dot(mean_hsv.T, inv)

        f = function([], new_W)

        Wv = f()
        aWv = self.model.W.get_value()

        diffs = Wv - aWv
        max_diff = np.abs(diffs).max()

        if max_diff > self.tol:
            raise Exception("W deviates from its correct value by at most "+str(max_diff))
示例#5
0
    def test_grad_W(self):
        """tests that the gradient of the log probability with respect to W
        matches my analytical derivation """

        #self.model.set_param_values(self.new_params)

        g = T.grad(self.prob,
                   self.model.W,
                   consider_constant=self.mf_obs.values())

        B = self.model.B
        W = self.model.W
        mean_hsv = self.stats.d['mean_hsv']

        mean_sq_hs = self.stats.d['mean_sq_hs']

        mean_HS = self.mf_obs['H_hat'] * self.mf_obs['S_hat']

        m = mean_HS.shape[0]

        outer_prod = T.dot(mean_HS.T, mean_HS)
        outer_prod.name = 'outer_prod<from_observations>'
        outer = outer_prod / m
        mask = T.identity_like(outer)
        second_hs = (1. - mask) * outer + alloc_diag(mean_sq_hs)

        term1 = (B * mean_hsv).T
        term2 = -B.dimshuffle(0, 'x') * T.dot(W, second_hs)

        analytical = term1 + term2

        f = function([], (g, analytical))

        gv, av = f()

        assert gv.shape == av.shape

        max_diff = np.abs(gv - av).max()

        if max_diff > self.tol:
            print "gv"
            print gv
            print "av"
            print av
            raise Exception(
                "analytical gradient on W deviates from theano gradient on W by up to "
                + str(max_diff))
示例#6
0
    def test_grad_W(self):
        """tests that the gradient of the log probability with respect to W
        matches my analytical derivation """

        #self.model.set_param_values(self.new_params)

        g = T.grad(self.prob, self.model.W, consider_constant = self.mf_obs.values())

        B = self.model.B
        W = self.model.W
        mean_hsv = self.stats.d['mean_hsv']

        mean_sq_hs = self.stats.d['mean_sq_hs']

        mean_HS = self.mf_obs['H_hat'] * self.mf_obs['S_hat']

        m = mean_HS.shape[0]

        outer_prod = T.dot(mean_HS.T,mean_HS)
        outer_prod.name = 'outer_prod<from_observations>'
        outer = outer_prod/m
        mask = T.identity_like(outer)
        second_hs = (1.-mask) * outer + alloc_diag(mean_sq_hs)


        term1 = (B * mean_hsv).T
        term2 = - B.dimshuffle(0,'x') * T.dot(W, second_hs)

        analytical = term1 + term2

        f = function([],(g,analytical))

        gv, av = f()

        assert gv.shape == av.shape

        max_diff = np.abs(gv-av).max()

        if max_diff > self.tol:
            print "gv"
            print gv
            print "av"
            print av
            raise Exception("analytical gradient on W deviates from theano gradient on W by up to "+str(max_diff))