本文整理汇总了Python中theano.tensor.eye方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.eye方法的具体用法?Python tensor.eye怎么用?Python tensor.eye使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.eye方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _make_rnn
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def _make_rnn(self, seq_length=4):
self.embedding_dim = embedding_dim = 3
self.vocab_size = vocab_size = 10
self.seq_length = seq_length
def compose_network(h_prev, inp, embedding_dim, model_dim, vs, name="compose"):
# Just add the two embeddings!
W = T.concatenate([T.eye(model_dim), T.eye(model_dim)], axis=0)
i = T.concatenate([h_prev, inp], axis=1)
return i.dot(W)
X = T.imatrix("X")
training_mode = T.scalar("training_mode")
vs = VariableStore()
embeddings = np.arange(vocab_size).reshape(
(vocab_size, 1)).repeat(embedding_dim, axis=1)
self.model = RNN(
embedding_dim, embedding_dim, vocab_size, seq_length, compose_network,
IdentityLayer, training_mode, None, vs,
X=X, make_test_fn=True, initial_embeddings=embeddings)
示例2: get_output_for
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def get_output_for(self, input, init=False, **kwargs):
if input.ndim > 2:
# if the input has more than two dimensions, flatten it into a
# batch of feature vectors.
input = input.flatten(2)
activation = T.tensordot(input, self.W, [[1], [0]])
abs_dif = (T.sum(abs(activation.dimshuffle(0,1,2,'x') - activation.dimshuffle('x',1,2,0)),axis=2)
+ 1e6 * T.eye(input.shape[0]).dimshuffle(0,'x',1))
if init:
mean_min_abs_dif = 0.5 * T.mean(T.min(abs_dif, axis=2),axis=0)
abs_dif /= mean_min_abs_dif.dimshuffle('x',0,'x')
self.init_updates = [(self.log_weight_scale, self.log_weight_scale-T.log(mean_min_abs_dif).dimshuffle(0,'x'))]
f = T.sum(T.exp(-abs_dif),axis=2)
if init:
mf = T.mean(f,axis=0)
f -= mf.dimshuffle('x',0)
self.init_updates.append((self.b, -mf))
else:
f += self.b.dimshuffle('x',0)
return T.concatenate([input, f], axis=1)
示例3: linear_mmd2_and_hotelling
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def linear_mmd2_and_hotelling(X, Y, biased=True, reg=0):
if not biased:
raise ValueError("linear_mmd2_and_hotelling only works for biased est")
n = X.shape[0]
p = X.shape[1]
Z = X - Y
Z_bar = Z.mean(axis=0)
mmd2 = Z_bar.dot(Z_bar)
Z_cent = Z - Z_bar
S = Z_cent.T.dot(Z_cent) / (n - 1)
# z' inv(S) z = z' inv(L L') z = z' inv(L)' inv(L) z = ||inv(L) z||^2
L = slinalg.cholesky(S + reg * T.eye(p))
Linv_Z_bar = slinalg.solve_lower_triangular(L, Z_bar)
lambda_ = n * Linv_Z_bar.dot(Linv_Z_bar)
# happens on the CPU!
return mmd2, lambda_
示例4: compute_log_ei
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def compute_log_ei(self, x, incumbent):
Kzz = compute_kernel(self.lls, self.lsf, self.z, self.z) + T.eye(self.z.shape[ 0 ]) * self.jitter * T.exp(self.lsf)
KzzInv = T.nlinalg.MatrixInversePSD()(Kzz)
LLt = T.dot(self.LParamPost, T.transpose(self.LParamPost))
covCavityInv = KzzInv + LLt * casting(self.n_points - self.set_for_training) / casting(self.n_points)
covCavity = T.nlinalg.MatrixInversePSD()(covCavityInv)
meanCavity = T.dot(covCavity, casting(self.n_points - self.set_for_training) / casting(self.n_points) * self.mParamPost)
KzzInvcovCavity = T.dot(KzzInv, covCavity)
KzzInvmeanCavity = T.dot(KzzInv, meanCavity)
Kxz = compute_kernel(self.lls, self.lsf, x, self.z)
B = T.dot(KzzInvcovCavity, KzzInv) - KzzInv
v_out = T.exp(self.lsf) + T.dot(Kxz * T.dot(Kxz, B), T.ones_like(self.z[ : , 0 : 1 ])) # + T.exp(self.lvar_noise)
m_out = T.dot(Kxz, KzzInvmeanCavity)
s = (incumbent - m_out) / T.sqrt(v_out)
log_ei = T.log((incumbent - m_out) * ratio(s) + T.sqrt(v_out)) + log_n_pdf(s)
return log_ei
示例5: recurrence_relation
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def recurrence_relation(y, y_mask, blank_symbol):
n_y = y.shape[0]
blanks = tensor.zeros((2, y.shape[1])) + blank_symbol
ybb = tensor.concatenate((y, blanks), axis=0).T
sec_diag = (tensor.neq(ybb[:, :-2], ybb[:, 2:]) *
tensor.eq(ybb[:, 1:-1], blank_symbol) *
y_mask.T)
# r1: LxL
# r2: LxL
# r3: LxLxB
r2 = tensor.eye(n_y, k=1)
r3 = (tensor.eye(n_y, k=2).dimshuffle(0, 1, 'x') *
sec_diag.dimshuffle(1, 'x', 0))
return r2, r3
示例6: path_probabs
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def path_probabs(cls, y, y_hat, y_mask, y_hat_mask, blank_symbol):
pred_y = cls.class_batch_to_labeling_batch(y, y_hat, y_hat_mask)
r2, r3 = cls.recurrence_relation(y, y_mask, blank_symbol)
def step(p_curr, p_prev):
# instead of dot product, we * first
# and then sum oven one dimension.
# objective: T.dot((p_prev)BxL, LxLxB)
# solusion: Lx1xB * LxLxB --> LxLxB --> (sumover)xLxB
dotproduct = (p_prev + tensor.dot(p_prev, r2) +
(p_prev.dimshuffle(1, 'x', 0) * r3).sum(axis=0).T)
return p_curr.T * dotproduct * y_mask.T # B x L
probabilities, _ = theano.scan(
step,
sequences=[pred_y],
outputs_info=[tensor.eye(y.shape[0])[0] * tensor.ones(y.T.shape)])
return probabilities, probabilities.shape
示例7: log_path_probabs
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def log_path_probabs(cls, y, y_hat, y_mask, y_hat_mask, blank_symbol):
pred_y = cls.class_batch_to_labeling_batch(y, y_hat, y_hat_mask)
r2, r3 = cls.recurrence_relation(y, y_mask, blank_symbol)
def step(log_p_curr, log_p_prev):
p1 = log_p_prev
p2 = cls.log_dot_matrix(p1, r2)
p3 = cls.log_dot_tensor(p1, r3)
p123 = cls.log_add(p3, cls.log_add(p1, p2))
return (log_p_curr.T +
p123 +
cls._epslog(y_mask.T))
log_probabilities, _ = theano.scan(
step,
sequences=[cls._epslog(pred_y)],
outputs_info=[cls._epslog(tensor.eye(y.shape[0])[0] *
tensor.ones(y.T.shape))])
return log_probabilities
示例8: Noise
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def Noise(hyp, X1, X2=None, all_pairs=True):
''' Noise kernel. Takes as an input a distance matrix D
and creates a new matrix as Kij = sn2 if Dij == 0 else 0'''
if X2 is None:
X2 = X1
sn2 = hyp**2
if all_pairs and X1 is X2:
# D = (X1[:,None,:] - X2[None,:,:]).sum(2)
K = tt.eye(X1.shape[0])*sn2
return K
else:
# D = (X1 - X2).sum(1)
if X1 is X2:
K = tt.ones((X1.shape[0],))*sn2
else:
K = 0
return K
# K = tt.eq(D,0)*sn2
# return K
示例9: _recurrence_relation
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def _recurrence_relation(y, y_mask):
# with blank symbol of -1 this falls back to the recurrence that fails
# with repeating symbols!
blank_symbol = -1
n_y = y.shape[0]
blanks = tensor.zeros((2, y.shape[1])) + blank_symbol
ybb = tensor.concatenate((y, blanks), axis=0).T
sec_diag = (tensor.neq(ybb[:, :-2], ybb[:, 2:]) *
tensor.eq(ybb[:, 1:-1], blank_symbol) *
y_mask.T)
# r1: LxL
# r2: LxL
# r3: LxLxB
r2 = tensor.eye(n_y, k=1)
r3 = (tensor.eye(n_y, k=2).dimshuffle(0, 1, 'x') *
sec_diag.dimshuffle(1, 'x', 0))
return r2, r3
示例10: _log_path_probs
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def _log_path_probs(y, y_mask, y_hat, y_hat_mask):
pred_y = _class_batch_to_labeling_batch(y, y_hat, y_hat_mask)
r2, r3 = _recurrence_relation(y, y_mask)
def step(log_p_curr, log_p_prev):
p1 = log_p_prev
p2 = _log_dot_matrix(p1, r2)
p3 = _log_dot_tensor(p1, r3)
p123 = _log_add(p3, _log_add(p1, p2))
return (log_p_curr.T +
p123 +
_epslog(y_mask.T))
log_probabilities, _ = theano.scan(
step,
sequences=[_epslog(pred_y)],
outputs_info=[_epslog(tensor.eye(y.shape[0])[0] *
tensor.ones(y.T.shape))])
return log_probabilities
示例11: plain_ctc
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def plain_ctc(self, ):
labels2 = tt.concatenate((self.labels, [self.blank, self.blank]))
sec_diag = tt.neq(labels2[:-2], labels2[2:]) * \
tt.eq(labels2[1:-1], self.blank)
recurrence_relation = \
tt.eye(self.n) + \
tt.eye(self.n, k=1) + \
tt.eye(self.n, k=2) * sec_diag.dimshuffle((0, 'x'))
pred_y = self.inpt[:, self.labels]
probabilities, _ = theano.scan(
lambda curr, accum: curr * tt.dot(accum, recurrence_relation),
sequences=[pred_y],
outputs_info=[tt.eye(self.n)[0]]
)
# TODO: -2 only if blank at end
labels_probab = tt.sum(probabilities[-1, -2:])
self.cost = -tt.log(labels_probab)
self.debug = probabilities.T
示例12: _plain_ctc
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def _plain_ctc(self, ):
labels2 = tt.concatenate((self.labels, [self.blank, self.blank]))
sec_diag = tt.neq(labels2[:-2], labels2[2:]) * tt.eq(labels2[1:-1], self.blank)
# Last two entries of sec_diag do not matter as they multiply zero rows below.
recurrence_relation = \
tt.eye(self.n) + \
tt.eye(self.n, k=1) + \
tt.eye(self.n, k=2) * sec_diag.dimshuffle((0, 'x'))
pred_y = self.inpt[:, self.labels]
fwd_pbblts, _ = th.scan(
lambda curr, accum: curr * tt.dot(accum, recurrence_relation),
sequences=[pred_y],
outputs_info=[tt.eye(self.n)[0]]
)
# TODO: Add probabilites[-1, -2] only if last label is blank.
# liklihood = ifelse(tt.eq(self.n, 1), fwd_pbblts[-1, -1],
# ifelse(tt.neq(self.labels[-1], self.blank), fwd_pbblts[-1, -1],
# fwd_pbblts[-1, -1] + fwd_pbblts[-1, -2]))
liklihood = fwd_pbblts[-1, -1]
self.cost = -tt.log(liklihood)
self.debug = fwd_pbblts.T
示例13: recurrence_relation
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def recurrence_relation(y, y_mask):
# with blank symbol of -1 this falls back to the recurrence that fails
# with repeating symbols!
blank_symbol = -1
n_y = y.shape[0]
blanks = tensor.zeros((2, y.shape[1])) + blank_symbol
ybb = tensor.concatenate((y, blanks), axis=0).T
sec_diag = (tensor.neq(ybb[:, :-2], ybb[:, 2:]) *
tensor.eq(ybb[:, 1:-1], blank_symbol) *
y_mask.T)
# r1: LxL
# r2: LxL
# r3: LxLxB
r2 = tensor.eye(n_y, k=1)
r3 = (tensor.eye(n_y, k=2).dimshuffle(0, 1, 'x') *
sec_diag.dimshuffle(1, 'x', 0))
return r2, r3
示例14: log_path_probs
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def log_path_probs(y, y_mask, y_hat, y_hat_mask):
pred_y = class_batch_to_labeling_batch(y, y_hat, y_hat_mask)
r2, r3 = recurrence_relation(y, y_mask)
def step(log_p_curr, log_p_prev):
p1 = log_p_prev
p2 = _log_dot_matrix(p1, r2)
p3 = _log_dot_tensor(p1, r3)
p123 = _log_add(p3, _log_add(p1, p2))
return (log_p_curr.T +
p123 +
_epslog(y_mask.T))
log_probabilities, _ = theano.scan(
step,
sequences=[_epslog(pred_y)],
outputs_info=[_epslog(tensor.eye(y.shape[0])[0] *
tensor.ones(y.T.shape))])
return log_probabilities
示例15: _ctc_normal
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import eye [as 别名]
def _ctc_normal(self, predict,labels):
n = labels.shape[0]
labels2 = T.concatenate((labels, [self.tpo["CTC_blank"], self.tpo["CTC_blank"]]))
sec_diag = T.neq(labels2[:-2], labels2[2:]) * \
T.eq(labels2[1:-1], self.tpo["CTC_blank"])
recurrence_relation = \
T.eye(n) + \
T.eye(n, k=1) + \
T.eye(n, k=2) * sec_diag.dimshuffle((0, 'x'))
pred_y = predict[:, labels]
probabilities, _ = theano.scan(
lambda curr, accum: curr * T.dot(accum, recurrence_relation),
sequences=[pred_y],
outputs_info=[T.eye(n)[0]]
)
labels_probab = T.sum(probabilities[-1, -2:])
return -T.log(labels_probab)