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

Enhanced Docstrings and Examples in /ops/function.py, /ops/math.py and /ops/nn.py. #736

Merged
merged 5 commits into from
Aug 17, 2023
Merged

Conversation

Faisal-Alsrheed
Copy link
Contributor

@Faisal-Alsrheed Faisal-Alsrheed commented Aug 16, 2023

Main Changes:

  1. Improved docstrings and added examples for segment_sum, segment_max, and rsqrt functions in keras_core/ops/math.py.
  2. Added docstring for network_nodes in keras_core/ops/function.py
  3. Enhanced the docstring for multi_hot function in keras_core/ops/nn.py.

My target to add very easy to understand examples.

@Faisal-Alsrheed Faisal-Alsrheed marked this pull request as ready for review August 16, 2023 15:55
@Faisal-Alsrheed
Copy link
Contributor Author

Thank you for your patience, I'm starting to understand GitHub slowly :)

Copy link
Contributor

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@@ -180,7 +180,7 @@ def map_graph(inputs, outputs):

Returns:
A tuple `(nodes, nodes_by_depth, operations, operations_by_depth)`.
- nodes: list of Node instances.
- network_nodes:dict mapping the unique node keys to the Node instances
Copy link
Contributor

Choose a reason for hiding this comment

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

Add space after :

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

>>> segment_ids = keras_core.ops.convert_to_tensor([0, 1, 0, 1, 0, 1])
>>> segment_sum(data, segment_ids)
array([9 12], shape=(2,), dtype=int32)
>>> data = keras_core.ops.convert_to_tensor([1,2,10,20,100,200])
Copy link
Contributor

Choose a reason for hiding this comment

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

What was the issue with the initial code example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue was that the example does not work with the JAX backend.
`ValueError: Argument 'num_segments' must be set when using the JAX backend. Received: num_segments=None
So, I have changed it to an easier-to-understand example, and now it is working with all backends.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good -- please make sure to format the code in the black style, e.g. spaces after ,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, Now i am using pip install black and this tool https://black.vercel.app/

>>> segment_max(data, segment_ids)
array([9 12], shape=(2,), dtype=int32)
"""
>>> data = keras_core.ops.convert_to_tensor([1,2,10,20,100,200])
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise here?

Copy link
Contributor Author

@Faisal-Alsrheed Faisal-Alsrheed Aug 16, 2023

Choose a reason for hiding this comment

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

Firstly, the problem was that the example didn't work with the JAX backend. It raised a ValueError: Argument 'num_segments' must be set when using the JAX backend. Received: num_segments=None.

The second issue was that the output is incorrect; it seemed as if it was copied from segment_sum.

To address these, I've modified the example to make it more understandable, and it now functions correctly with all backends."

>>> rsqrt(x)
"""

>>> data = keras_core.ops.convert_to_tensor([1.0,10.0,100.0])
Copy link
Contributor

Choose a reason for hiding this comment

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

Use spaces are ,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


>>> data = keras_core.ops.convert_to_tensor([1.0,10.0,100.0])
>>> keras_core.ops.rsqrt(data)
Array([1. , 0.31622776, 0.1 ], dtype=float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use array (lowercase) and trim whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -1501,6 +1501,31 @@ def compute_output_spec(self, inputs):
]
)
def multi_hot(inputs, num_tokens, axis=-1, dtype=None):
"""
Encodes integer labels as multi-hot vectors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this to the line above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@Faisal-Alsrheed
Copy link
Contributor Author

I will be more than happy to write, enhance, and check all the docstrings. Additionally, I'll add examples that are easy to understand and compatible with all backends. Please let me know if that sounds good. @fchollet

>>> segment_ids = keras_core.ops.convert_to_tensor([0, 1, 0, 1, 0, 1])
>>> segment_sum(data, segment_ids)
array([9 12], shape=(2,), dtype=int32)
>>> data = keras_core.ops.convert_to_tensor([1,2,10,20,100,200])
Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good -- please make sure to format the code in the black style, e.g. spaces after ,

keras_core/ops/math.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@Faisal-Alsrheed Faisal-Alsrheed left a comment

Choose a reason for hiding this comment

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

Done

>>> segment_ids = keras_core.ops.convert_to_tensor([0, 1, 0, 1, 0, 1])
>>> segment_sum(data, segment_ids)
array([9 12], shape=(2,), dtype=int32)
>>> data = keras_core.ops.convert_to_tensor([1,2,10,20,100,200])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, Now i am using pip install black and this tool https://black.vercel.app/

keras_core/ops/math.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@Faisal-Alsrheed Faisal-Alsrheed left a comment

Choose a reason for hiding this comment

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

Done

@Faisal-Alsrheed
Copy link
Contributor Author

Great now I am using black python library and this website https://black.vercel.app/ for formatting.

@Faisal-Alsrheed
Copy link
Contributor Author

My next step will be to check all the functions docstring and then discuss with you a standard way to write them even the order including examples and reference and so on

@fchollet
Copy link
Contributor

Great now I am using black python library and this website https://black.vercel.app/ for formatting.

Once you have installed black you can just run it from the terminal, e.g. black file/path/

Copy link
Contributor

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

>>> segment_ids = keras_core.ops.convert_to_tensor([0, 1, 0, 1, 0, 1])
>>> segment_max(data, segment_ids)
array([9 12], shape=(2,), dtype=int32)
>>> data = keras_core.ops.convert_to_tensor([1, 2, 10, 20, 100, 200])
Copy link
Contributor

Choose a reason for hiding this comment

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

Unindent: the code example should have the indent as the rest of the docstring

@@ -688,16 +690,17 @@ def stft(
class Rsqrt(Operation):
"""Computes reciprocal of square root of x element-wise.

Args:
x: input tensor
Args:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same, fix indent

Copy link
Contributor

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@fchollet fchollet merged commit 0dda60f into keras-team:main Aug 17, 2023
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.

2 participants