本文整理汇总了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
示例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, :])
示例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
示例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)
示例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)
示例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)
###############################################################################
示例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)
示例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])
示例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
示例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])
示例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)
示例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)
示例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)
示例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)
示例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)