Merge pull request #1400 from jacquesqiao/topology
add Topology to handle actions on networkavx_docs
commit
c444708a03
@ -0,0 +1,83 @@
|
||||
# Copyright PaddlePaddle contributors. All Rights Reserved
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import unittest
|
||||
import paddle.v2.layer as layer
|
||||
import paddle.v2.topology as topology
|
||||
import paddle.v2.data_type as data_type
|
||||
import paddle.trainer_config_helpers as conf_helps
|
||||
|
||||
|
||||
class TestTopology(unittest.TestCase):
|
||||
def test_data_type(self):
|
||||
pixel = layer.data(name='pixel', type=data_type.dense_vector(784))
|
||||
label = layer.data(name='label', type=data_type.integer_value(10))
|
||||
hidden = layer.fc(input=pixel,
|
||||
size=100,
|
||||
act=conf_helps.SigmoidActivation())
|
||||
inference = layer.fc(input=hidden,
|
||||
size=10,
|
||||
act=conf_helps.SoftmaxActivation())
|
||||
cost = layer.classification_cost(input=inference, label=label)
|
||||
topo = topology.Topology(cost)
|
||||
data_types = topo.data_type()
|
||||
self.assertEqual(len(data_types), 2)
|
||||
pixel_data_type = filter(lambda type: type[0] == "pixel", data_types)
|
||||
self.assertEqual(len(pixel_data_type), 1)
|
||||
pixel_data_type = pixel_data_type[0]
|
||||
self.assertEqual(pixel_data_type[1].type, data_type.DataType.Dense)
|
||||
self.assertEqual(pixel_data_type[1].dim, 784)
|
||||
|
||||
label_data_type = filter(lambda type: type[0] == "label", data_types)
|
||||
self.assertEqual(len(label_data_type), 1)
|
||||
label_data_type = label_data_type[0]
|
||||
self.assertEqual(label_data_type[1].type, data_type.DataType.Index)
|
||||
self.assertEqual(label_data_type[1].dim, 10)
|
||||
|
||||
def test_get_layer(self):
|
||||
pixel = layer.data(name='pixel', type=data_type.dense_vector(784))
|
||||
label = layer.data(name='label', type=data_type.integer_value(10))
|
||||
hidden = layer.fc(input=pixel,
|
||||
size=100,
|
||||
act=conf_helps.SigmoidActivation())
|
||||
inference = layer.fc(input=hidden,
|
||||
size=10,
|
||||
act=conf_helps.SoftmaxActivation())
|
||||
cost = layer.classification_cost(input=inference, label=label)
|
||||
topo = topology.Topology(cost)
|
||||
pixel_layer = topo.get_layer("pixel")
|
||||
label_layer = topo.get_layer("label")
|
||||
self.assertEqual(pixel_layer, pixel)
|
||||
self.assertEqual(label_layer, label)
|
||||
|
||||
def test_parse(self):
|
||||
pixel = layer.data(name='pixel', type=data_type.dense_vector(784))
|
||||
label = layer.data(name='label', type=data_type.integer_value(10))
|
||||
hidden = layer.fc(input=pixel,
|
||||
size=100,
|
||||
act=conf_helps.SigmoidActivation())
|
||||
inference = layer.fc(input=hidden,
|
||||
size=10,
|
||||
act=conf_helps.SoftmaxActivation())
|
||||
maxid = layer.max_id(input=inference)
|
||||
cost1 = layer.classification_cost(input=inference, label=label)
|
||||
cost2 = layer.cross_entropy_cost(input=inference, label=label)
|
||||
|
||||
topology.Topology(cost2).proto()
|
||||
topology.Topology([cost1]).proto()
|
||||
topology.Topology([cost1, cost2]).proto()
|
||||
topology.Topology([inference, maxid]).proto()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,95 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import collections
|
||||
|
||||
from paddle.proto.ModelConfig_pb2 import ModelConfig
|
||||
|
||||
import layer as v2_layer
|
||||
|
||||
__all__ = ['Topology']
|
||||
|
||||
|
||||
class Topology(object):
|
||||
"""
|
||||
Topology is used to store the information about all layers
|
||||
and network configs.
|
||||
"""
|
||||
|
||||
def __init__(self, layers):
|
||||
if not isinstance(layers, collections.Sequence):
|
||||
__check_layer_type__(layers)
|
||||
layers = [layers]
|
||||
for layer in layers:
|
||||
__check_layer_type__(layer)
|
||||
self.layers = layers
|
||||
self.__model_config__ = v2_layer.parse_network(*layers)
|
||||
assert isinstance(self.__model_config__, ModelConfig)
|
||||
|
||||
def proto(self):
|
||||
return self.__model_config__
|
||||
|
||||
def get_layer(self, name):
|
||||
"""
|
||||
get v2.Layer Class instance by layer name
|
||||
:param name:
|
||||
:return:
|
||||
"""
|
||||
result_layer = []
|
||||
|
||||
def find_layer_by_name(layer, layer_name):
|
||||
if len(result_layer) == 1:
|
||||
return
|
||||
elif layer.name == layer_name:
|
||||
result_layer.append(layer)
|
||||
else:
|
||||
for parent_layer in layer.__parent_layers__.values():
|
||||
find_layer_by_name(parent_layer, layer_name)
|
||||
|
||||
for layer in self.layers:
|
||||
find_layer_by_name(layer, name)
|
||||
|
||||
assert len(result_layer) == 1
|
||||
return result_layer[0]
|
||||
|
||||
def data_layers(self):
|
||||
"""
|
||||
get all data layer
|
||||
:return:
|
||||
"""
|
||||
data_layers = set()
|
||||
|
||||
def find_data_layer(layer):
|
||||
if isinstance(layer, v2_layer.DataLayerV2):
|
||||
data_layers.add(layer)
|
||||
for parent_layer in layer.__parent_layers__.values():
|
||||
find_data_layer(parent_layer)
|
||||
|
||||
for layer in self.layers:
|
||||
find_data_layer(layer)
|
||||
|
||||
return data_layers
|
||||
|
||||
def data_type(self):
|
||||
"""
|
||||
get data_type from proto, such as:
|
||||
[('image', dense_vector(768)), ('label', integer_value(10))]
|
||||
"""
|
||||
return [(data_layer.name, data_layer.type)
|
||||
for data_layer in self.data_layers()]
|
||||
|
||||
|
||||
def __check_layer_type__(layer):
|
||||
if not isinstance(layer, v2_layer.LayerV2):
|
||||
raise ValueError('layer should have type paddle.layer.Layer')
|
Loading…
Reference in new issue