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

Complete missing docstrings in keras-core/ops/core.py #494

Merged
merged 7 commits into from
Jul 15, 2023
Merged
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
61 changes: 57 additions & 4 deletions keras_core/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,30 +273,83 @@ def stop_gradient(variable):

@keras_core_export("keras_core.ops.shape")
def shape(x):
"""Gets the shape of the tensor input."""
"""Gets the shape of the tensor input.

Args:
x: A tensor. This function will try to access the `shape` attribute of
the input tensor.

Returns:
A tuple of integers or None values, indicating the shape of the input
tensor.

Example:
>>> x = keras_core.zeros((8, 12))
>>> keras_core.ops.shape(x)
(8, 12)

"""

if any_symbolic_tensors((x,)):
return x.shape
return backend.core.shape(x)


@keras_core_export("keras_core.ops.cast")
def cast(x, dtype):
"""Cast a tensor to the desired dtype."""
"""Cast a tensor to the desired dtype.

Args:
x: A tensor or variable.
dtype: The target type.

Returns:
A tensor of the specified `dtype`.

Example:
>>> x = keras_core.ops.arange(4)
>>> keras_core.ops.cast(x, "float32")

"""
dtype = backend.standardize_dtype(dtype)

if any_symbolic_tensors((x,)):
return backend.KerasTensor(shape=x.shape, dtype=dtype)
return backend.core.cast(x, dtype)


@keras_core_export("keras_core.ops.convert_to_tensor")
def convert_to_tensor(x, dtype=None):
"""Convert a NumPy array to a tensor."""
"""Convert a NumPy array to a tensor.

Args:
x: A NumPy array.
dtype: The target type.

Returns:
A tensor of the specified `dtype`.

Example:
>>> x = np.array([1, 2, 3])
>>> keras_core.ops.convert_to_tensor(x)

"""

return backend.convert_to_tensor(x, dtype=dtype)


@keras_core_export("keras_core.ops.convert_to_numpy")
def convert_to_numpy(x):
"""Convert a tensor to a NumPy array."""
"""Convert a tensor to a NumPy array.

Args:
x: A tensor.

Returns:
A NumPy array.

"""

if any_symbolic_tensors((x,)):
# This will raise a `ValueError` defined in the `KerasTensor` class. We
# trigger it rather than duplicate it here.
Expand Down