本文整理汇总了Python中dynet.transpose方法的典型用法代码示例。如果您正苦于以下问题:Python dynet.transpose方法的具体用法?Python dynet.transpose怎么用?Python dynet.transpose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dynet
的用法示例。
在下文中一共展示了dynet.transpose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def forward(self, observations):
def log_sum_exp(scores):
npval = scores.npvalue()
argmax_score = np.argmax(npval)
max_score_expr = dy.pick(scores, argmax_score)
max_score_expr_broadcast = dy.concatenate([max_score_expr] * self.tagset_size)
return max_score_expr + dy.log(dy.sum_dim(dy.transpose(dy.exp(scores - max_score_expr_broadcast)),[1]))
init_alphas = [-1e10] * self.tagset_size
init_alphas[t2i[START_TAG]] = 0
for_expr = dy.inputVector(init_alphas)
for obs in observations:
alphas_t = []
for next_tag in range(self.tagset_size):
obs_broadcast = dy.concatenate([dy.pick(obs, next_tag)] * self.tagset_size)
next_tag_expr = for_expr + self.transitions[next_tag] + obs_broadcast
alphas_t.append(log_sum_exp(next_tag_expr))
for_expr = dy.concatenate(alphas_t)
terminal_expr = for_expr + self.transitions[t2i["<STOP>"]]
alpha = log_sum_exp(terminal_expr)
return alpha
示例2: score_snippets
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def score_snippets(snippets, scorer):
""" Scores snippets given a scorer.
Inputs:
snippets (list of Snippet): The snippets to score.
scorer (dy.Expression): Dynet vector against which to score the snippets.
Returns:
dy.Expression, list of str, where the first is the scores and the second
is the names of the snippets that were scored.
"""
snippet_expressions = [snippet.embedding for snippet in snippets]
all_snippet_embeddings = dy.concatenate(snippet_expressions, d=1)
if du.is_vector(scorer):
scorer = du.add_dim(scorer)
scores = dy.transpose(dy.transpose(scorer) * all_snippet_embeddings)
if scores.dim()[0][0] != len(snippets):
raise ValueError("Got " + str(scores.dim()[0][0]) + " scores for "
+ str(len(snippets)) + " snippets")
return scores, [snippet.name for snippet in snippets]
示例3: forward
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def forward(self, observations):
def log_sum_exp(scores):
npval = scores.npvalue()
argmax_score = np.argmax(npval)
max_score_expr = dy.pick(scores, argmax_score)
max_score_expr_broadcast = dy.concatenate([max_score_expr] * self.dim_output)
return max_score_expr + dy.log(dy.sum_elems(dy.transpose(dy.exp(scores - max_score_expr_broadcast))))
init_alphas = [-1e10] * self.dim_output
init_alphas[self.sp_s] = 0
for_expr = dy.inputVector(init_alphas)
for obs in observations:
alphas_t = []
for next_tag in range(self.dim_output):
obs_broadcast = dy.concatenate([dy.pick(obs, next_tag)] * self.dim_output)
next_tag_expr = for_expr + self.trans[next_tag] + obs_broadcast
alphas_t.append(log_sum_exp(next_tag_expr))
for_expr = dy.concatenate(alphas_t)
terminal_expr = for_expr + self.trans[self.sp_e]
alpha = log_sum_exp(terminal_expr)
return alpha
示例4: source_ranker_cache
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def source_ranker_cache(self, rel):
"""
test mode only (no updates, no dropout)
:param rel: relation to create cache for quick score calculation once source is given
:return: mode-appropriate pre-computation for association scores
"""
T = self.embeddings.as_array()
A = self.word_assoc_weights[rel].as_array()
if self.mode == BILINEAR_MODE:
return A.dot(T.transpose())
elif self.mode == DIAG_RANK1_MODE:
diag_A = np.diag(A[0])
rank1_BC = np.outer(A[1],A[2])
ABC = diag_A + rank1_BC
return ABC.dot(T.transpose())
elif self.mode == TRANSLATIONAL_EMBED_MODE:
return A - T
elif self.mode == DISTMULT:
return A * T # elementwise, broadcast
示例5: score_from_source_cache
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def score_from_source_cache(self, cache, src):
"""
test mode only (no updates, no dropout)
:param cache: cache computed earlier using source_ranker_cache
:param src: index of source node to create ranking of all targets for
:return: array of scores for all possible targets
"""
s = self.embeddings[src].npvalue()
if self.mode == BILINEAR_MODE:
return (s.dot(cache)).transpose()
elif self.mode == DIAG_RANK1_MODE:
return (s.dot(cache)).transpose()
elif self.mode == TRANSLATIONAL_EMBED_MODE:
diff_vecs = s + cache
return -np.sqrt((diff_vecs * diff_vecs).sum(1))
elif self.mode == DISTMULT:
return cache.dot(s)
示例6: _compute_guided_attention
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def _compute_guided_attention(self, att_vect, decoder_step, input_size, output_size):
if output_size <= 1 or input_size <= 1:
return dy.scalarInput(0)
target_probs = []
t1 = float(decoder_step) / output_size
for encoder_step in range(input_size):
target_probs.append(1.0 - np.exp(-((float(encoder_step) / input_size - t1) ** 2) / 0.08))
# print target_probs
target_probs = dy.inputVector(target_probs)
# print (target_probs.npvalue().shape, att_vect.npvalue().shape)
return dy.transpose(target_probs) * att_vect
示例7: linear_transform
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def linear_transform(exp, params):
""" Multiplies a dy.Expression and a set of parameters.
Inputs:
exp (dy.Expression): A Dynet tensor.
params (dy.Parameters): Dynet parameters.
Returns:
dy.Expression representing exp * params.
"""
if is_vector(exp):
exp = add_dim(exp)
return dy.transpose(exp) * params
示例8: __call__
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def __call__(self, query, keys, values=None):
if not values:
values = keys
query_t, keys_t, values_t = self.transform_arguments(query,
keys,
values)
scores = dy.transpose(query_t * keys_t)
distribution = dy.softmax(scores)
context_vector = values_t * distribution
return AttentionResult(scores, distribution, context_vector)
示例9: _score_vocabulary_tokens
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def _score_vocabulary_tokens(self, state):
scores = dy.transpose(du.linear_layer(state,
self.vocabulary_weights,
self.vocabulary_biases))
if scores.dim()[0][0] != len(self.vocabulary.inorder_tokens):
raise ValueError("Got " +
str(scores.dim()[0][0]) +
" scores for " +
str(len(self.vocabulary.inorder_tokens)) +
" vocabulary items")
return scores, self.vocabulary.inorder_tokens
示例10: _get_snippet_scorer
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def _get_snippet_scorer(self, state):
return dy.transpose(du.linear_layer(dy.transpose(state),
self.snippet_weights))
示例11: ergm_score
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def ergm_score(self):
"""
:return: ERGM score (dynet Expression) computed based on ERGM weights and features only
Does not populate any field
"""
W = dy.parameter(self.ergm_weights)
f = dy.transpose(dy.inputVector([self.feature_vals[k] for k in self.feature_set]))
return f * W
示例12: word_assoc_score
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def word_assoc_score(self, source_idx, target_idx, relation):
"""
NOTE THAT DROPOUT IS BEING APPLIED HERE
:param source_idx: embedding index of source atom
:param target_idx: embedding index of target atom
:param relation: relation type
:return: score
"""
# prepare
s = self.embeddings[source_idx]
if self.no_assoc:
A = dy.const_parameter(self.word_assoc_weights[relation])
else:
A = dy.parameter(self.word_assoc_weights[relation])
dy.dropout(A, self.dropout)
t = self.embeddings[target_idx]
# compute
if self.mode == BILINEAR_MODE:
return dy.transpose(s) * A * t
elif self.mode == DIAG_RANK1_MODE:
diag_A = dyagonalize(A[0])
rank1_BC = A[1] * dy.transpose(A[2])
ABC = diag_A + rank1_BC
return dy.transpose(s) * ABC * t
elif self.mode == TRANSLATIONAL_EMBED_MODE:
return -dy.l2_norm(s - t + A)
elif self.mode == DISTMULT:
return dy.sum_elems(dy.cmult(dy.cmult(s, A), t))
示例13: _compute_guided_attention
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def _compute_guided_attention(self, att_vect, decoder_step, num_characters, num_mgcs):
target_probs = []
t1 = float(decoder_step) / num_mgcs
for encoder_step in range(num_characters):
target_probs.append(1.0 - np.exp(-((float(encoder_step) / num_characters - t1) ** 2) / 0.1))
target_probs = dy.inputVector(target_probs)
return dy.transpose(target_probs) * att_vect
示例14: __call__
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def __call__(self, x, y):
W = dy.parameter(self.W)
w_x = dy.parameter(self.w_x)
w_y = dy.parameter(self.w_y)
b = dy.parameter(self.b)
out = dy.transpose(x) * W * y
out += dy.dot_product(w_x, x)
out += dy.dot_product(w_y, y)
out = dy.concatenate([dy.scalarInput(0)] * (self.n_out - 1) + [out])
out += b
return out
示例15: __call__
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import transpose [as 别名]
def __call__(self, query, options, gold, lengths, query_no):
if len(options) == 1:
return None, 0
final = []
if args.word_vectors:
qvecs = [dy.lookup(self.pEmbedding, w) for w in query]
qvec_max = dy.emax(qvecs)
qvec_mean = dy.average(qvecs)
for otext, features in options:
inputs = dy.inputTensor(features)
if args.word_vectors:
ovecs = [dy.lookup(self.pEmbedding, w) for w in otext]
ovec_max = dy.emax(ovecs)
ovec_mean = dy.average(ovecs)
inputs = dy.concatenate([inputs, qvec_max, qvec_mean, ovec_max, ovec_mean])
if args.drop > 0:
inputs = dy.dropout(inputs, args.drop)
h = inputs
for pH, pB in zip(self.hidden, self.bias):
h = dy.affine_transform([pB, pH, h])
if args.nonlin == "linear":
pass
elif args.nonlin == "tanh":
h = dy.tanh(h)
elif args.nonlin == "cube":
h = dy.cube(h)
elif args.nonlin == "logistic":
h = dy.logistic(h)
elif args.nonlin == "relu":
h = dy.rectify(h)
elif args.nonlin == "elu":
h = dy.elu(h)
elif args.nonlin == "selu":
h = dy.selu(h)
elif args.nonlin == "softsign":
h = dy.softsign(h)
elif args.nonlin == "swish":
h = dy.cmult(h, dy.logistic(h))
final.append(dy.sum_dim(h, [0]))
final = dy.concatenate(final)
nll = -dy.log_softmax(final)
dense_gold = []
for i in range(len(options)):
dense_gold.append(1.0 / len(gold) if i in gold else 0.0)
answer = dy.inputTensor(dense_gold)
loss = dy.transpose(answer) * nll
predicted_link = np.argmax(final.npvalue())
return loss, predicted_link