本文整理匯總了Python中dynet.parameter方法的典型用法代碼示例。如果您正苦於以下問題:Python dynet.parameter方法的具體用法?Python dynet.parameter怎麽用?Python dynet.parameter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dynet
的用法示例。
在下文中一共展示了dynet.parameter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_multilinear_forward
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def test_multilinear_forward():
model = dy.Model()
a, b, c = np.random.RandomState(0).randn(3, 100)
ml = MultilinearFactored(n_features=100, n_inputs=3, n_components=5,
model=model)
dy_fwd = ml(dy.inputVector(a),
dy.inputVector(b),
dy.inputVector(c)).value()
U = [dy.parameter(u).value() for u in ml.get_components()]
expected = np.dot(U[0], a)
expected *= np.dot(U[1], b)
expected *= np.dot(U[2], c)
expected = np.sum(expected)
assert (expected - dy_fwd) ** 2 < 1e-4
示例2: word_repr
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def word_repr(self, char_seq, cembs):
# obtain the word representation when given its character sequence
wlen = len(char_seq)
if 'rgW%d'%wlen not in self.param_exprs:
self.param_exprs['rgW%d'%wlen] = dy.parameter(self.params['reset_gate_W'][wlen-1])
self.param_exprs['rgb%d'%wlen] = dy.parameter(self.params['reset_gate_b'][wlen-1])
self.param_exprs['cW%d'%wlen] = dy.parameter(self.params['com_W'][wlen-1])
self.param_exprs['cb%d'%wlen] = dy.parameter(self.params['com_b'][wlen-1])
chars = dy.concatenate(cembs)
reset_gate = dy.logistic(self.param_exprs['rgW%d'%wlen] * chars + self.param_exprs['rgb%d'%wlen])
word = dy.tanh(self.param_exprs['cW%d'%wlen] * dy.cmult(reset_gate,chars) + self.param_exprs['cb%d'%wlen])
if self.known_words is not None and tuple(char_seq) in self.known_words:
return (word + dy.lookup(self.params['word_embed'],self.known_words[tuple(char_seq)]))/2.
return word
示例3: __init__
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def __init__(self, lstm):
self.lstm = lstm
self.outputs = []
self.c = dynet.parameter(self.lstm.c0)
self.h = dynet.tanh(self.c)
self.W_i = dynet.parameter(self.lstm.W_i)
self.b_i = dynet.parameter(self.lstm.b_i)
self.W_f = dynet.parameter(self.lstm.W_f)
self.b_f = dynet.parameter(self.lstm.b_f)
self.W_c = dynet.parameter(self.lstm.W_c)
self.b_c = dynet.parameter(self.lstm.b_c)
self.W_o = dynet.parameter(self.lstm.W_o)
self.b_o = dynet.parameter(self.lstm.b_o)
示例4: predict_emb
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def predict_emb(self, chars):
dy.renew_cg()
finit = self.char_fwd_lstm.initial_state()
binit = self.char_bwd_lstm.initial_state()
H = dy.parameter(self.lstm_to_rep_params)
Hb = dy.parameter(self.lstm_to_rep_bias)
O = dy.parameter(self.mlp_out)
Ob = dy.parameter(self.mlp_out_bias)
pad_char = self.c2i[PADDING_CHAR]
char_ids = [pad_char] + chars + [pad_char]
embeddings = [self.char_lookup[cid] for cid in char_ids]
bi_fwd_out = finit.transduce(embeddings)
bi_bwd_out = binit.transduce(reversed(embeddings))
rep = dy.concatenate([bi_fwd_out[-1], bi_bwd_out[-1]])
return O * dy.tanh(H * rep + Hb) + Ob
示例5: build_tagging_graph
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def build_tagging_graph(self, sentence):
dy.renew_cg()
embeddings = [self.word_rep(w) for w in sentence]
lstm_out = self.word_bi_lstm.transduce(embeddings)
H = {}
Hb = {}
O = {}
Ob = {}
scores = {}
for att in self.attributes:
H[att] = dy.parameter(self.lstm_to_tags_params[att])
Hb[att] = dy.parameter(self.lstm_to_tags_bias[att])
O[att] = dy.parameter(self.mlp_out[att])
Ob[att] = dy.parameter(self.mlp_out_bias[att])
scores[att] = []
for rep in lstm_out:
score_t = O[att] * dy.tanh(H[att] * rep + Hb[att]) + Ob[att]
scores[att].append(score_t)
return scores
示例6: build_tagging_graph
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def build_tagging_graph(self, sentence):
dy.renew_cg()
embeddings = [self.word_rep(w) for w in sentence]
lstm_out = self.bi_lstm.transduce(embeddings)
H = dy.parameter(self.lstm_to_tags_params)
Hb = dy.parameter(self.lstm_to_tags_bias)
O = dy.parameter(self.mlp_out)
Ob = dy.parameter(self.mlp_out_bias)
scores = []
if options.bigram:
for rep, word in zip(lstm_out, sentence):
bi1 = dy.lookup(self.bigram_lookup, word[0], update=self.we_update)
bi2 = dy.lookup(self.bigram_lookup, word[1], update=self.we_update)
if self.dropout is not None:
bi1 = dy.dropout(bi1, self.dropout)
bi2 = dy.dropout(bi2, self.dropout)
score_t = O * dy.tanh(H * dy.concatenate(
[bi1,
rep,
bi2]) + Hb) + Ob
scores.append(score_t)
else:
for rep in lstm_out:
score_t = O * dy.tanh(H * rep + Hb) + Ob
scores.append(score_t)
return scores
示例7: encode_ws
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def encode_ws(self, X, train=False):
dy.renew_cg()
# Remove dy.parameters(...) for DyNet v.2.1
#w_ws = dy.parameter(self.w_ws)
#b_ws = dy.parameter(self.b_ws)
w_ws = self.w_ws
b_ws = self.b_ws
ipts = []
length = len(X[0])
for i in range(length):
uni = X[0][i]
bi = X[1][i]
ctype = X[2][i]
start = X[3][i]
end = X[4][i]
vec_uni = dy.concatenate([self.UNI[uid] for uid in uni])
vec_bi = dy.concatenate([self.BI[bid] for bid in bi])
vec_start = dy.esum([self.WORD[sid] for sid in start])
vec_end = dy.esum([self.WORD[eid] for eid in end])
vec_ctype = dy.concatenate([self.CTYPE[cid] for cid in ctype])
vec_at_i = dy.concatenate([vec_uni, vec_bi, vec_ctype, vec_start, vec_end])
if train is True:
vec_at_i = dy.dropout(vec_at_i, self.dropout_rate)
ipts.append(vec_at_i)
bilstm_outputs = self.ws_model.transduce(ipts)
observations = [w_ws*h+b_ws for h in bilstm_outputs]
return observations
示例8: encode_pt
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def encode_pt(self, X, train=False):
dy.renew_cg()
# Remove dy.parameters(...) for DyNet v.2.1
#w_pos = dy.parameter(self.w_pos)
#b_pos = dy.parameter(self.b_pos)
w_pos = self.w_pos
b_pos = self.b_pos
ipts = []
length = len(X[0])
for i in range(length):
cids = X[0][i]
wid = X[1][i]
tids = X[2][i]
vec_char = self.char_seq_model.transduce([self.UNI[cid] for cid in cids])[-1]
vec_tags = []
for tid in tids:
if tid == 0:
zero = dy.inputVector(np.zeros(self.dim_tag_emb))
vec_tags.append(zero)
else:
vec_tags.append(self.POS[tid])
vec_tag = dy.esum(vec_tags)
if wid == 0:
vec_word = dy.inputVector(np.zeros(self.dim_word))
else:
vec_word = self.WORD[wid]
vec_at_i = dy.concatenate([vec_word, vec_char, vec_tag])
if train is True:
vec_at_i = dy.dropout(vec_at_i, self.dropout_rate)
ipts.append(vec_at_i)
hiddens = self.pos_model.transduce(ipts)
probs = [dy.softmax(w_pos*h+b_pos) for h in hiddens]
return probs
示例9: ergm_score
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [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
示例10: rescore
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def rescore(self):
"""
Computes score based on current parameter and feature values, populates field
"""
self.dy_score = self.ergm_score()
self.score = self.dy_score.scalar_value()
self.score_is_stale = False
### EDGE ABLATION MODE ###
示例11: word_assoc_score
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [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))
示例12: hid_layer
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def hid_layer(self,x,dropout):
if dropout:
W = dy.dropout(dy.parameter(self._W1),0.3)
b = dy.dropout(dy.parameter(self._b1),0.3)
else:
W = dy.parameter(self._W1)
b = dy.parameter(self._b1)
return self.activation(W*x+b)
示例13: hid_2_layer
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def hid_2_layer(self,x,dropout):
if dropout:
W = dy.dropout(dy.parameter(self._W12),0.3)
b = dy.dropout(dy.parameter(self._b12),0.3)
else:
W = dy.parameter(self._W12)
b = dy.parameter(self._b12)
return self.activation(W*x+b)
示例14: out_layer
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def out_layer(self,x,dropout):
if dropout:
W = dy.dropout(dy.parameter(self._W2),0.3)
b = dy.dropout(dy.parameter(self._b2),0.3)
else:
W = dy.parameter(self._W2)
b = dy.parameter(self._b2)
return (W*x+b)
示例15: __call__
# 需要導入模塊: import dynet [as 別名]
# 或者: from dynet import parameter [as 別名]
def __call__(self, x):
b = dy.parameter(self.b)
W = dy.parameter(self.W)
out = b + W * x
if self.activation:
out = self.activation(out)
return out