Skip to content

Better example code for the "Taking advantage of graphs" section#2424

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

Better example code for the "Taking advantage of graphs" section#2424
HsienChing wants to merge 1 commit intotensorflow:masterfrom
HsienChing:patch-4

Conversation

@HsienChing
Copy link
Copy Markdown

Better example code for the "Taking advantage of graphs" section

Position:
"Taking advantage of graphs" section

Link:
https://www.tensorflow.org/guide/intro_to_graphs#taking_advantage_of_graphs

Condition:
The example code is OK, no bug. I am talking about a more detailed consideration about the tutorial.

In the last line of the example code, the assert keyword is used to check the values between a_regular_function() and tf.function(a_regular_function).

assert(orig_value == tf_function_value)

However, when I run the code, nothing happens. As I learned the usage of assert, I understood that "noting happens" indicates "orig_value == tf_function_value".

NOTE: In Python, the assert keyword is a debugging tool used to verify that a condition is true. If the condition evaluates to False, the program immediately raises an AssertionError and terminates, helping developers catch bugs during the development and testing phases.

Suggestion:
In my opinion, a tutorial should give readers an "explicit" guide instead of an "implicit" guide. Using print is a better choice than using assert.

When using print, a True or False will be sent according to the conditional expression orig_value == tf_function_value, and readers will understand the result explicitly and immediately.

print(orig_value == tf_function_value)

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

# Better example code for the "Taking advantage of graphs" section

**Position:**  
"Taking advantage of graphs" section

**Link:**  
https://www.tensorflow.org/guide/intro_to_graphs#taking_advantage_of_graphs

**Condition:**  
The example code is OK, no bug. I am talking about a more detailed consideration about the tutorial.

In the last line of the example code, the `assert` keyword is used to check the values between `a_regular_function()` and `tf.function(a_regular_function)`.
```Python
assert(orig_value == tf_function_value)
```
However, when I run the code, nothing happens. As I learned the usage of `assert`, I understood that "noting happens" indicates "orig_value == tf_function_value". 

NOTE: In Python, the [`assert`](https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-assert_stmt) keyword is a debugging tool used to verify that a condition is true. If the condition evaluates to False, the program immediately raises an AssertionError and terminates, helping developers catch bugs during the development and testing phases.

**Suggestion:**  
In my opinion, a tutorial should give readers an "explicit" guide instead of an "implicit" guide. Using `print` is a better choice than using `assert`.

When using `print`, a `True` or `False` will be sent according to the conditional expression `orig_value == tf_function_value`, and readers will understand the result explicitly and immediately.
```Python
print(orig_value == tf_function_value)
```

REF: https://github.com/HsienChing/ML_DL_project_State_Estimation_of_Li-ion_Batteries/blob/main/other/Issues_in_TensorFlow_official_doc_Introduction_to_graphs_and_tf.function.ipynb
@HsienChing HsienChing requested a review from a team as a code owner April 9, 2026 07:38
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 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-4

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