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

fix torch error for bincount #927

Merged
merged 7 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion keras_core/backend/torch/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ def average(x, axis=None, weights=None):

def bincount(x, weights=None, minlength=0):
x = convert_to_tensor(x, dtype=int)
weights = convert_to_tensor(weights)
if weights is not None:
weights = convert_to_tensor(weights)

return torch.bincount(x, weights, minlength)


Expand Down
1 change: 1 addition & 0 deletions keras_core/layers/preprocessing/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def call(self, inputs):
output_mode=self.output_mode,
depth=len(self.bin_boundaries) + 1,
dtype=self.compute_dtype,
count_weights=None,
backend_module=self.backend,
)
return outputs
Expand Down
4 changes: 2 additions & 2 deletions keras_core/layers/preprocessing/discretization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def test_correctness(self):
layer = layers.Discretization(
bin_boundaries=[0.0, 0.5, 1.0], output_mode="count"
)
output = layer(np.array([[0.1, 0.8, 0.9]]))
output = layer(np.array([0.1, 0.8, 0.9]))
self.assertTrue(backend.is_tensor(output))
self.assertAllClose(output, np.array([[0, 1, 2, 0]]))
divyashreepathihalli marked this conversation as resolved.
Show resolved Hide resolved
self.assertAllClose(output, np.array([0, 1, 2, 0]))

def test_tf_data_compatibility(self):
# With fixed bins
Expand Down
18 changes: 9 additions & 9 deletions keras_core/utils/numerical_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ def encode_categorical_inputs(
)

binary_output = output_mode in ("multi_hot", "one_hot")
bincounts = backend_module.numpy.bincount(
inputs,
weights=count_weights,
minlength=depth,
)
if binary_output:
one_hot_input = backend_module.nn.one_hot(inputs, depth)
bincounts = backend_module.numpy.where(
backend_module.numpy.any(one_hot_input, axis=-2), 1, 0
)
one_hot_input = backend_module.nn.one_hot(inputs, depth)
bincounts = backend_module.numpy.where(
backend_module.numpy.any(one_hot_input, axis=-2), 1, 0
)
else:
bincounts = backend_module.numpy.bincount(
divyashreepathihalli marked this conversation as resolved.
Show resolved Hide resolved
inputs,
minlength=depth,
)
bincounts = backend_module.cast(bincounts, dtype)

return bincounts
Loading