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


Python tensor.identity_like方法代碼示例

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


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

示例1: batch_jacobian

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import identity_like [as 別名]
def batch_jacobian(f, wrt, size=None, *args, **kwargs):
    """Computes the jacobian of f(x) w.r.t. x in parallel.

    Args:
        f: Symbolic function.
        x: Variables to differentiate with respect to.
        size: Expected vector size of f(x).
        *args: Additional positional arguments to pass to `f()`.
        **kwargs: Additional key-word arguments to pass to `f()`.

    Returns:
        Theano tensor.
    """
    if isinstance(wrt, T.TensorVariable):
        if size is None:
            y = f(wrt, *args, **kwargs).shape[-1]
        x_rep = T.tile(wrt, (size, 1))
        y_rep = f(x_rep, *args, **kwargs)
    else:
        if size is None:
            size = f(*wrt, *args, **kwargs).shape[-1]
        x_rep = [T.tile(x, (size, 1)) for x in wrt]
        y_rep = f(*x_rep, *args, **kwargs)

    J = T.grad(
        cost=None,
        wrt=x_rep,
        known_grads={y_rep: T.identity_like(y_rep)},
        disconnected_inputs="ignore",
    )
    return J 
開發者ID:anassinator,項目名稱:ilqr,代碼行數:33,代碼來源:autodiff.py

示例2: get_output_for

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import identity_like [as 別名]
def get_output_for(self, inputs, **kwargs):
        """
        Compute diffusion convolution of inputs.

        """

        A = inputs[0]
        X = inputs[1]

        # Normalize by degree.
        A = A / (T.sum(A, 0) + 1.0)

        Apow_list = [T.identity_like(A)]
        for i in range(1, self.parameters.num_hops + 1):
            Apow_list.append(A.dot(Apow_list[-1]))
        Apow = T.stack(Apow_list)

        Apow_dot_X = T.dot(Apow, X)

        Apow_dot_X_times_W = Apow_dot_X * self.W

        out = T.reshape(
            self.nonlinearity(T.mean(Apow_dot_X_times_W, 1)),
            (1, (self.parameters.num_hops + 1) * self.num_features)
        )

        return out 
開發者ID:jcatw,項目名稱:dcnn,代碼行數:29,代碼來源:layers.py

示例3: fast_jacobian

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import identity_like [as 別名]
def fast_jacobian(expr, wrt, chunk_size=16, func=None):
    '''
    Computes the jacobian by tiling the inputs
    Copied from https://gist.github.com/aam-at/2b2bc5c35850b553d4ec
    '''
    assert isinstance(expr, Variable), \
        "tensor.jacobian expects a Variable as `expr`"
    assert expr.ndim < 2, \
        ("tensor.jacobian expects a 1 dimensional variable as "
         "`expr`. If not use flatten to make it a vector")

    num_chunks = tt.ceil(1.0 * expr.shape[0] / chunk_size)
    num_chunks = tt.cast(num_chunks, 'int32')
    steps = tt.arange(num_chunks)
    remainder = expr.shape[0] % chunk_size

    def chunk_grad(i):
        ''' operates on a subset of the gradient variables '''
        wrt_rep = tt.tile(wrt, (chunk_size, 1))
        if func is not None:
            expr_rep = func(wrt_rep)
        else:
            expr_rep, _ = theano.scan(
                fn=lambda wrt_: theano.clone(expr, {wrt: wrt_}),
                sequences=wrt_rep)
        chunk_expr_grad = tt.roll(
            tt.identity_like(expr_rep),
            i * chunk_size,
            axis=1)
        return tt.grad(cost=None,
                       wrt=wrt_rep,
                       known_grads={
                           expr_rep: chunk_expr_grad
                       })

    grads, _ = theano.scan(chunk_grad, sequences=steps)
    grads = grads.reshape((chunk_size * grads.shape[0], wrt.shape[0]))
    jac = ifelse.ifelse(tt.eq(remainder, 0), grads, grads[:expr.shape[0], :])
    return jac 
開發者ID:mcgillmrl,項目名稱:kusanagi,代碼行數:41,代碼來源:utils_.py

示例4: __call__

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import identity_like [as 別名]
def __call__(self, loss):
        loss += K.sum(K.square(self.p.dot(self.p.T) - T.identity_like(self.p))) * self.strength
        return loss 
開發者ID:williamleif,項目名稱:socialsent,代碼行數:5,代碼來源:embedding_transformer.py

示例5: lda_loss

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import identity_like [as 別名]
def lda_loss(n_components, margin):
    """
    The main loss function (inner_lda_objective) is wrapped in this function due to
    the constraints imposed by Keras on objective functions
    """
    def inner_lda_objective(y_true, y_pred):
        """
        It is the loss function of LDA as introduced in the original paper. 
        It is adopted from the the original implementation in the following link:
        https://github.com/CPJKU/deep_lda
        Note: it is implemented by Theano tensor operations, and does not work on Tensorflow backend
        """
        r = 1e-4

        # init groups
        yt = T.cast(y_true.flatten(), "int32")
        groups = numpy_unique(yt)

        def compute_cov(group, Xt, yt):
            Xgt = Xt[T.eq(yt, group).nonzero()[0], :]
            Xgt_bar = Xgt - T.mean(Xgt, axis=0)
            m = T.cast(Xgt_bar.shape[0], 'float32')
            return (1.0 / (m - 1)) * T.dot(Xgt_bar.T, Xgt_bar)

        # scan over groups
        covs_t, updates = theano.scan(fn=compute_cov, outputs_info=None,
                                      sequences=[groups], non_sequences=[y_pred, yt])

        # compute average covariance matrix (within scatter)
        Sw_t = T.mean(covs_t, axis=0)

        # compute total scatter
        Xt_bar = y_pred - T.mean(y_pred, axis=0)
        m = T.cast(Xt_bar.shape[0], 'float32')
        St_t = (1.0 / (m - 1)) * T.dot(Xt_bar.T, Xt_bar)

        # compute between scatter
        Sb_t = St_t - Sw_t

        # cope for numerical instability (regularize)
        Sw_t += T.identity_like(Sw_t) * r

        # return T.cast(T.neq(yt[0], -1), 'float32')*T.nlinalg.trace(T.dot(T.nlinalg.matrix_inverse(St_t), Sb_t))

        # compute eigenvalues
        evals_t = T.slinalg.eigvalsh(Sb_t, Sw_t)

        # get eigenvalues
        top_k_evals = evals_t[-n_components:]

        # maximize variance between classes
        # (k smallest eigenvalues below threshold)
        thresh = T.min(top_k_evals) + margin
        top_k_evals = top_k_evals[(top_k_evals <= thresh).nonzero()]
        costs = T.mean(top_k_evals)

        return -costs

    return inner_lda_objective 
開發者ID:VahidooX,項目名稱:DeepLDA,代碼行數:61,代碼來源:objectives.py

示例6: test_grad_W

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import identity_like [as 別名]
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)) 
開發者ID:zchengquan,項目名稱:TextDetector,代碼行數:46,代碼來源:test_s3c_misc.py


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