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

Add one_hot operator. #7819

Merged
merged 2 commits into from
Jan 26, 2018
Merged

Add one_hot operator. #7819

merged 2 commits into from
Jan 26, 2018

Conversation

pkuyym
Copy link
Contributor

@pkuyym pkuyym commented Jan 24, 2018

Resolves #7740

depth)).astype('float32')

for i in xrange(np.product(x.shape)):
out[i, x[i]] = 1.0
Copy link
Contributor

Choose a reason for hiding this comment

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

for i in xrange(np.product(x.shape)):
     out[i, x[i]] = 1.0

==>

out[range(0,x_lod[0][-1]),x] = 1.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, but I think current implement is ok.

"The tensor consists of one-hot representations of values in X.");
AddAttr<int>("depth",
"An integer to specify the length of one-hot vector.");
AddAttr<int>("dtype",
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we provide a default type over here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done.

PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U,
"Last dimension of Input(X) should be 1.");

int depth = ctx->Attrs().Get<int>("depth");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should enforce that a positive depth is provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done.

import paddle.v2.fluid.core as core


class TestOneHotOp(OpTest):
Copy link
Contributor

Choose a reason for hiding this comment

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

It will also help to have some negative examples, where the depth is less than the index values. We can catch the exception thrown by the executor to make sure that the code gives an error for such faulty cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, have added more unit test.

Copy link
Contributor

@abhinavarora abhinavarora left a comment

Choose a reason for hiding this comment

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

LGTM!

@abhinavarora abhinavarora merged commit 84c12c6 into PaddlePaddle:develop Jan 26, 2018
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.

3 participants