本文整理汇总了Python中dynet.dropout方法的典型用法代码示例。如果您正苦于以下问题:Python dynet.dropout方法的具体用法?Python dynet.dropout怎么用?Python dynet.dropout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dynet
的用法示例。
在下文中一共展示了dynet.dropout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: source_ranker_cache
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [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
示例2: target_ranker_cache
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def target_ranker_cache(self, rel):
"""
test mode only (no updates, no dropout)
:param rel: relation to create cache for quick score calculation once target is given
:returns: mode-appropriate pre-computation for association scores
"""
S = self.embeddings.as_array()
A = self.word_assoc_weights[rel].as_array()
if self.mode == BILINEAR_MODE:
return S.dot(A)
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 S.dot(ABC)
elif self.mode == TRANSLATIONAL_EMBED_MODE:
return S + A
elif self.mode == DISTMULT:
return S * A # elementwise, broadcast
示例3: score_from_source_cache
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [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)
示例4: evaluate_struct
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def evaluate_struct(self, fwd_out, back_out, lefts, rights, test=False):
fwd_span_out = []
for left_index, right_index in zip(lefts, rights):
fwd_span_out.append(fwd_out[right_index] - fwd_out[left_index - 1])
fwd_span_vec = dynet.concatenate(fwd_span_out)
back_span_out = []
for left_index, right_index in zip(lefts, rights):
back_span_out.append(back_out[left_index] - back_out[right_index + 1])
back_span_vec = dynet.concatenate(back_span_out)
hidden_input = dynet.concatenate([fwd_span_vec, back_span_vec])
if self.droprate > 0 and not test:
hidden_input = dynet.dropout(hidden_input, self.droprate)
hidden_output = self.activation(self.W1_struct * hidden_input + self.b1_struct)
scores = (self.W2_struct * hidden_output + self.b2_struct)
return scores
示例5: evaluate_label
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def evaluate_label(self, fwd_out, back_out, lefts, rights, test=False):
fwd_span_out = []
for left_index, right_index in zip(lefts, rights):
fwd_span_out.append(fwd_out[right_index] - fwd_out[left_index - 1])
fwd_span_vec = dynet.concatenate(fwd_span_out)
back_span_out = []
for left_index, right_index in zip(lefts, rights):
back_span_out.append(back_out[left_index] - back_out[right_index + 1])
back_span_vec = dynet.concatenate(back_span_out)
hidden_input = dynet.concatenate([fwd_span_vec, back_span_vec])
if self.droprate > 0 and not test:
hidden_input = dynet.dropout(hidden_input, self.droprate)
hidden_output = self.activation(self.W1_label * hidden_input + self.b1_label)
scores = (self.W2_label * hidden_output + self.b2_label)
return scores
示例6: dropout
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def dropout(h_keep_prob):
def compile_fn(di, dh):
p = dh['keep_prop']
Dropout = dy.dropout
def fn(di):
return {'out': Dropout(di['in'], p)}
return fn
return siso_dynet_module('Dropout', compile_fn, {'keep_prop': h_keep_prob})
示例7: dnn_net_simple
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def dnn_net_simple(num_classes):
# declaring hyperparameter
h_nonlin_name = D(['relu', 'tanh',
'elu']) # nonlinearity function names to choose from
h_opt_drop = D(
[0, 1]) # dropout optional hyperparameter; 0 is exclude, 1 is include
h_drop_keep_prob = D([0.25, 0.5,
0.75]) # dropout probability to choose from
h_num_hidden = D([64, 128, 256, 512, 1024
]) # number of hidden units for affine transform module
h_num_repeats = D([1, 2]) # 1 is appearing once, 2 is appearing twice
# defining search space topology
model = mo.siso_sequential([
flatten(),
mo.siso_repeat(
lambda: mo.siso_sequential([
dense(h_num_hidden),
nonlinearity(h_nonlin_name),
mo.siso_optional(lambda: dropout(h_drop_keep_prob), h_opt_drop),
]), h_num_repeats),
dense(D([num_classes]))
])
return model
示例8: dnn_cell
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def dnn_cell(h_num_hidden, h_nonlin_name, h_opt_drop, h_drop_keep_prob):
return mo.siso_sequential([
dense(h_num_hidden),
nonlinearity(h_nonlin_name),
mo.siso_optional(lambda: dropout(h_drop_keep_prob), h_opt_drop)
])
示例9: set_dropout
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def set_dropout(self, p):
self.bi_lstm.set_dropout(p)
self.dropout = p
示例10: disable_dropout
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def disable_dropout(self):
self.bi_lstm.disable_dropout()
self.dropout = None
示例11: build_tagging_graph
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [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
示例12: forward_one_multilayer
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def forward_one_multilayer(lstm_input, layer_states, dropout_amount=0.):
""" Goes forward for one multilayer RNN cell step.
Inputs:
lstm_input (dy.Expression): Some input to the step.
layer_states (list of dy.RNNState): The states of each layer in the cell.
dropout_amount (float, optional): The amount of dropout to apply, in
between the layers.
Returns:
(list of dy.Expression, list of dy.Expression), dy.Expression, (list of dy.RNNSTate),
representing (each layer's cell memory, each layer's cell hidden state),
the final hidden state, and (each layer's updated RNNState).
"""
num_layers = len(layer_states)
new_states = []
cell_states = []
hidden_states = []
state = lstm_input
for i in range(num_layers):
new_states.append(layer_states[i].add_input(state))
layer_c, layer_h = new_states[i].s()
state = layer_h
if i < num_layers - 1:
state = dy.dropout(state, dropout_amount)
cell_states.append(layer_c)
hidden_states.append(layer_h)
return (cell_states, hidden_states), state, new_states
示例13: encode_sequence
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def encode_sequence(sequence, rnns, embedder, dropout_amount=0.):
""" Encodes a sequence given RNN cells and an embedding function.
Inputs:
seq (list of str): The sequence to encode.
rnns (list of dy._RNNBuilder): The RNNs to use.
emb_fn (dict str->dy.Expression): Function that embeds strings to
word vectors.
size (int): The size of the RNN.
dropout_amount (float, optional): The amount of dropout to apply.
Returns:
(list of dy.Expression, list of dy.Expression), list of dy.Expression,
where the first pair is the (final cell memories, final cell states) of
all layers, and the second list is a list of the final layer's cell
state for all tokens in the sequence.
"""
layer_states = []
for rnn in rnns:
hidden_size = rnn.spec[2]
layer_states.append(rnn.initial_state([dy.zeroes((hidden_size, 1)),
dy.zeroes((hidden_size, 1))]))
outputs = []
for token in sequence:
rnn_input = embedder(token)
(cell_states, hidden_states), output, layer_states = \
forward_one_multilayer(rnn_input,
layer_states,
dropout_amount)
outputs.append(output)
return (cell_states, hidden_states), outputs
示例14: _get_intermediate_state
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [as 别名]
def _get_intermediate_state(self, state, dropout_amount=0.):
intermediate_state = dy.tanh(
du.linear_layer(
state, self.state_transform_weights))
return dy.dropout(intermediate_state, dropout_amount)
示例15: encode_ws
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import dropout [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