Skip to content

Better example code for the subsection "1. Replaced a variable with a…#2422

Open
HsienChing wants to merge 1 commit intotensorflow:masterfrom
HsienChing:patch-3
Open

Better example code for the subsection "1. Replaced a variable with a…#2422
HsienChing wants to merge 1 commit intotensorflow:masterfrom
HsienChing:patch-3

Conversation

@HsienChing
Copy link
Copy Markdown

Better example code for the subsection "1. Replaced a variable with a tensor"

Position: "1. Replaced a variable with a tensor" subsection

Link: https://www.tensorflow.org/guide/autodiff#1_replaced_a_variable_with_a_tensor

Condition: The original example is very concise. However, it took me a considerable amount of time to modify the code and test it to understand the example fully.

I think there are two points that confused me.
Point 1: The derivative of y = x + 1 is a constant scalar 1. This will cause no value change as the number of epoch increases. We can't obviously see the change in the value. Point 2: The comment directly gives the correct code # This should be x.assign_add(1) without the reason.

Suggestion: I have modified the example code as shown below.

  1. The formula has been changed from y = x + 1 to y = x**2. The derivative is now 2x, and we can obviously see the value change corresponding to epoch.
  2. A comment "The tf.Variable has been inadvertently replaced with a tf.Tensor." has been added.
  3. A epoch indicator is added. print("epoch:", epoch)
  4. The number of epoch is changed from 2 to 3.

REF: https://github.com/HsienChing/ML_DL_project_State_Estimation_of_Li-ion_Batteries/blob/main/other/Issues_in_TensorFlow_official_doc_Introduction_to_gradients_and_automatic_differentiation.ipynb

… tensor"

# Better example code for the subsection "1. Replaced a variable with a tensor"

**Position:** "1. Replaced a variable with a tensor" subsection

**Link:** https://www.tensorflow.org/guide/autodiff#1_replaced_a_variable_with_a_tensor

**Condition:** The original example is very concise. However, it took me a considerable amount of time to modify the code and test it to understand the example fully. 

I think there are two points that confused me.  
Point 1: The derivative of `y = x + 1` is a constant scalar `1`. This will cause no value change as the number of epoch increases. We can't obviously see the change in the value.  
Point 2: The comment directly gives the correct code `# This should be x.assign_add(1)` without the reason.

**Suggestion:** I have modified the example code as shown below.  
1. The formula has been changed from `y = x + 1` to `y = x**2`. The derivative is now `2x`, and we can obviously see the value change corresponding to epoch. 
2. A comment "The `tf.Variable` has been inadvertently replaced with a `tf.Tensor`." has been added.
3. A epoch indicator is added. `print("epoch:", epoch)`
4. The number of epoch is changed from 2 to 3.

REF: https://github.com/HsienChing/ML_DL_project_State_Estimation_of_Li-ion_Batteries/blob/main/other/Issues_in_TensorFlow_official_doc_Introduction_to_gradients_and_automatic_differentiation.ipynb
@HsienChing HsienChing requested a review from a team as a code owner April 8, 2026 09:34
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Preview

Preview and run these notebook edits with Google Colab: Rendered notebook diffs available on ReviewNB.com.

Format and style

Use the TensorFlow docs notebook tools to format for consistent source diffs and lint for style:
$ python3 -m pip install -U --user git+https://github.com/tensorflow/docs

$ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb
$ python3 -m tensorflow_docs.tools.nblint --arg=repo:tensorflow/docs notebook.ipynb
If commits are added to the pull request, synchronize your local branch: git pull origin patch-3

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.

1 participant