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


Python tensor.diag方法代碼示例

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


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

示例1: compute_output_shape

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def compute_output_shape(self, input_shape):
        if len(input_shape) != 3:
            raise RuntimeError("Expects 3 inputs: L, mu, a")
        for i, shape in enumerate(input_shape):
            if len(shape) != 2:
                raise RuntimeError("Input {} has {} dimensions but should have 2".format(i, len(shape)))
        assert self.mode in ('full','diag')
        if self.mode == 'full':
            expected_elements = (self.nb_actions * self.nb_actions + self.nb_actions) // 2
        elif self.mode == 'diag':
            expected_elements = self.nb_actions
        else:
            expected_elements = None
        assert expected_elements is not None
        if input_shape[0][1] != expected_elements:
            raise RuntimeError("Input 0 (L) should have {} elements but has {}".format(input_shape[0][1]))
        if input_shape[1][1] != self.nb_actions:
            raise RuntimeError(
                "Input 1 (mu) should have {} elements but has {}".format(self.nb_actions, input_shape[1][1]))
        if input_shape[2][1] != self.nb_actions:
            raise RuntimeError(
                "Input 2 (action) should have {} elements but has {}".format(self.nb_actions, input_shape[1][1]))
        return input_shape[0][0], 1 
開發者ID:wau,項目名稱:keras-rl2,代碼行數:25,代碼來源:dqn.py

示例2: OrthogonalInit

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def OrthogonalInit(rng, shape):
    if len(shape) != 2:
        raise ValueError

    if shape[0] == shape[1]:
        # For square weight matrices we can simplify the logic
        # and be more exact:
        M = rng.randn(*shape).astype(theano.config.floatX)
        Q, R = numpy.linalg.qr(M)
        Q = Q * numpy.sign(numpy.diag(R))
        return Q

    M1 = rng.randn(shape[0], shape[0]).astype(theano.config.floatX)
    M2 = rng.randn(shape[1], shape[1]).astype(theano.config.floatX)

    # QR decomposition of matrix with entries in N(0, 1) is random
    Q1, R1 = numpy.linalg.qr(M1)
    Q2, R2 = numpy.linalg.qr(M2)
    # Correct that NumPy doesn't force diagonal of R to be non-negative
    Q1 = Q1 * numpy.sign(numpy.diag(R1))
    Q2 = Q2 * numpy.sign(numpy.diag(R2))

    n_min = min(shape[0], shape[1])
    return numpy.dot(Q1[:, :n_min], Q2[:n_min, :]) 
開發者ID:sordonia,項目名稱:hred-qs,代碼行數:26,代碼來源:utils.py

示例3: _grab_probs

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def _grab_probs(class_probs, target, use_fast_ver=False):
    if class_probs.ndim == 3:
        class_probs = class_probs.reshape((-1, class_probs.shape[-1]))

    shape0 = class_probs.shape[0]
    shape1 = class_probs.shape[1]

    p = None
    if target.ndim == 2 and use_fast_ver:
        target = target.flatten()
        cp = class_probs.reshape((target.shape[0], -1))
        p = TT.diag(cp.T[target])
    else:
        if target.ndim > 1:
            target = target.flatten()
        assert target.ndim == 1, 'make sure target is a vector of ints'
        assert 'int' in target.dtype
        pos = TT.arange(shape0)*shape1
        new_targ = target + pos
        p = class_probs.reshape((shape0*shape1, 1))[new_targ].reshape((shape0,))
    return p 
開發者ID:caglar,項目名稱:Attentive_reader,代碼行數:23,代碼來源:costs.py

示例4: cross_entropy

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def cross_entropy(self, yhat):
        return T.cast(T.mean(-T.log(T.diag(yhat) + 1e-24)), theano.config.floatX) 
開發者ID:mquad,項目名稱:hgru4rec,代碼行數:4,代碼來源:hgru4rec.py

示例5: bpr

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def bpr(self, yhat):
        return T.cast(T.mean(-T.log(T.nnet.sigmoid(T.diag(yhat) - yhat.T))), theano.config.floatX) 
開發者ID:mquad,項目名稱:hgru4rec,代碼行數:4,代碼來源:hgru4rec.py

示例6: top1

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def top1(self, yhat):
        yhatT = yhat.T
        return T.cast(T.mean(
            T.mean(T.nnet.sigmoid(-T.diag(yhat) + yhatT) + T.nnet.sigmoid(yhatT ** 2), axis=0) - T.nnet.sigmoid(
                T.diag(yhat) ** 2) / self.batch_size), theano.config.floatX)

    ############################################################################### 
開發者ID:mquad,項目名稱:hgru4rec,代碼行數:9,代碼來源:hgru4rec.py

示例7: __init__

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def __init__(self, nb_actions, mode='full', **kwargs):
        if mode not in ('full', 'diag'):
            raise RuntimeError('Unknown mode "{}" in NAFLayer.'.format(self.mode))

        self.nb_actions = nb_actions
        self.mode = mode
        super(NAFLayer, self).__init__(**kwargs) 
開發者ID:wau,項目名稱:keras-rl2,代碼行數:9,代碼來源:dqn.py

示例8: GrabProbs

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def GrabProbs(classProbs, target, gRange=None):
    if classProbs.ndim > 2:
        classProbs = classProbs.reshape((classProbs.shape[0] * classProbs.shape[1], classProbs.shape[2]))
    else:
        classProbs = classProbs

    if target.ndim > 1:
        tflat = target.flatten()
    else:
        tflat = target
    return T.diag(classProbs.T[tflat]) 
開發者ID:julianser,項目名稱:hred-latent-piecewise,代碼行數:13,代碼來源:utils.py

示例9: _get_pi_var

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def _get_pi_var(self):
        w_var, lambda_var = self.param_vars
        A_split_var, b_split_var, _ = self._get_A_b_c_split_vars()

        A_var = T.tensordot(A_split_var, w_var / self.repeats, axes=(0, 0)) + T.diag(lambda_var)
        B_var = T.tensordot(b_split_var, w_var / self.repeats, axes=(0, 0))
        pi_var = T.batched_tensordot(T.nlinalg.matrix_inverse(A_var), B_var, axes=(2, 1))  # preprocessed units
        return pi_var 
開發者ID:alexlee-gk,項目名稱:visual_dynamics,代碼行數:10,代碼來源:servoing_policy.py

示例10: GrabProbs

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def GrabProbs(class_probs, target, gRange=None):
    if class_probs.ndim > 2:
        class_probs = class_probs.reshape((class_probs.shape[0] * class_probs.shape[1], class_probs.shape[2]))
    else:
        class_probs = class_probs
    if target.ndim > 1:
        tflat = target.flatten()
    else:
        tflat = target
     
    # return grabber(class_probs, target).flatten()
    ### Hack for Theano, much faster than [x, y] indexing 
    ### avoids a copy onto the GPU
    return T.diag(class_probs.T[tflat]) 
開發者ID:sordonia,項目名稱:hred-qs,代碼行數:16,代碼來源:utils.py

示例11: cross_entropy

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def cross_entropy(self, yhat):
        if self.smoothing:
            n_out = self.batch_size + self.n_sample
            return T.cast(T.mean((1.0-(n_out/(n_out-1))*self.smoothing) * (-T.log(T.diag(yhat)+1e-24)) + (self.smoothing/(n_out-1)) * T.sum(-T.log(yhat+1e-24), axis=1)), theano.config.floatX)
        else:
            return T.cast(T.mean(-T.log(T.diag(yhat)+1e-24)), theano.config.floatX) 
開發者ID:gabrielspmoreira,項目名稱:chameleon_recsys,代碼行數:8,代碼來源:gru4rec2.py

示例12: cross_entropy_logits

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def cross_entropy_logits(self, yhat):
        if self.smoothing:
            n_out = self.batch_size + self.n_sample
            return T.cast(T.mean((1.0-(n_out/(n_out-1))*self.smoothing) * T.diag(yhat) + (self.smoothing/(n_out-1)) * T.sum(yhat, axis=1)), theano.config.floatX)
        else:
            return T.cast(T.mean(T.diag(yhat)), theano.config.floatX) 
開發者ID:gabrielspmoreira,項目名稱:chameleon_recsys,代碼行數:8,代碼來源:gru4rec2.py

示例13: bpr

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def bpr(self, yhat):
        return T.cast(T.mean(-T.log(T.nnet.sigmoid(T.diag(yhat)-yhat.T))), theano.config.floatX) 
開發者ID:gabrielspmoreira,項目名稱:chameleon_recsys,代碼行數:4,代碼來源:gru4rec2.py

示例14: bpr_max

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def bpr_max(self, yhat):
        softmax_scores = self.softmax_neg(yhat).T
        return T.cast(T.mean(-T.log(T.sum(T.nnet.sigmoid(T.diag(yhat)-yhat.T)*softmax_scores, axis=0)+1e-24)+self.bpreg*T.sum((yhat.T**2)*softmax_scores, axis=0)), theano.config.floatX) 
開發者ID:gabrielspmoreira,項目名稱:chameleon_recsys,代碼行數:5,代碼來源:gru4rec2.py

示例15: top1

# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import diag [as 別名]
def top1(self, yhat):
        yhatT = yhat.T
        return T.cast(T.mean(T.mean(T.nnet.sigmoid(-T.diag(yhat)+yhatT)+T.nnet.sigmoid(yhatT**2), axis=0)-T.nnet.sigmoid(T.diag(yhat)**2)/(self.batch_size+self.n_sample)), theano.config.floatX) 
開發者ID:gabrielspmoreira,項目名稱:chameleon_recsys,代碼行數:5,代碼來源:gru4rec2.py


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