Commit 3c37470
authored
Better example code for the subsection "1. Replaced a variable with a 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.ipynb1 parent 7c36502 commit 3c37470
1 file changed
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
799 | 799 | | |
800 | 800 | | |
801 | 801 | | |
802 | | - | |
| 802 | + | |
803 | 803 | | |
804 | | - | |
| 804 | + | |
805 | 805 | | |
| 806 | + | |
806 | 807 | | |
807 | | - | |
| 808 | + | |
| 809 | + | |
808 | 810 | | |
809 | 811 | | |
810 | 812 | | |
| |||
0 commit comments