-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add one_hot operator. #7819
Conversation
depth)).astype('float32') | ||
|
||
for i in xrange(np.product(x.shape)): | ||
out[i, x[i]] = 1.0 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
paddle/operators/one_hot_op.cc
Outdated
"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", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Resolves #7740