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

[Keras Ops] Add Histogram Operation #20316

Merged
merged 3 commits into from
Oct 2, 2024

Conversation

DavidLandup0
Copy link
Contributor

@DavidLandup0 DavidLandup0 commented Oct 2, 2024

This PR adds the keras.ops.histogram() operation to the source implementations and API.

Example Usage

JAX

import numpy as np
import os

os.environ["KERAS_BACKEND"] = "jax"
import keras

input_tensor = np.array([1, 2, 3, 4, 5])
keras.ops.histogram(input_tensor)

Outputs:

(Array([1., 0., 1., 0., 0., 1., 0., 1., 0., 1.], dtype=float32),
 Array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ], dtype=float32))

TensorFlow

import numpy as np
import os

os.environ["KERAS_BACKEND"] = "tensorflow"
import keras

input_tensor = np.array([1, 2, 3, 4, 5])
keras.ops.histogram(input_tensor)

Outputs:

(<tf.Tensor: shape=(10,), dtype=int64, numpy=array([1, 0, 1, 0, 0, 1, 0, 1, 0, 1])>,
 <tf.Tensor: shape=(11,), dtype=float64, numpy=array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ])>)

NumPy

import numpy as np
import os

os.environ["KERAS_BACKEND"] = "numpy"
import keras

input_tensor = np.array([1, 2, 3, 4, 5])
keras.ops.histogram(input_tensor)

Outputs:

(array([1, 0, 1, 0, 0, 1, 0, 1, 0, 1]),
 array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ]))

Torch

import numpy as np
import os

os.environ["KERAS_BACKEND"] = "torch"
import keras

input_tensor = np.array([1., 2., 3., 4., 5.])
keras.ops.histogram(input_tensor)

Outputs:

(tensor([1., 0., 1., 0., 0., 1., 0., 1., 0., 1.]),
 tensor([1.0000, 1.4000, 1.8000, 2.2000, 2.6000, 3.0000, 3.4000, 3.8000, 4.2000,
         4.6000, 5.0000]))

Notes

  • The TensorFlow tf.histogram_fixed_width() and tf.histogram_fixed_width_bins() methods had different numerical behavior from PyTorch, NumPy and JAX, so the TensorFlow math module has its own implementation.

Copy link
Collaborator

@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.

Awesome -- thank you for the contribution!

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Oct 2, 2024
@codecov-commenter
Copy link

codecov-commenter commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 72.88136% with 16 lines in your changes missing coverage. Please review.

Project coverage is 78.82%. Comparing base (084b7e1) to head (3a7b031).

Files with missing lines Patch % Lines
keras/src/ops/math.py 60.52% 11 Missing and 4 partials ⚠️
keras/api/_tf_keras/keras/ops/__init__.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #20316      +/-   ##
==========================================
- Coverage   78.83%   78.82%   -0.01%     
==========================================
  Files         512      512              
  Lines       48995    49054      +59     
  Branches     9021     9035      +14     
==========================================
+ Hits        38624    38667      +43     
- Misses       8509     8521      +12     
- Partials     1862     1866       +4     
Flag Coverage Δ
keras 78.68% <72.88%> (-0.01%) ⬇️
keras-jax 62.27% <47.45%> (-0.02%) ⬇️
keras-numpy 57.42% <49.15%> (-0.01%) ⬇️
keras-tensorflow 63.56% <64.40%> (+<0.01%) ⬆️
keras-torch 62.26% <49.15%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@google-ml-butler google-ml-butler bot removed the ready to pull Ready to be merged into the codebase label Oct 2, 2024
@fchollet fchollet merged commit 08910e2 into keras-team:master Oct 2, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants