Skip to content

Commit 0b0d3d0

Browse files
authored
Merge pull request #2134 from lcy-seso/add_param_attr_to_nce
add param_attr to nce_layer and enable multiple inputs.
2 parents be8e3b5 + 7556cef commit 0b0d3d0

File tree

5 files changed

+117
-6
lines changed

5 files changed

+117
-6
lines changed

python/paddle/trainer_config_helpers/layers.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,12 +4921,14 @@ def crf_decoding_layer(input,
49214921

49224922
@wrap_act_default(act=SigmoidActivation())
49234923
@wrap_bias_attr_default(has_bias=True)
4924+
@wrap_param_attr_default()
49244925
@wrap_name_default()
49254926
@layer_support()
49264927
def nce_layer(input,
49274928
label,
4928-
num_classes,
4929+
num_classes=None,
49294930
act=None,
4931+
param_attr=None,
49304932
weight=None,
49314933
num_neg_samples=10,
49324934
neg_distribution=None,
@@ -4942,7 +4944,8 @@ def nce_layer(input,
49424944
49434945
.. code-block:: python
49444946
4945-
cost = nce_layer(input=layer1, label=layer2, weight=layer3,
4947+
cost = nce_layer(input=[layer1, layer2], label=layer2,
4948+
param_attr=[attr1, attr2], weight=layer3,
49464949
num_classes=3, neg_distribution=[0.1,0.3,0.6])
49474950
49484951
:param name: layer name
@@ -4957,6 +4960,8 @@ def nce_layer(input,
49574960
:type num_classes: int
49584961
:param act: Activation, default is Sigmoid.
49594962
:type act: BaseActivation
4963+
:param param_attr: The Parameter Attribute|list.
4964+
:type param_attr: ParameterAttribute
49604965
:param num_neg_samples: number of negative samples. Default is 10.
49614966
:type num_neg_samples: int
49624967
:param neg_distribution: The distribution for generating the random negative labels.
@@ -4972,9 +4977,20 @@ def nce_layer(input,
49724977
"""
49734978
if isinstance(input, LayerOutput):
49744979
input = [input]
4980+
assert not isinstance(param_attr, collections.Sequence)
4981+
param_attr = [param_attr]
4982+
else:
4983+
if isinstance(param_attr, collections.Sequence):
4984+
assert len(input) == len(param_attr)
4985+
else:
4986+
param_attr = [copy.deepcopy(param_attr) for _ in range(len(input))]
4987+
49754988
assert isinstance(input, collections.Sequence)
4989+
49764990
assert isinstance(label, LayerOutput)
49774991
assert label.layer_type == LayerType.DATA
4992+
if num_classes is None:
4993+
num_classes = label.size
49784994
if neg_distribution is not None:
49794995
assert isinstance(neg_distribution, collections.Sequence)
49804996
assert len(neg_distribution) == num_classes
@@ -4984,9 +5000,9 @@ def nce_layer(input,
49845000

49855001
ipts_for_layer = []
49865002
parents = []
4987-
for each_input in input:
5003+
for each_input, attr in zip(input, param_attr):
49885004
assert isinstance(each_input, LayerOutput)
4989-
ipts_for_layer.append(each_input.name)
5005+
ipts_for_layer.append(Input(each_input.name, **attr.attr))
49905006
parents.append(each_input)
49915007
ipts_for_layer.append(label.name)
49925008
parents.append(label)

python/paddle/trainer_config_helpers/tests/configs/protostr/test_cost_layers.protostr

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,22 @@ layers {
215215
}
216216
coeff: 1.0
217217
}
218+
layers {
219+
name: "__nce_layer_0__"
220+
type: "nce"
221+
size: 1
222+
active_type: "sigmoid"
223+
inputs {
224+
input_layer_name: "__fc_layer_0__"
225+
input_parameter_name: "___nce_layer_0__.w0"
226+
}
227+
inputs {
228+
input_layer_name: "labels"
229+
}
230+
bias_parameter_name: "___nce_layer_0__.wbias"
231+
num_classes: 5000
232+
num_neg_samples: 10
233+
}
218234
parameters {
219235
name: "___fc_layer_0__.w0"
220236
size: 800
@@ -245,6 +261,26 @@ parameters {
245261
initial_strategy: 0
246262
initial_smart: true
247263
}
264+
parameters {
265+
name: "___nce_layer_0__.w0"
266+
size: 20000
267+
initial_mean: 0.0
268+
initial_std: 0.0141421356237
269+
dims: 5000
270+
dims: 4
271+
initial_strategy: 0
272+
initial_smart: true
273+
}
274+
parameters {
275+
name: "___nce_layer_0__.wbias"
276+
size: 5000
277+
initial_mean: 0.0
278+
initial_std: 0.0
279+
dims: 1
280+
dims: 5000
281+
initial_strategy: 0
282+
initial_smart: false
283+
}
248284
input_layer_names: "input"
249285
input_layer_names: "labels"
250286
input_layer_names: "crf_label"
@@ -267,6 +303,7 @@ output_layer_names: "__cross_entropy_with_selfnorm_0__"
267303
output_layer_names: "__huber_cost_0__"
268304
output_layer_names: "__multi_binary_label_cross_entropy_0__"
269305
output_layer_names: "__sum_cost_0__"
306+
output_layer_names: "__nce_layer_0__"
270307
sub_models {
271308
name: "root"
272309
layer_names: "input"
@@ -292,6 +329,7 @@ sub_models {
292329
layer_names: "__huber_cost_0__"
293330
layer_names: "__multi_binary_label_cross_entropy_0__"
294331
layer_names: "__sum_cost_0__"
332+
layer_names: "__nce_layer_0__"
295333
input_layer_names: "input"
296334
input_layer_names: "labels"
297335
input_layer_names: "crf_label"
@@ -314,6 +352,7 @@ sub_models {
314352
output_layer_names: "__huber_cost_0__"
315353
output_layer_names: "__multi_binary_label_cross_entropy_0__"
316354
output_layer_names: "__sum_cost_0__"
355+
output_layer_names: "__nce_layer_0__"
317356
is_recurrent_layer_group: false
318357
}
319358

python/paddle/trainer_config_helpers/tests/configs/protostr/test_cost_layers_with_weight.protostr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ layers {
6060
}
6161
coeff: 1.0
6262
}
63+
layers {
64+
name: "multi_class_label"
65+
type: "data"
66+
size: 500
67+
active_type: ""
68+
}
69+
layers {
70+
name: "__nce_layer_0__"
71+
type: "nce"
72+
size: 1
73+
active_type: "sigmoid"
74+
inputs {
75+
input_layer_name: "__fc_layer_0__"
76+
input_parameter_name: "___nce_layer_0__.w0"
77+
}
78+
inputs {
79+
input_layer_name: "multi_class_label"
80+
}
81+
inputs {
82+
input_layer_name: "weight"
83+
}
84+
bias_parameter_name: "___nce_layer_0__.wbias"
85+
num_classes: 500
86+
num_neg_samples: 10
87+
}
6388
parameters {
6489
name: "___fc_layer_0__.w0"
6590
size: 3000
@@ -80,9 +105,30 @@ parameters {
80105
initial_strategy: 0
81106
initial_smart: false
82107
}
108+
parameters {
109+
name: "___nce_layer_0__.w0"
110+
size: 5000
111+
initial_mean: 0.0
112+
initial_std: 0.04472135955
113+
dims: 500
114+
dims: 10
115+
initial_strategy: 0
116+
initial_smart: true
117+
}
118+
parameters {
119+
name: "___nce_layer_0__.wbias"
120+
size: 500
121+
initial_mean: 0.0
122+
initial_std: 0.0
123+
dims: 1
124+
dims: 500
125+
initial_strategy: 0
126+
initial_smart: false
127+
}
83128
input_layer_names: "input"
84129
input_layer_names: "label"
85130
input_layer_names: "weight"
131+
input_layer_names: "multi_class_label"
86132
output_layer_names: "__cost_0__"
87133
output_layer_names: "__mse_cost_0__"
88134
evaluators {
@@ -100,9 +146,12 @@ sub_models {
100146
layer_names: "__fc_layer_0__"
101147
layer_names: "__cost_0__"
102148
layer_names: "__mse_cost_0__"
149+
layer_names: "multi_class_label"
150+
layer_names: "__nce_layer_0__"
103151
input_layer_names: "input"
104152
input_layer_names: "label"
105153
input_layer_names: "weight"
154+
input_layer_names: "multi_class_label"
106155
output_layer_names: "__cost_0__"
107156
output_layer_names: "__mse_cost_0__"
108157
evaluator_names: "classification_error_evaluator"

python/paddle/trainer_config_helpers/tests/configs/test_cost_layers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@
4040
name='huber_label', size=1)),
4141
multi_binary_label_cross_entropy(
4242
input=probs, label=xe_label),
43-
sum_cost(input=hidden))
43+
sum_cost(input=hidden),
44+
nce_layer(
45+
input=hidden, label=labels))

python/paddle/trainer_config_helpers/tests/configs/test_cost_layers_with_weight.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
classification_cost(
1212
input=fc, label=lbl, weight=wt),
1313
mse_cost(
14-
input=fc, label=lbl, weight=wt))
14+
input=fc, label=lbl, weight=wt),
15+
nce_layer(
16+
input=fc,
17+
label=data_layer(
18+
name='multi_class_label', size=500),
19+
weight=wt))

0 commit comments

Comments
 (0)