コード例 #1
0
ファイル: profiling.py プロジェクト: HIPS/autograd
def dot_equivalent():
    # MNIST-scale convolution operation
    import autograd.scipy.signal
    dat = npr.randn(256, 3, 24, 5, 24, 5)
    kernel = npr.randn(3, 5, 5)
    with tictoc():
        np.tensordot(dat, kernel, axes=[(1, 3, 5), (0, 1, 2)])
コード例 #2
0
ファイル: _autograd.py プロジェクト: pymanopt/pymanopt
 def vector_dot_grad(*args, **kwargs):
     args, vector = args[:-1], args[-1]
     try:
         return np.tensordot(fun_grad(*args, **kwargs), vector,
                             axes=vector.ndim)
     except AttributeError:
         # Assume we are on the product manifold.
         return np.sum([np.tensordot(fun_grad(*args, **kwargs)[k],
                                     vector[k], axes=vector[k].ndim)
                        for k in range(len(vector))])
コード例 #3
0
    def setUp(self):
        self.m = m = 100
        self.n = n = 50
        self.man = Sphere(m, n)

        # For automatic testing of ehess2rhess
        self.proj = lambda x, u: u - npa.tensordot(x, u, np.ndim(u)) * x
コード例 #4
0
ファイル: gmm.py プロジェクト: mattjj/svae
def local_meanfield(global_natparam, node_potentials):
    dirichlet_natparam, niw_natparams = global_natparam
    node_potentials = gaussian.pack_dense(*node_potentials)

    # compute expected global parameters using current global factors
    label_global = dirichlet.expectedstats(dirichlet_natparam)
    gaussian_globals = niw.expectedstats(niw_natparams)

    # compute mean field fixed point using unboxed node_potentials
    label_stats = meanfield_fixed_point(label_global, gaussian_globals, getval(node_potentials))

    # compute values that depend directly on boxed node_potentials at optimum
    gaussian_natparam, gaussian_stats, gaussian_kl = \
        gaussian_meanfield(gaussian_globals, node_potentials, label_stats)
    label_natparam, label_stats, label_kl = \
        label_meanfield(label_global, gaussian_globals, gaussian_stats)

    # collect sufficient statistics for gmm prior (sum across conditional iid)
    dirichlet_stats = np.sum(label_stats, 0)
    niw_stats = np.tensordot(label_stats, gaussian_stats, [0, 0])

    local_stats = label_stats, gaussian_stats
    prior_stats = dirichlet_stats, niw_stats
    natparam = label_natparam, gaussian_natparam
    kl = label_kl + gaussian_kl

    return local_stats, prior_stats, natparam, kl
コード例 #5
0
ファイル: gmm.py プロジェクト: mattjj/svae
def meanfield_fixed_point(label_global, gaussian_globals, node_potentials, tol=1e-3, max_iter=100):
    kl = np.inf
    label_stats = initialize_meanfield(label_global, node_potentials)
    for i in xrange(max_iter):
        gaussian_natparam, gaussian_stats, gaussian_kl = \
            gaussian_meanfield(gaussian_globals, node_potentials, label_stats)
        label_natparam, label_stats, label_kl = \
            label_meanfield(label_global, gaussian_globals, gaussian_stats)

        # recompute gaussian_kl linear term with new label_stats b/c labels were updated
        gaussian_global_potentials = np.tensordot(label_stats, gaussian_globals, [1, 0])
        linear_difference = gaussian_natparam - gaussian_global_potentials - node_potentials
        gaussian_kl = gaussian_kl + np.tensordot(linear_difference, gaussian_stats, 3)

        kl, prev_kl = label_kl + gaussian_kl, kl
        if abs(kl - prev_kl) < tol:
            break
    else:
        print 'iteration limit reached'

    return label_stats
コード例 #6
0
ファイル: gmm_svae.py プロジェクト: andymiller/svae
def label_meanfield(label_global, gaussian_globals, gaussian_stats):
    partial_contract = lambda a, b: \
        sum(np.tensordot(x, y, axes=np.ndim(y)) for x, y, in zip(a, b))

    gaussian_local_natparams = map(niw.expectedstats, gaussian_globals)
    node_params = np.array([
        partial_contract(gaussian_stats, natparam) for natparam in gaussian_local_natparams]).T

    local_natparam = dirichlet.expectedstats(label_global) + node_params
    stats = normalize(np.exp(local_natparam  - logsumexp(local_natparam, axis=1, keepdims=True)))
    vlb = np.sum(logsumexp(local_natparam, axis=1)) - contract(stats, node_params)

    return local_natparam, stats, vlb
コード例 #7
0
ファイル: _autograd.py プロジェクト: gitter-badger/pymanopt
 def hess(x, g): return np.tensordot(ad.hessian(objective)(x),
                                     g, axes=x.ndim)
 return hess
コード例 #8
0
ファイル: gmm_svae.py プロジェクト: andymiller/svae
 def get_local_natparam(gaussian_globals, node_potentials, label_stats):
     local_natparams = [np.tensordot(label_stats, param, axes=1)
                        for param in zip(*map(niw.expectedstats, gaussian_globals))]
     return add(local_natparams, make_full_potentials(node_potentials))
コード例 #9
0
ファイル: gmm_svae.py プロジェクト: andymiller/svae
def get_global_stats(label_stats, gaussian_stats):
    contract = lambda w: lambda p: np.tensordot(w, p, axes=1)
    global_label_stats = np.sum(label_stats, axis=0)
    global_gaussian_stats = tuple(map(contract(w), gaussian_stats) for w in label_stats.T)
    return global_label_stats, global_gaussian_stats
コード例 #10
0
 def vector_dot_fun(*args, **kwargs):
     args, vector = args[:-1], args[-1]
     return np.tensordot(vector, fun(*args, **kwargs), axes=np.ndim(vector))
コード例 #11
0
 def vector_dot_grad(*args, **kwargs):
     args, vector = args[:-1], args[-1]
     return np.tensordot(fun_grad(*args, **kwargs), vector, np.ndim(vector))
コード例 #12
0
ファイル: test_wrappers.py プロジェクト: AugustLONG/autograd
def test_tensor_jacobian_product():
    fun = lambda a: np.roll(np.sin(a), 1)
    a = npr.randn(5, 4, 3)
    V = npr.randn(5, 4)
    J = jacobian(fun)(a)
    check_equivalent(np.tensordot(V, J, axes=np.ndim(V)), vector_jacobian_product(fun)(a, V))
コード例 #13
0
ファイル: test_numpy.py プロジェクト: xindaya/autograd
 def fun(x):
     return np.tensordot(x * np.ones((2,2)),
                         x * np.ones((2,2)), 2)
コード例 #14
0
ファイル: test_wrappers.py プロジェクト: AugustLONG/autograd
def test_matrix_jacobian_product():
    fun = lambda a: np.roll(np.sin(a), 1)
    a = npr.randn(5, 4)
    V = npr.randn(5, 4)
    J = jacobian(fun)(a)
    check_equivalent(np.tensordot(V, J), vector_jacobian_product(fun)(a, V))
コード例 #15
0
ファイル: gmm.py プロジェクト: mattjj/svae
def label_meanfield(label_global, gaussian_globals, gaussian_stats):
    node_potentials = np.tensordot(gaussian_stats, gaussian_globals, [[1,2], [1,2]])
    natparam = node_potentials + label_global
    stats = categorical.expectedstats(natparam)
    kl = np.tensordot(stats, node_potentials) - categorical.logZ(natparam)
    return natparam, stats, kl
コード例 #16
0
ファイル: gmm.py プロジェクト: mattjj/svae
def gaussian_meanfield(gaussian_globals, node_potentials, label_stats):
    global_potentials = np.tensordot(label_stats, gaussian_globals, [1, 0])
    natparam = node_potentials + global_potentials
    stats = gaussian.expectedstats(natparam)
    kl = np.tensordot(node_potentials, stats, 3) - gaussian.logZ(natparam)
    return natparam, stats, kl
コード例 #17
0
ファイル: test_wrappers.py プロジェクト: AugustLONG/autograd
def test_hessian_tensor_product():
    fun = lambda a: np.sum(np.sin(a))
    a = npr.randn(5, 4, 3)
    V = npr.randn(5, 4, 3)
    H = hessian(fun)(a)
    check_equivalent(np.tensordot(H, V, axes=np.ndim(V)), hessian_vector_product(fun)(a, V))
コード例 #18
0
ファイル: test_wrappers.py プロジェクト: AugustLONG/autograd
def test_hessian_matrix_product():
    fun = lambda a: np.sum(np.sin(a))
    a = npr.randn(5, 4)
    V = npr.randn(5, 4)
    H = hessian(fun)(a)
    check_equivalent(np.tensordot(H, V), hessian_vector_product(fun)(a, V))