Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/sequence pool #5229

Merged
merged 6 commits into from
Oct 31, 2017
Merged

Conversation

dzhwinter
Copy link
Contributor

fix python layer interface

def sums(input, program=None, init_program=None):
helper = LayerHelper('sum', **locals())
if not isinstance(input, list) and not isinstance(input, tuple):
input = [input]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to do the converting here. Operator's constructor will do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

helper = LayerHelper('sum', **locals())
if not isinstance(input, list) and not isinstance(input, tuple):
input = [input]
out = helper.create_tmp_variable(dtype=input[0].data_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LayerHelper has a member function: input_dtype. It will check whether all the inputs are the same type and then return the type. I think using it here is a better choice.

def input_dtype(self, input_param_name='input'):
        inputs = self.multiple_input(input_param_name)
        dtype = None
        for each in inputs:
            if dtype is None:
                dtype = each.data_type
            elif dtype != each.data_type:
                raise ValueError("Data Type mismatch")
        return dtype

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


def cos_sim(X, Y, program=None, init_program=None):
helper = LayerHelper('cos_sim', **locals())
out = helper.create_tmp_variable(dtype=X.data_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use LayerHelper::input_dtype(). See my above comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

},
outputs={"Out": pre_bias},
attrs={
'context_stride': stride,
'context_start': 0,
'context_length': filter_size
})

pre_act = helper.append_bias_op(pre_bias)
return helper.append_activation(pre_act)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove activation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure sequence conv need a non-linear activation. So I remove it.

Copy link
Collaborator

@JiayiFeng JiayiFeng Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shall retain it. Users can set act as None to disable it.

outputs={"Out": pool_out},
attrs={"strategy": pool_type})
outputs={"Out": [pool_out]},
attrs={"strategy": ENUM_POOL_TYPE[pool_type.upper()]})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this change for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"max", "sqrt" are keywords of Python, I cannot bear our keyword overloading anymore.....

Copy link
Collaborator

@JiayiFeng JiayiFeng Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why ENUM_POOL_TYPE becomes a map.

Copy link
Contributor Author

@dzhwinter dzhwinter Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because attribute of "strategy" needs a fixed enum index, not a string.

    AddAttr<int>(
        "strategy",
        "(int, default AVERAGE) the pooling strategy of SequencePoolOp.")
        .SetDefault(AVERAGE)
        .InEnum({AVERAGE, SUM, SQRT, MAX, LAST, FIRST});

And this attribute will be definitely replaced with another PR, and the sequence_conv attributes, etc. Those tons of code will be rewritten after I finished the book chapter 5.


return helper.append_activation(batch_norm_out)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove batch_norm layer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a mistake remove caused by the conflict. It has been fixed.

@@ -103,22 +103,18 @@ def sequence_conv_pool(input,
filter_size,
pool_size,
pool_stride,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pool_size and pool_stride are also useless. Please remove them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

pool_size=pool_size,
pool_type='max',
pool_stride=pool_stride,
pool_type='sum',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think max is a little more normal than sum, so I think it's fine to keep the default as max.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.
Find a frequent usage -- embedding and then pooling in Sum is equivalent to the FC layer.

Copy link
Collaborator

@JiayiFeng JiayiFeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dzhwinter dzhwinter merged commit 9b70b6a into PaddlePaddle:develop Oct 31, 2017
zchen0211 pushed a commit to zchen0211/Paddle that referenced this pull request Oct 31, 2017
* "modify layers.py"

* "fix pool interface"

* "add export type to layers"

* "fix based on comment"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants