未验证 提交 72209769 编写于 作者: Z zhongpu 提交者: GitHub

fix if logic for dygraph, test=develop (#4447)

上级 5678d3f0
...@@ -37,7 +37,7 @@ class PrePostProcessLayer(Layer): ...@@ -37,7 +37,7 @@ class PrePostProcessLayer(Layer):
for cmd in self.process_cmd: for cmd in self.process_cmd:
if cmd == "a": # add residual connection if cmd == "a": # add residual connection
self.functors.append(lambda x, y: x + y if y else x) self.functors.append(lambda x, y: x + y if y is not None else x)
self.exec_order += "a" self.exec_order += "a"
elif cmd == "n": # add layer normalization elif cmd == "n": # add layer normalization
self.functors.append( self.functors.append(
...@@ -215,7 +215,7 @@ class MultiHeadAttentionLayer(Layer): ...@@ -215,7 +215,7 @@ class MultiHeadAttentionLayer(Layer):
y=transpose_k, y=transpose_k,
transpose_y=True) transpose_y=True)
#alpha=self._d_model**-0.5) #alpha=self._d_model**-0.5)
if attn_bias: if attn_bias is not None:
product += attn_bias product += attn_bias
weights = fluid.layers.softmax(product) weights = fluid.layers.softmax(product)
if self._dropout_rate: if self._dropout_rate:
......
...@@ -164,7 +164,7 @@ class Linear_chain_crf(fluid.dygraph.Layer): ...@@ -164,7 +164,7 @@ class Linear_chain_crf(fluid.dygraph.Layer):
"Transition": self._transition, "Transition": self._transition,
"Label": [label] "Label": [label]
} }
if length: if length is not None:
this_inputs['Length'] = [length] this_inputs['Length'] = [length]
self._helper.append_op( self._helper.append_op(
type='linear_chain_crf', type='linear_chain_crf',
...@@ -212,7 +212,7 @@ class Crf_decoding(fluid.dygraph.Layer): ...@@ -212,7 +212,7 @@ class Crf_decoding(fluid.dygraph.Layer):
viterbi_path = self._helper.create_variable_for_type_inference( viterbi_path = self._helper.create_variable_for_type_inference(
dtype=self._dtype) dtype=self._dtype)
this_inputs = {"Emission": [input], "Transition": self._transition, "Label": label} this_inputs = {"Emission": [input], "Transition": self._transition, "Label": label}
if length: if length is not None:
this_inputs['Length'] = [length] this_inputs['Length'] = [length]
self._helper.append_op( self._helper.append_op(
type='crf_decoding', type='crf_decoding',
...@@ -245,7 +245,7 @@ class Chunk_eval(fluid.dygraph.Layer): ...@@ -245,7 +245,7 @@ class Chunk_eval(fluid.dygraph.Layer):
num_correct_chunks = self._helper.create_variable_for_type_inference(dtype="int64") num_correct_chunks = self._helper.create_variable_for_type_inference(dtype="int64")
this_input = {"Inference": [input], "Label": [label]} this_input = {"Inference": [input], "Label": [label]}
if seq_length: if seq_length is not None:
this_input["SeqLength"] = [seq_length] this_input["SeqLength"] = [seq_length]
self._helper.append_op( self._helper.append_op(
......
...@@ -156,7 +156,7 @@ class BOW(fluid.dygraph.Layer): ...@@ -156,7 +156,7 @@ class BOW(fluid.dygraph.Layer):
fc_1 = self._fc1(bow_1) fc_1 = self._fc1(bow_1)
fc_2 = self._fc2(fc_1) fc_2 = self._fc2(fc_1)
prediction = self._fc_prediction(fc_2) prediction = self._fc_prediction(fc_2)
if label: if label is not None:
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(x=cost) avg_cost = fluid.layers.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label) acc = fluid.layers.accuracy(input=prediction, label=label)
...@@ -258,4 +258,4 @@ class BiGRU(fluid.dygraph.Layer): ...@@ -258,4 +258,4 @@ class BiGRU(fluid.dygraph.Layer):
acc = fluid.layers.accuracy(input=prediction, label=label) acc = fluid.layers.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc return avg_cost, prediction, acc
else: else:
return prediction return prediction
\ No newline at end of file
...@@ -169,7 +169,7 @@ class AttentionModel(fluid.dygraph.Layer): ...@@ -169,7 +169,7 @@ class AttentionModel(fluid.dygraph.Layer):
memory = self.attn_fc(enc_output) memory = self.attn_fc(enc_output)
attn = fluid.layers.matmul(query, memory, transpose_y=True) attn = fluid.layers.matmul(query, memory, transpose_y=True)
if mask: if mask is not None:
attn = fluid.layers.transpose(attn, [1, 0, 2]) attn = fluid.layers.transpose(attn, [1, 0, 2])
attn = fluid.layers.elementwise_add(attn, mask * 1000000000, -1) attn = fluid.layers.elementwise_add(attn, mask * 1000000000, -1)
attn = fluid.layers.transpose(attn, [1, 0, 2]) attn = fluid.layers.transpose(attn, [1, 0, 2])
......
...@@ -78,7 +78,7 @@ class PrePostProcessLayer(Layer): ...@@ -78,7 +78,7 @@ class PrePostProcessLayer(Layer):
self.functors = [] self.functors = []
for cmd in self.process_cmd: for cmd in self.process_cmd:
if cmd == "a": # add residual connection if cmd == "a": # add residual connection
self.functors.append(lambda x, y: x + y if y else x) self.functors.append(lambda x, y: x + y if y is not None else x)
elif cmd == "n": # add layer normalization elif cmd == "n": # add layer normalization
self.functors.append( self.functors.append(
self.add_sublayer( self.add_sublayer(
...@@ -156,7 +156,7 @@ class MultiHeadAttention(Layer): ...@@ -156,7 +156,7 @@ class MultiHeadAttention(Layer):
y=k, y=k,
transpose_y=True, transpose_y=True,
alpha=self.d_model**-0.5) alpha=self.d_model**-0.5)
if attn_bias: if attn_bias is not None:
product += attn_bias product += attn_bias
weights = layers.softmax(product) weights = layers.softmax(product)
if self.dropout_rate: if self.dropout_rate:
...@@ -936,4 +936,4 @@ class Transformer(Layer): ...@@ -936,4 +936,4 @@ class Transformer(Layer):
layers.gather_tree(predict_ids, parent_ids), [1, 2, 0]) layers.gather_tree(predict_ids, parent_ids), [1, 2, 0])
finished_scores = topk_scores finished_scores = topk_scores
return finished_seq, finished_scores return finished_seq, finished_scores
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册