Skip to content

Commit

Permalink
set mixedlayer output size according to input operator (#414)
Browse files Browse the repository at this point in the history
* set mixedlayer output size according to input operator
* change from num_channel to num_channels for conv_operator (the old one is
really misleading because all the others are num_channels)

* also changed the arg name in projections.py
  • Loading branch information
Haonan authored and emailweixu committed Nov 10, 2016
1 parent 5ccf84a commit 8d4c453
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
43 changes: 23 additions & 20 deletions python/paddle/trainer_config_helpers/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,17 @@ def __enter__(self):
def __exit__(self, *args, **kwargs):
del args, kwargs # unused parameter to suppress warning
assert len(self.inputs) != 0
MixedLayer(
ml = MixedLayer(
name=self.name,
size=self.size,
active_type=self.activation.name,
bias=ParamAttr.to_bias(self.bias_attr),
inputs=self.inputs,
**ExtraLayerAttribute.to_kwargs(self.layer_attr)
)
# update the size which might be computed inside MixedLayer
# according to the operator's output size
self.size = ml.config.size


@wrap_name_default("mixed")
Expand Down Expand Up @@ -2045,7 +2048,7 @@ def __reduce_concat_type__(a, b):

if layer_type == LayerType.CONCAT_LAYER:
assert not bias_attr

Layer(
name=name, type=layer_type,
inputs=[x.name for x in input] if is_concat_layer else input,
Expand Down Expand Up @@ -2623,7 +2626,7 @@ def out_prod_layer(input1, input2, name=None, layer_attr=None):
assert isinstance(input1, LayerOutput)
assert isinstance(input2, LayerOutput)
Layer(name=name,
type="out_prod",
type=LayerType.OUT_PROD_LAYER,
inputs=[input1.name, input2.name],
**ExtraLayerAttribute.to_kwargs(layer_attr))
return LayerOutput(name=name,
Expand Down Expand Up @@ -2790,7 +2793,7 @@ def __real_step__(*args):

def __cost_input__(input, label, weight=None):
"""
inputs and parents for cost layers.
inputs and parents for cost layers.
"""
ipts = [Input(input.name), Input(label.name)]
parents = [input, label]
Expand All @@ -2799,7 +2802,7 @@ def __cost_input__(input, label, weight=None):
ipts.append(Input(weight.name))
parents.append(weight)
return ipts, parents


@wrap_name_default()
@layer_support()
Expand Down Expand Up @@ -2884,7 +2887,7 @@ def __add_evaluator__(e):


def conv_operator(img, filter, filter_size, num_filters,
num_channel=None, stride=1, padding=0,
num_channels=None, stride=1, padding=0,
filter_size_y=None, stride_y=None, padding_y=None):
"""
Different from img_conv_layer, conv_op is an Operator, which can be used
Expand Down Expand Up @@ -2914,8 +2917,8 @@ def conv_operator(img, filter, filter_size, num_filters,
:type filter_size_y: int
:param num_filters: channel of output data.
:type num_filters: int
:param num_channel: channel of input data.
:type num_channel: int
:param num_channels: channel of input data.
:type num_channels: int
:param stride: The x dimension of the stride.
:type stride: int
:param stride_y: The y dimension of the stride.
Expand All @@ -2934,19 +2937,19 @@ def conv_operator(img, filter, filter_size, num_filters,
if padding_y is None:
padding_y = padding

if num_channel is None:
num_channel = img.num_filters
if num_channels is None:
num_channels = img.num_filters

assert isinstance(filter, LayerOutput)
if filter.size is not None:
filter.size = filter_size * filter_size_y * num_filters * num_channel
filter.size = filter_size * filter_size_y * num_filters * num_channels

op = ConvOperator(input_layer_names=[img.name, filter.name],
num_filters=num_filters,
conv_conf=Conv(filter_size=filter_size,
padding=padding,
stride=stride,
channels=num_channel,
channels=num_channels,
filter_size_y=filter_size_y,
padding_y=padding_y,
stride_y=stride_y,
Expand Down Expand Up @@ -2986,8 +2989,8 @@ def conv_projection(input, filter_size, num_filters,
:type filter_size_y: int
:param num_filters: channel of output data.
:type num_filters: int
:param num_channel: channel of input data.
:type num_channel: int
:param num_channels: channel of input data.
:type num_channels: int
:param stride: The x dimension of the stride.
:type stride: int
:param stride_y: The y dimension of the stride.
Expand Down Expand Up @@ -3478,15 +3481,15 @@ def maxout_layer(input,
- Input: output of a conv layer.
- Output: feature map size same as input. Channel is (input channel) / groups.
So groups should be larger than 1, and the num of channels should be able
So groups should be larger than 1, and the num of channels should be able
to devided by groups.
Please refer to Paper:
Please refer to Paper:
- Maxout Networks: http://www.jmlr.org/proceedings/papers/v28/goodfellow13.pdf
- Multi-digit Number Recognition from Street View \
Imagery using Deep Convolutional Neural Networks: \
https://arxiv.org/pdf/1312.6082v4.pdf
The simple usage is:
.. code-block:: python
Expand Down Expand Up @@ -3731,9 +3734,9 @@ def nce_layer(input, label, num_classes, weight=None,
:param weight: weight layer, can be None(default)
:type weight: LayerOutput
:param num_classes: number of classes.
:type num_classes: int
:type num_classes: int
:param num_neg_samples: number of negative samples. Default is 10.
:type num_neg_samples: int
:type num_neg_samples: int
:param neg_distribution: The distribution for generating the random negative labels.
A uniform distribution will be used if not provided.
If not None, its length must be equal to num_classes.
Expand All @@ -3754,7 +3757,7 @@ def nce_layer(input, label, num_classes, weight=None,
assert isinstance(neg_distribution, collections.Sequence)
assert len(neg_distribution) == num_classes
assert sum(neg_distribution) == 1

ipts_for_layer = []
parents = []
for each_input in input:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

with mixed_layer() as m7:
m7 += conv_operator(img=img, filter=flt, num_filters=64,
num_channel=1, filter_size=3)
num_channels=1, filter_size=3)

end = mixed_layer(input=[full_matrix_projection(input=m5),
trans_full_matrix_projection(input=m6),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
filter=y1,
filter_size=1,
num_filters=5,
num_channel=5,
num_channels=5,
stride=1)])

assert z1.size > 0

y2 = fc_layer(input=y, size=15)

cos1 = cos_sim(a=x1, b=y1)
Expand Down

0 comments on commit 8d4c453

Please sign in to comment.