-
Notifications
You must be signed in to change notification settings - Fork 118
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
added erf op to math.py #908
Conversation
There was a problem hiding this 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! 👍
keras_core/backend/torch/math.py
Outdated
|
||
def erf(x): | ||
if not isinstance(x, torch.Tensor): | ||
x = torch.tensor(x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just do x = convert_to_tensor(x)
unconditionally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
keras_core/ops/math.py
Outdated
"""Computes the error function of x element-wise. | ||
|
||
Args: | ||
input_tensor: A tensor of type `float32` or `float64`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can have more types, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edited the comments based on that
keras_core/ops/math.py
Outdated
|
||
@keras_core_export("keras_core.ops.erf") | ||
def erf(x): | ||
"""Functional interface to the `Erf` operation.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the docstring should be, not the op above, since this is the public symbol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relocated it!
keras_core/ops/math.py
Outdated
>>> y_large = Erf()(x_large) | ||
""" | ||
|
||
def __init__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed it
keras_core/ops/math.py
Outdated
def compute_output_spec(self, input_tensor): | ||
return KerasTensor(shape=input_tensor.shape, dtype=input_tensor.dtype) | ||
|
||
def call(self, input_tensor): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just x
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with x
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates!
keras_core/ops/math.py
Outdated
return KerasTensor(shape=x.shape, dtype=x.dtype) | ||
|
||
def call(self, x): | ||
return backend.erf(x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to call backend.math
. Tests are failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implemented it
keras_core/ops/math.py
Outdated
|
||
Examples: | ||
|
||
# Basic usage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're not printing any outputs, just use a fenced code block for the code example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Hi @fchollet , Thanks so much for the guidance. The Tests are failing because the errors are greater than the tolerance level (1×10^-5) set as you can see below.
Kindly guide me on how to resolve this issue. Thanks & Regards |
You can just lower the precision of this particular check to 1e-4. |
lowered the precision tolerance to 1e-4 for erf function
Hi @fchollet , Thanks for the reply, Lowered the precision to 1e-4 such that it would resolve the issue but the maximum difference is around 0.12 which is way higher than 0.0001 which still leads to failing tests. Kindly advise. |
keras_core/ops/math_test.py
Outdated
|
||
def test_erf_operation_edge_cases(self): | ||
# Test for edge cases | ||
edge_values = np.array([1e10, -1e10, 1e-10, -1e-10], dtype=np.float64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your test values are too large. Try 1e5. This the source of the large discrepancy IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have implemented the changes, but I can see from the tests that it is failing for the below array examples. I wonder if there is anything wrong in the implementation function itself.
- x: array([ 1.128379e+00, -1.128379e+00, 1.273240e-05, -1.273240e-05])
- x: array([-1.128354, -1.123101, -0.950886, 0. , 0.950886, 1.123101, 1.128354])
lowered the test values to 1e5
Keras Core is becoming Keras 3, and we're switching development to the main repository! Please reopen this PR in the keras-team/keras repository. Unfortunately we aren't able to automatically transfer PRs (but we have transferred all issues). |
Hi @fchollet ,
Hope you are doing well. As discussed in issue keras-team/keras#18442, I have raised this PR to work on the ERF function. Kindly review the changes I have currently made. Once approved, I will go ahead with the implementation for JAX, torch, and numpy backends.
Thanks & Regards