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

return value of backward in class Sum #2

Closed
ifoyooo opened this issue Oct 3, 2021 · 2 comments
Closed

return value of backward in class Sum #2

ifoyooo opened this issue Oct 3, 2021 · 2 comments

Comments

@ifoyooo
Copy link

ifoyooo commented Oct 3, 2021

From my perspective, the shape of 'grad_output' should be broadcasted into a_shape.
Although it can still pass task3_1, I'm not sure of its accuracy in more complex situations.

            @staticmethod
            def backward(ctx, grad_output):
                a_shape, dim = ctx.saved_values
              
                if dim is None:
                    out = grad_output.zeros(a_shape)
                    out._tensor._storage[:] = grad_output[0]
                    return out
                else:
                # START Code Update
                    return grad_output #should be replaced by add_zip(grad_output,zeros(a_shape)) #
                # END Code Update
@ifoyooo
Copy link
Author

ifoyooo commented Oct 3, 2021

I just realized the case 2 in function 'expand' is exactly to tackle the problem aroused by the inconsistence between origin shape and gradient shape which is caused by the return value.

        # Case 2: Backward is a smaller than self. Broadcast up.
        true_shape = TensorData.shape_broadcast(self.shape, other.shape)
        buf = self.zeros(true_shape)
        self.backend._id_map(other, out=buf)
        if self.shape == true_shape:
            return buf

I'm sorry to trouble you.

@ifoyooo ifoyooo closed this as completed Oct 3, 2021
@srush
Copy link
Contributor

srush commented Oct 4, 2021

Thanks. Yes, this is not well documented. Just kind of snuck it in the fix this issue.

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

No branches or pull requests

2 participants