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

copy.deepcopy leads to incorrect gradient #555

Open
jbellevi opened this issue Jun 29, 2020 · 1 comment
Open

copy.deepcopy leads to incorrect gradient #555

jbellevi opened this issue Jun 29, 2020 · 1 comment

Comments

@jbellevi
Copy link

Consider the following :

from autograd import grad 
import autograd.numpy as np
import copy


def summation_no_copy(x):
    sum = 0

    for i in range(1, 3):
        sum += i * np.sum(x)
    return sum


def summation_copy(x):
    sum = 0

    for i in range(1, 3):
        sum += i * np.sum(copy.deepcopy(x))

    return sum


x = np.array([1, 2, 3, 4, 3.5, 920, 0])

grad_copy = grad(summation_copy)
grad_no_copy = grad(summation_no_copy)

print(f'with deepcopy: {grad_copy(x)}')
print(f'without deepcopy: {grad_no_copy(x)}')

The output is:

with deepcopy: [1. 1. 1. 1. 1. 1. 1.]
without deepcopy: [3. 3. 3. 3. 3. 3. 3.]

I'm not sure whether this is a bug, or if this simply isn't supported, but it seems like the gradient calculation is broken after the first deepcopy (i.e. doesn't consider any of the following deepcopies). I need the gradient of a more complicated function which requires the use of deepcopy: how can I go about getting it?

@tylerflex
Copy link

@jbellevi curious if this ever got resolved? I think I encountered the same 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

3 participants
@jbellevi @tylerflex and others