Skip to content

Commit

Permalink
add weight for cost layer interface (PaddlePaddle#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
luotao1 authored and reyoung committed Oct 10, 2016
1 parent 86bb5ef commit 199a6a4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
44 changes: 35 additions & 9 deletions python/paddle/trainer_config_helpers/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2777,29 +2777,49 @@ def __real_step__(*args):

return tmp

def __cost_input__(input, label, weight=None):
"""
inputs and parents for cost layers.
"""
ipts = [Input(input.name), Input(label.name)]
parents = [input, label]
if weight is not None:
assert weight.layer_type == LayerType.DATA
ipts.append(Input(weight.name))
parents.append(weight)
return ipts, parents


@wrap_name_default()
def regression_cost(input, label, cost='square_error', name=None):
def regression_cost(input, label, weight=None, cost='square_error', name=None):
"""
Regression Layer.
TODO(yuyang18): Complete this method.
:param name: layer name.
:type name: basestring
:param input: Network prediction.
:type input: LayerOutput
:param label: Data label.
:type label: LayerOutput
:param weight: The weight affects the cost, namely the scale of cost.
It is an optional argument.
:type weight: LayerOutput
:param cost: Cost method.
:type cost: basestring
:return: LayerOutput object.
:rtype: LayerOutput
"""
Layer(inputs=[Input(input.name), Input(label.name)], type=cost, name=name)
return LayerOutput(
name, LayerType.COST, parents=[input, label]
)
ipts, parents = __cost_input__(input, label, weight)

Layer(inputs=ipts, type=cost, name=name)
return LayerOutput(name, LayerType.COST, parents=parents)


@wrap_name_default("cost")
@layer_support()
def classification_cost(input, label, name=None,
def classification_cost(input, label, weight=None, name=None,
cost="multi-class-cross-entropy",
evaluator=classification_error_evaluator,
layer_attr=None):
Expand All @@ -2812,6 +2832,9 @@ def classification_cost(input, label, name=None,
:type input: LayerOutput
:param label: label layer name. data_layer often.
:type label: LayerOutput
:param weight: The weight affects the cost, namely the scale of cost.
It is an optional argument.
:type weight: LayerOutput
:param cost: cost method.
:type cost: basestring
:param evaluator: Evaluator method.
Expand All @@ -2823,7 +2846,10 @@ def classification_cost(input, label, name=None,
assert input.layer_type != LayerType.DATA
assert isinstance(input.activation, SoftmaxActivation)
assert label.layer_type == LayerType.DATA
Layer(name=name, type=cost, inputs=[Input(input.name), Input(label.name)],

ipts, parents = __cost_input__(input, label, weight)

Layer(name=name, type=cost, inputs=ipts,
**ExtraLayerAttribute.to_kwargs(layer_attr))

def __add_evaluator__(e):
Expand All @@ -2835,15 +2861,15 @@ def __add_evaluator__(e):
assert isinstance(e.for_classification, bool)
assert e.for_classification

e(name=e.__name__, input=input, label=label)
e(name=e.__name__, input=input, label=label, weight=weight)

if not isinstance(evaluator, collections.Sequence):
evaluator = [evaluator]

for each_evaluator in evaluator:
__add_evaluator__(each_evaluator)

return LayerOutput(name, LayerType.COST, parents=[input, label])
return LayerOutput(name, LayerType.COST, parents=parents)


def conv_operator(img, filter, filter_size, num_filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ a5d9259ff1fd7ca23d0ef090052cb1f2 last_first_seq.protostr
5913f87b39cee3b2701fa158270aca26 projections.protostr
6b39e34beea8dfb782bee9bd3dea9eb5 simple_rnn_layers.protostr
0fc1409600f1a3301da994ab9d28b0bf test_cost_layers.protostr
6cd5f28a3416344f20120698470e0a4c test_cost_layers_with_weight.protostr
144bc6d3a509de74115fa623741797ed test_expand_layer.protostr
2378518bdb71e8c6e888b1842923df58 test_fc.protostr
8bb44e1e5072d0c261572307e7672bda test_grumemory_layer.protostr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ configs=(test_fc layer_activations projections test_print_layer
test_sequence_pooling test_lstmemory_layer test_grumemory_layer
last_first_seq test_expand_layer test_ntm_layers test_hsigmoid
img_layers util_layers simple_rnn_layers unused_layers test_cost_layers
test_rnn_group)
test_cost_layers_with_weight test_rnn_group)


for conf in ${configs[*]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from paddle.trainer_config_helpers import *

settings(
learning_rate=1e-4,
batch_size=1000
)

data = data_layer(name='input', size=300)
lbl = data_layer(name='label', size=1)
wt = data_layer(name='weight', size=1)
fc = fc_layer(input=data, size=10, act=SoftmaxActivation())

outputs(classification_cost(input=fc, label=lbl, weight=wt),
regression_cost(input=fc, label=lbl, weight=wt))

0 comments on commit 199a6a4

Please sign in to comment.