|
| 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 unittest |
| 15 | +import paddle.v2.layer as layer |
| 16 | +import paddle.v2.topology as topology |
| 17 | +import paddle.v2.data_type as data_type |
| 18 | +import paddle.trainer_config_helpers as conf_helps |
| 19 | + |
| 20 | + |
| 21 | +class TestTopology(unittest.TestCase): |
| 22 | + def test_data_type(self): |
| 23 | + pixel = layer.data(name='pixel', type=data_type.dense_vector(784)) |
| 24 | + label = layer.data(name='label', type=data_type.integer_value(10)) |
| 25 | + hidden = layer.fc(input=pixel, |
| 26 | + size=100, |
| 27 | + act=conf_helps.SigmoidActivation()) |
| 28 | + inference = layer.fc(input=hidden, |
| 29 | + size=10, |
| 30 | + act=conf_helps.SoftmaxActivation()) |
| 31 | + cost = layer.classification_cost(input=inference, label=label) |
| 32 | + topo = topology.Topology(cost) |
| 33 | + data_types = topo.data_type() |
| 34 | + self.assertEqual(len(data_types), 2) |
| 35 | + pixel_data_type = filter(lambda type: type[0] == "pixel", data_types) |
| 36 | + self.assertEqual(len(pixel_data_type), 1) |
| 37 | + pixel_data_type = pixel_data_type[0] |
| 38 | + self.assertEqual(pixel_data_type[1].type, data_type.DataType.Dense) |
| 39 | + self.assertEqual(pixel_data_type[1].dim, 784) |
| 40 | + |
| 41 | + label_data_type = filter(lambda type: type[0] == "label", data_types) |
| 42 | + self.assertEqual(len(label_data_type), 1) |
| 43 | + label_data_type = label_data_type[0] |
| 44 | + self.assertEqual(label_data_type[1].type, data_type.DataType.Index) |
| 45 | + self.assertEqual(label_data_type[1].dim, 10) |
| 46 | + |
| 47 | + def test_get_layer(self): |
| 48 | + pixel = layer.data(name='pixel', type=data_type.dense_vector(784)) |
| 49 | + label = layer.data(name='label', type=data_type.integer_value(10)) |
| 50 | + hidden = layer.fc(input=pixel, |
| 51 | + size=100, |
| 52 | + act=conf_helps.SigmoidActivation()) |
| 53 | + inference = layer.fc(input=hidden, |
| 54 | + size=10, |
| 55 | + act=conf_helps.SoftmaxActivation()) |
| 56 | + cost = layer.classification_cost(input=inference, label=label) |
| 57 | + topo = topology.Topology(cost) |
| 58 | + pixel_layer = topo.get_layer("pixel") |
| 59 | + label_layer = topo.get_layer("label") |
| 60 | + self.assertEqual(pixel_layer, pixel) |
| 61 | + self.assertEqual(label_layer, label) |
| 62 | + |
| 63 | + def test_parse(self): |
| 64 | + pixel = layer.data(name='pixel', type=data_type.dense_vector(784)) |
| 65 | + label = layer.data(name='label', type=data_type.integer_value(10)) |
| 66 | + hidden = layer.fc(input=pixel, |
| 67 | + size=100, |
| 68 | + act=conf_helps.SigmoidActivation()) |
| 69 | + inference = layer.fc(input=hidden, |
| 70 | + size=10, |
| 71 | + act=conf_helps.SoftmaxActivation()) |
| 72 | + maxid = layer.max_id(input=inference) |
| 73 | + cost1 = layer.classification_cost(input=inference, label=label) |
| 74 | + cost2 = layer.cross_entropy_cost(input=inference, label=label) |
| 75 | + |
| 76 | + topology.Topology(cost2).proto() |
| 77 | + topology.Topology([cost1]).proto() |
| 78 | + topology.Topology([cost1, cost2]).proto() |
| 79 | + topology.Topology([inference, maxid]).proto() |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + unittest.main() |
0 commit comments