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

added segment_mean #21910

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ivy/functional/frontends/tensorflow/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ def tanh(x, name=None):
def rsqrt(x, name=None):
return ivy.reciprocal(ivy.sqrt(x))

@to_ivy_arrays_and_back
def segment_mean(
data, segment_ids, name="segment_mean"
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be name=None I think

):
ivy.utils.assertions.check_equal(
list(segment_ids.shape), [list(data.shape)[0]], as_array=False
)
x = ivy.zeros(tuple([segment_ids[-1] + 1] + (list(data.shape))[1:]))
count = ivy.zeros((segment_ids[-1] + 1,))
for i in range((segment_ids).shape[0]):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you investigate weather this could be done using the ivy functional API instead of for loops. I haven't given it much thought, but can you not use ivy.mean to calculate the mean of each segment?

x[segment_ids[i]] = x[segment_ids[i]] + data[i]
count[segment_ids[i]] += 1
for j in range(segment_ids[-1] + 1):
x[j] = ivy.divide(x[j], count[j])
return x


@to_ivy_arrays_and_back
def nextafter(x1, x2, name=None):
Expand Down
39 changes: 39 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_tensorflow/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,45 @@ def test_tensorflow_rsqrt(
)


# segment_mean
@handle_frontend_test(
fn_tree="tensorflow.math.segment_mean",
dtype_and_data=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("numeric"),
shape = (5, 6),
),
dtype_and_segment=helpers.dtype_and_values(
available_dtypes=["int32", "int64"],
shape = (5, ),
min_value = 0,
max_value = 4,
),
test_with_out=st.just(False),
)
def test_tensorflow_segment_mean(
*,
dtype_and_data,
dtype_and_segment,
frontend,
test_flags,
fn_tree,
backend_fw,
on_device,
):
data_dtype, data = dtype_and_data
segment_dtype, segment_ids = dtype_and_segment
helpers.test_frontend_function(
input_dtypes=data_dtype + segment_dtype
frontend=frontend,
backend_to_test=backend_fw,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
data=data[0],
segment_ids=ivy.sort(segment_ids[0]),
)


# nextafter
@handle_frontend_test(
fn_tree="tensorflow.math.nextafter",
Expand Down
Loading