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 gradients with ignore_idx in softmax_with_cross_entropy #28622

Merged
merged 3 commits into from
Nov 16, 2020

Conversation

guoshengCS
Copy link
Contributor

@guoshengCS guoshengCS commented Nov 14, 2020

PR types

Bug fixes

PR changes

OPs

Describe

Fix gradient calculation with ignore_idx in softmax_with_cross_entropy.

Example code for reproducing error gradients on elements with ignore_idx:

import paddle
import numpy as np

paddle.seed(123)
np.random.seed(123)

class Net(paddle.nn.Layer):
    def __init__(self):
        super(Net, self).__init__()
        self.embedder = paddle.nn.Embedding(100, 64)
        self.linear = paddle.nn.Linear(64, 5)

    def forward(self, x, y):
        x = self.embedder(x)
        self.logits = logits = self.linear(x)
        print(y)
        loss = paddle.nn.functional.softmax_with_cross_entropy(logits, y, ignore_index=1)
        loss = paddle.mean(loss)
        return loss

x_data = np.random.randint(0, 5, (4,)).astype("int64")
x = paddle.to_tensor(x_data)
y_data = np.random.randint(0, 5, (4, 1))
y_data[0, 0] = 1  # ignore_idx
y = paddle.to_tensor(y_data)

net = Net()
loss = net(x, y)
loss.backward()
print(net.logits.grad)

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot-old
Copy link

paddle-bot-old bot commented Nov 14, 2020

✅ This PR's description meets the template requirements!
Please wait for other CI results.

Remove softmax_with_cross_entropy from op_threshold_white_list.
test=develop
@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Nov 14, 2020
@PaddlePaddle PaddlePaddle unlocked this conversation Nov 14, 2020
Copy link
Contributor

@Xreki Xreki left a comment

Choose a reason for hiding this comment

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

LGTM for the change of max_relative_error

@guoshengCS guoshengCS merged commit 110febd into PaddlePaddle:develop Nov 16, 2020
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