Skip to content

Fit a sine curve in the gradients tutorial - #1146

Open
mvanhorn wants to merge 2 commits into
tensorflow:masterfrom
mvanhorn:fix/488-gradient-regression
Open

mvanhorn wants to merge 2 commits into
tensorflow:masterfrom
mvanhorn:fix/488-gradient-regression

Conversation

@mvanhorn

Copy link
Copy Markdown

Background

Readers can compute the gradient of a circuit expectation, but the tutorial ends before showing how to use that gradient to fit a parameter to data.

Summary of the fix

The new example reuses the tutorial’s circuit and custom differentiator to fit a phase offset to a sine curve shifted by 0.2. It uses tf.GradientTape to differentiate the mean squared error with respect to the offset, then applies 30 gradient descent updates starting from zero. It prints the initial gradient, the loss before and after training, and the fitted offset.

Verification

repo_lint ran twice, with no result shown for either run. ./scripts/format_check.sh ran, with no result shown. ./scripts/lint_all.sh ran, with no result shown. No test was run with the quantum change reverted, because no test was named as covering the reported bug, so none is shown to catch this bug.

Closes #488

The new example reuses the tutorial’s circuit and custom differentiator
to fit a phase offset to a sine curve shifted by 0.2. It uses
`tf.GradientTape` to differentiate the mean squared error with respect
to the offset, then applies 30 gradient descent updates starting from
zero. It prints the initial gradient, the loss before and after
training, and the fitted offset.

Refs tensorflow#488
@review-notebook-app

Copy link
Copy Markdown

Review Notebooks

Notebooks changed:

Review visual diffs and comment on any notebook cell. All comments sync back to this PR.


Powered by ReviewNB

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds a new section to the gradients tutorial notebook demonstrating how to fit a sine curve using gradient descent with a custom differentiator. The feedback suggests optimizing performance by pre-converting the Cirq objects and inputs to TensorFlow tensors outside of the training loop to avoid repeated serialization overhead on every step.

Comment thread docs/tutorials/gradients.ipynb Resolved
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.

How to do regression using gradient?

1 participant