|
| 1 | +# Copyright PaddlePaddle contributors. All Rights Reserved |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import difflib |
| 15 | +import unittest |
| 16 | + |
| 17 | +import paddle.trainer_config_helpers as conf_helps |
| 18 | +import paddle.v2.activation as activation |
| 19 | +import paddle.v2.attr as attr |
| 20 | +import paddle.v2.data_type as data_type |
| 21 | +import paddle.v2.layer as layer |
| 22 | +from paddle.trainer_config_helpers.config_parser_utils import \ |
| 23 | + parse_network_config as parse_network |
| 24 | + |
| 25 | +pixel = layer.data(name='pixel', type=data_type.dense_vector(784)) |
| 26 | +label = layer.data(name='label', type=data_type.integer_value(10)) |
| 27 | +weight = layer.data(name='weight', type=data_type.dense_vector(10)) |
| 28 | +score = layer.data(name='score', type=data_type.dense_vector(1)) |
| 29 | +hidden = layer.fc(input=pixel, |
| 30 | + size=100, |
| 31 | + act=activation.Sigmoid(), |
| 32 | + param_attr=attr.Param(name='hidden')) |
| 33 | +inference = layer.fc(input=hidden, size=10, act=activation.Softmax()) |
| 34 | + |
| 35 | + |
| 36 | +class CostLayerTest(unittest.TestCase): |
| 37 | + def test_cost_layer(self): |
| 38 | + cost1 = layer.classification_cost(input=inference, label=label) |
| 39 | + cost2 = layer.classification_cost( |
| 40 | + input=inference, label=label, weight=weight) |
| 41 | + cost3 = layer.cross_entropy_cost(input=inference, label=label) |
| 42 | + cost4 = layer.cross_entropy_with_selfnorm_cost( |
| 43 | + input=inference, label=label) |
| 44 | + cost5 = layer.regression_cost(input=inference, label=label) |
| 45 | + cost6 = layer.regression_cost( |
| 46 | + input=inference, label=label, weight=weight) |
| 47 | + cost7 = layer.multi_binary_label_cross_entropy_cost( |
| 48 | + input=inference, label=label) |
| 49 | + cost8 = layer.rank_cost(left=score, right=score, label=score) |
| 50 | + cost9 = layer.lambda_cost(input=inference, score=score) |
| 51 | + cost10 = layer.sum_cost(input=inference) |
| 52 | + cost11 = layer.huber_cost(input=score, label=label) |
| 53 | + |
| 54 | + print dir(layer) |
| 55 | + layer.parse_network(cost1, cost2) |
| 56 | + print dir(layer) |
| 57 | + #print layer.parse_network(cost3, cost4) |
| 58 | + #print layer.parse_network(cost5, cost6) |
| 59 | + #print layer.parse_network(cost7, cost8, cost9, cost10, cost11) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + unittest.main() |
0 commit comments