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

ti.ad..Tape and kernel.grad gives different gradients. #8119

Closed
IanYangChina opened this issue Jun 1, 2023 · 1 comment
Closed

ti.ad..Tape and kernel.grad gives different gradients. #8119

IanYangChina opened this issue Jun 1, 2023 · 1 comment
Assignees
Labels
question Question on using Taichi

Comments

@IanYangChina
Copy link

Hi,

I have been experimenting with ti.ad.Tape and kernel.grad.
Am I doing it the wrong way or there is a bug?
The following two chunks of codes should give the same results but they don't...

Using with ti.ad.Tape(loss)

ti.init()

max_t = 4
theta = ti.field(dtype=ti.f32, shape=())
loss = ti.field(dtype=ti.f32, shape=())
z = ti.field(dtype=ti.f32, shape=(max_t,))
z_ = ti.field(dtype=ti.f32, shape=())
ti.root.lazy_grad()

@ti.kernel
def compute(t: ti.i32):
    z[t+1] = z[t] + theta[None] * 2


@ti.kernel
def loss_func(t: ti.i32):
    loss[None] = ti.abs(z[t] - z_[None])


theta[None] = 2
z_[None] = 200
z[0] = 1

for k in range(10):
    loss[None] = 0

    with ti.ad.Tape(loss):
        for i in range(max_t-1):
            compute(i)
        loss_func(max_t-1)

    print(f'loss = {loss[None]: 0.3f}, theta.grad = {theta.grad}')
    theta[None] -= 0.2 * theta.grad[None]
    print(f'new theta = {theta[None]: 0.3f}')

Using kernel.grad()

for k in range(10):
    loss[None] = 0
    loss.grad[None] = 1

    for i in range(max_t-1):
        compute(i)
    loss_func(max_t-1)

    loss_func.grad(max_t-1)
    for i in range(max_t-1, -1, -1):
        compute.grad(i)
    print(f'loss = {loss[None]: 0.3f}, theta.grad = {theta.grad}')
    theta[None] -= 0.2 * theta.grad[None]
    print(f'new theta = {theta[None]: 0.3f}')

    theta.grad[None] = 0
    z.grad.fill(0)
    z_.grad[None] = 0

Any thought?

@IanYangChina IanYangChina added the question Question on using Taichi label Jun 1, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Jun 1, 2023
@jim19930609 jim19930609 self-assigned this Jun 2, 2023
@jim19930609 jim19930609 moved this from Untriaged to Todo in Taichi Lang Jun 2, 2023
@IanYangChina
Copy link
Author

Closing issue. Turns out I put the wrong number in the loop in the backward pass. Should be in range(max-2, -1, -1)

@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on using Taichi
Projects
Status: Done
Development

No branches or pull requests

2 participants