當前位置: 首頁>>代碼示例>>Python>>正文


Python tensor.jacobian方法代碼示例

本文整理匯總了Python中theano.tensor.jacobian方法的典型用法代碼示例。如果您正苦於以下問題:Python tensor.jacobian方法的具體用法?Python tensor.jacobian怎麽用?Python tensor.jacobian使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在theano.tensor的用法示例。


在下文中一共展示了tensor.jacobian方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_dot_not_output

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def test_dot_not_output(self):
        """
        Test the case where the vector input to the dot is not already an
        output of the inner function.
        """

        v = T.vector()
        m = T.matrix()
        output = T.dot(v, m)

        # Compile the function twice, once with the optimization and once
        # without
        opt_mode = mode.including("scan")
        f_opt = theano.function([v, m], T.jacobian(output, v), mode=opt_mode)

        no_opt_mode = mode.excluding("scanOp_pushout_output")
        f_no_opt = theano.function([v, m], T.jacobian(output, v), mode=no_opt_mode)

        # Ensure that the optimization was performed correctly in f_opt
        # The inner function of scan should have only one output and it should
        # not be the result of a Dot
        scan_node = [node for node in f_opt.maker.fgraph.toposort()
                     if isinstance(node.op, Scan)][0]
        assert len(scan_node.op.outputs) == 1
        assert not isinstance(scan_node.op.outputs[0], T.Dot)

        # Ensure that the function compiled with the optimization produces
        # the same results as the function compiled without
        v_value = numpy.random.random((4)).astype(config.floatX)
        m_value = numpy.random.random((4, 5)).astype(config.floatX)

        output_opt = f_opt(v_value, m_value)
        output_no_opt = f_no_opt(v_value, m_value)

        utt.assert_allclose(output_opt, output_no_opt) 
開發者ID:muhanzhang,項目名稱:D-VAE,代碼行數:37,代碼來源:test_scan_opt.py

示例2: test001_jacobian_vector

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def test001_jacobian_vector():
    x = tensor.vector()
    y = x * 2
    rng = numpy.random.RandomState(seed=utt.fetch_seed())

    # test when the jacobian is called with a tensor as wrt
    Jx = tensor.jacobian(y, x)
    f = theano.function([x], Jx)
    vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
    assert numpy.allclose(f(vx), numpy.eye(10) * 2)

    # test when the jacobian is called with a tuple as wrt
    Jx = tensor.jacobian(y, (x,))
    assert isinstance(Jx, tuple)
    f = theano.function([x], Jx[0])
    vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
    assert numpy.allclose(f(vx), numpy.eye(10) * 2)

    # test when the jacobian is called with a list as wrt
    Jx = tensor.jacobian(y, [x])
    assert isinstance(Jx, list)
    f = theano.function([x], Jx[0])
    vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
    assert numpy.allclose(f(vx), numpy.eye(10) * 2)

    # test when the jacobian is called with a list of two elements
    z = tensor.vector()
    y = x * z
    Js = tensor.jacobian(y, [x, z])
    f = theano.function([x, z], Js)
    vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
    vz = rng.uniform(size=(10,)).astype(theano.config.floatX)
    vJs = f(vx, vz)
    evx = numpy.zeros((10, 10))
    evz = numpy.zeros((10, 10))
    numpy.fill_diagonal(evx, vx)
    numpy.fill_diagonal(evz, vz)
    assert numpy.allclose(vJs[0], evz)
    assert numpy.allclose(vJs[1], evx) 
開發者ID:muhanzhang,項目名稱:D-VAE,代碼行數:41,代碼來源:test_2nd_order_grads.py

示例3: test003_jacobian_scalar

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def test003_jacobian_scalar():
    x = tensor.scalar()
    y = x * 2
    rng = numpy.random.RandomState(seed=utt.fetch_seed())

    # test when the jacobian is called with a tensor as wrt
    Jx = tensor.jacobian(y, x)
    f = theano.function([x], Jx)
    vx = numpy.cast[theano.config.floatX](rng.uniform())
    assert numpy.allclose(f(vx), 2)

    # test when the jacobian is called with a tuple as wrt
    Jx = tensor.jacobian(y, (x,))
    assert isinstance(Jx, tuple)
    f = theano.function([x], Jx[0])
    vx = numpy.cast[theano.config.floatX](rng.uniform())
    assert numpy.allclose(f(vx), 2)

    # test when the jacobian is called with a list as wrt
    Jx = tensor.jacobian(y, [x])
    assert isinstance(Jx, list)
    f = theano.function([x], Jx[0])
    vx = numpy.cast[theano.config.floatX](rng.uniform())
    assert numpy.allclose(f(vx), 2)

    # test when the jacobian is called with a list of two elements
    z = tensor.scalar()
    y = x * z
    Jx = tensor.jacobian(y, [x, z])
    f = theano.function([x, z], Jx)
    vx = numpy.cast[theano.config.floatX](rng.uniform())
    vz = numpy.cast[theano.config.floatX](rng.uniform())
    vJx = f(vx, vz)

    assert numpy.allclose(vJx[0], vz)
    assert numpy.allclose(vJx[1], vx) 
開發者ID:muhanzhang,項目名稱:D-VAE,代碼行數:38,代碼來源:test_2nd_order_grads.py

示例4: _get_updates

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def _get_updates(self):
        n = self.params['batch_size']
        N = self.params['train_size']
        prec_lik = self.params['prec_lik']
        prec_prior = self.params['prec_prior']
        gc_norm = self.params['gc_norm']
        alpha = self.params['alpha']
        mu = self.params['mu']
        use_gamma = self.params['use_gamma']

        # compute log-likelihood
        error = self.model_outputs - self.true_outputs
        logliks = log_normal(error, prec_lik)
        sumloglik = logliks.sum()
        meanloglik = sumloglik / n

        # compute gradients
        grads = tensor.grad(cost = meanloglik, wrt = self.weights)

        # update preconditioning matrix
        V_t_next = [alpha * v + (1 - alpha) * g * g for g, v in zip(grads, self.V_t)]
        G_t = [1. / (mu + tensor.sqrt(v)) for v in V_t_next]

        logprior = log_prior_normal(self.weights, prec_prior)
        grads_prior = tensor.grad(cost = logprior, wrt = self.weights)

        updates = []
        [updates.append((v, v_n)) for v, v_n in zip(self.V_t, V_t_next)]

        for p, g, gp, gt in zip(self.weights, grads, grads_prior, G_t):
            # inject noise
            noise = tensor.sqrt(self.lr * gt) * trng.normal(p.shape)
            if use_gamma:
                # compute gamma
                gamma = nlinalg.extract_diag(tensor.jacobian(gt.flatten(), p).flatten(ndim=2))
                gamma = gamma.reshape(p.shape)
                updates.append((p, p + 0.5 * self.lr * ((gt * (gp + N * g)) + gamma) + noise))
            else:
                updates.append((p, p + 0.5 * self.lr * (gt * (gp + N * g)) + noise))

        return updates, sumloglik 
開發者ID:akshaykgupta,項目名稱:SG_MCMC,代碼行數:43,代碼來源:sgmcmc.py

示例5: nigp_updates

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def nigp_updates(self):
        idims = self.D
        msg = 'Compiling derivative of mean function at training inputs'
        utils.print_with_stamp(msg, self.name)

        # we need to evaluate the derivative of the mean function at the
        # training inputs
        def dM2_f_i(mx, beta, hyp, X):
            hyps = (hyp[:idims+1], hyp[idims+1])
            kernel_func = partial(cov.Sum, hyps, self.covs)
            k = kernel_func(mx[None, :], X).flatten()
            mean = k.dot(beta)
            dmean = tt.jacobian(mean.flatten(), mx)
            return tt.square(dmean.flatten())

        def dM2_f(beta, hyp, X):
            # iterate over training inputs
            dM2_o, updts = theano.scan(fn=dM2_f_i, sequences=[X],
                                       non_sequences=[beta, hyp, X],
                                       allow_gc=False)
            return dM2_o

        # iterate over output dimensions
        dM2, updts = theano.scan(fn=dM2_f, sequences=[self.beta, self.hyp],
                                 non_sequences=[self.X], allow_gc=False)

        # update the nigp parameter using the derivative of the mean function
        nigp = ((dM2[:, :, :, None]*self.X_cov[None]).sum(2)*dM2).sum(-1)
        nigp_updts = updts + (self.nigp, nigp)

        return nigp_updts 
開發者ID:mcgillmrl,項目名稱:kusanagi,代碼行數:33,代碼來源:GP.py

示例6: compute_output

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def compute_output(self, network, h_vw, x_vw):
        batch_axis = network.find_hyperparameter(["batch_axis"])
        if batch_axis is None:
            # NOTE: this code path is not tested!
            jacobian = T.jacobian(h_vw.variable.ravel(), x_vw.variable)
            res = (jacobian ** 2).mean()
            res_shape = ()
        else:
            batch_size = h_vw.symbolic_shape()[batch_axis]
            # sum across batch to avoid disconnected input error
            # ravel to be a vector
            h_var = h_vw.variable.sum(axis=batch_axis).ravel()
            x_var = x_vw.variable
            # shape of result = h_var.shape + x_var.shape
            jacobian = T.jacobian(h_var, x_var)
            # put batch axis as first dimension
            # adding 1 to batch axis, because len(h_var.shape) == 1
            swapped_jacobian = jacobian.swapaxes(0, batch_axis + 1)
            # convert to a matrix and mean over elements in a batch
            reshaped_jacobian = swapped_jacobian.reshape((batch_size, -1))
            res = (reshaped_jacobian ** 2).mean(axis=1)
            res_shape = (h_vw.shape[batch_axis],)
        network.create_vw(
            "default",
            variable=res,
            shape=res_shape,
            tags={"output"},
        ) 
開發者ID:SBU-BMI,項目名稱:u24_lymphocyte,代碼行數:30,代碼來源:contraction_penalty.py

示例7: Lop

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def Lop(f, wrt, eval_points, consider_constant=None,
        disconnected_inputs='raise'):
    """
    Computes the L operation on `f` wrt to `wrt` evaluated at points given
    in `eval_points`. Mathematically this stands for the jacobian of `f` wrt
    to `wrt` left muliplied by the eval points.

    :type f: Variable or list of Variables
        `f` stands for the output of the computational graph to which you
        want to apply the L operator
    :type wrt: Variable or list of `Variables`s
        variables for which you compute the L operator of the expression
        described by `f`
    :type eval_points: Variable or list of Variables
                        evalutation points for each of the variables in `f`

    :rtype: Variable or list/tuple of Variables depending on type of f
    :return: symbolic expression such that
        L_op[i] = sum_i ( d f[i] / d wrt[j]) eval_point[i]
        where the indices in that expression are magic multidimensional
        indices that specify both the position within a list and all
        coordinates of the tensor element in the last
        If `f` is a list/tuple, then return a list/tuple with the results.
    """
    if type(eval_points) not in (list, tuple):
        eval_points = [eval_points]

    using_list = isinstance(wrt, list)
    using_tuple = isinstance(wrt, tuple)

    if not isinstance(f, (list, tuple)):
        f = [f]

    # make copies of f and grads so we don't modify the client's copy
    f = list(f)
    grads = list(eval_points)

    if not isinstance(wrt, (list, tuple)):
        wrt = [wrt]

    assert len(f) == len(grads)
    known = dict(izip(f, grads))

    ret = grad(cost=None, known_grads=known,
               consider_constant=consider_constant, wrt=wrt,
               disconnected_inputs=disconnected_inputs)

    return format_as(using_list, using_tuple, ret)


#########################
# Gradient
######################### 
開發者ID:muhanzhang,項目名稱:D-VAE,代碼行數:55,代碼來源:gradient.py

示例8: test002_jacobian_matrix

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def test002_jacobian_matrix():
    x = tensor.matrix()
    y = 2 * x.sum(axis=0)
    rng = numpy.random.RandomState(seed=utt.fetch_seed())
    ev = numpy.zeros((10, 10, 10))
    for dx in xrange(10):
        ev[dx, :, dx] = 2.

    # test when the jacobian is called with a tensor as wrt
    Jx = tensor.jacobian(y, x)
    f = theano.function([x], Jx)
    vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
    assert numpy.allclose(f(vx), ev)

    # test when the jacobian is called with a tuple as wrt
    Jx = tensor.jacobian(y, (x,))
    assert isinstance(Jx, tuple)
    f = theano.function([x], Jx[0])
    vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
    assert numpy.allclose(f(vx), ev)

    # test when the jacobian is called with a list as wrt
    Jx = tensor.jacobian(y, [x])
    assert isinstance(Jx, list)
    f = theano.function([x], Jx[0])
    vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
    assert numpy.allclose(f(vx), ev)

    # test when the jacobian is called with a list of two elements
    z = tensor.matrix()
    y = (x * z).sum(axis=1)
    Js = tensor.jacobian(y, [x, z])
    f = theano.function([x, z], Js)
    vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
    vz = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
    vJs = f(vx, vz)
    evx = numpy.zeros((10, 10, 10))
    evz = numpy.zeros((10, 10, 10))
    for dx in xrange(10):
        evx[dx, dx, :] = vx[dx, :]
        evz[dx, dx, :] = vz[dx, :]
    assert numpy.allclose(vJs[0], evz)
    assert numpy.allclose(vJs[1], evx) 
開發者ID:muhanzhang,項目名稱:D-VAE,代碼行數:45,代碼來源:test_2nd_order_grads.py

示例9: test_vectors

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import jacobian [as 別名]
def test_vectors(self):
        
        try:
            import theano.tensor as T
            from theano import function            
        except:
            return
            
        for MT in [False, True]:

            # Set up variables and function
            vals = [np.random.randn(20) for i in range(5)]
            f = lambda a, b, c, d, e : a + (b * c) - d ** e

            # Set up our objects
            Cs = [ch.Ch(v) for v in vals]
            C_result = f(*Cs)
            C_result.MT = MT

            # Set up Theano equivalents
            Ts = T.dvectors('T1', 'T2', 'T3', 'T4', 'T5')
            TF = f(*Ts)
            T_result = function(Ts, TF)        

            if False:
                import theano.gradient
                which = 1
                theano_sse = (TF**2.).sum()
                theano_grad = theano.gradient.grad(theano_sse, Ts[which])
                theano_fn = function(Ts, theano_grad)
                print(theano_fn(*vals))
                C_result_grad = ch.SumOfSquares(C_result).dr_wrt(Cs[which])
                print(C_result_grad)
                
                # if True:
                #     aaa = np.linalg.solve(C_result_grad.T.dot(C_result_grad), C_result_grad.dot(np.zeros(C_result_grad.shape[1])))
                #     theano_hes = theano.R_obbb = theano.R_op()
                
                import pdb; pdb.set_trace()

            # Make sure values and derivatives are equal
            np.testing.assert_array_equal(C_result.r, T_result(*vals))
            for k in range(len(vals)):
                theano_derivative = function(Ts, T.jacobian(TF, Ts[k]))(*vals)
                our_derivative = np.array(C_result.dr_wrt(Cs[k]).todense())
                #print theano_derivative, our_derivative   
            
                # Theano produces has more nans than we do during exponentiation. 
                # So we test only on entries where Theano is without NaN's    
                without_nans = np.nonzero(np.logical_not(np.isnan(theano_derivative.flatten())))[0]
                np.testing.assert_array_equal(theano_derivative.flatten()[without_nans], our_derivative.flatten()[without_nans]) 
開發者ID:mattloper,項目名稱:chumpy,代碼行數:53,代碼來源:test_ch.py


注:本文中的theano.tensor.jacobian方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。