Skip to content

Commit 32d1dbe

Browse files
Add docstrings in numpy (#617)
* Add docstrings in numpy * passed review comments
1 parent 9a8da03 commit 32d1dbe

File tree

1 file changed

+300
-0
lines changed

1 file changed

+300
-0
lines changed

keras_core/ops/numpy.py

+300
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,38 @@ def append(
586586
x2,
587587
axis=None,
588588
):
589+
"""Append tensor `x2` to the end of tensor `x1`.
590+
591+
Args:
592+
x1: First input tensor.
593+
x2: Second input tensor.
594+
axis: Axis along which tensor `x2` is appended to tensor `x1`.
595+
If `None`, both tensors are flattened before use.
596+
597+
Returns:
598+
A tensor with the values of `x2` appended to `x1`.
599+
600+
Examples:
601+
602+
>>> x1 = keras_core.ops.convert_to_tensor([1, 2, 3])
603+
>>> x2 = keras_core.ops.convert_to_tensor([[4, 5, 6], [7, 8, 9]])
604+
>>> keras_core.ops.append(x1, x2)
605+
array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
606+
607+
When `axis` is specified, `x1` and `x2` must have compatible shapes.
608+
>>> x1 = keras_core.ops.convert_to_tensor([[1, 2, 3], [4, 5, 6]])
609+
>>> x2 = keras_core.ops.convert_to_tensor([[7, 8, 9]])
610+
>>> keras_core.ops.append(x1, x2, axis=0)
611+
array([[1, 2, 3],
612+
[4, 5, 6],
613+
[7, 8, 9]], dtype=int32)
614+
>>> x3 = keras_core.ops.convert_to_tensor([7, 8, 9])
615+
>>> keras_core.ops.append(x1, x3, axis=0)
616+
Traceback (most recent call last):
617+
...
618+
TypeError: Cannot concatenate arrays with different numbers of
619+
dimensions: got (2, 3), (3,).
620+
"""
589621
if any_symbolic_tensors((x1, x2)):
590622
return Append(axis=axis).symbolic_call(x1, x2)
591623
return backend.numpy.append(x1, x2, axis=axis)
@@ -604,6 +636,51 @@ def compute_output_spec(self, start, stop=None, step=1, dtype=None):
604636

605637
@keras_core_export(["keras_core.ops.arange", "keras_core.ops.numpy.arange"])
606638
def arange(start, stop=None, step=1, dtype=None):
639+
"""Return evenly spaced values within a given interval.
640+
641+
`arange` can be called with a varying number of positional arguments:
642+
* `arange(stop)`: Values are generated within the half-open interval
643+
`[0, stop)` (in other words, the interval including start but excluding
644+
stop).
645+
* `arange(start, stop)`: Values are generated within the half-open interval
646+
`[start, stop)`.
647+
* `arange(start, stop, step)`: Values are generated within the half-open
648+
interval `[start, stop)`, with spacing between values given by step.
649+
650+
Args:
651+
start: Integer or real, representing the start of the interval. The
652+
interval includes this value.
653+
stop: Integer or real, representing the end of the interval. The
654+
interval does not include this value, except in some cases where
655+
`step` is not an integer and floating point round-off affects the
656+
lenght of `out`. Defaults to `None`.
657+
step: Integer or real, represent the spacing between values. For any
658+
output `out`, this is the distance between two adjacent values,
659+
`out[i+1] - out[i]`. The default step size is 1. If `step` is
660+
specified as a position argument, `start` must also be given.
661+
dtype: The type of the output array. If `dtype` is not given, infer the
662+
data type from the other input arguments.
663+
664+
Returns:
665+
Tensor of evenly spaced values.
666+
For floating point arguments, the length of the result is
667+
`ceil((stop - start)/step)`. Because of floating point overflow, this
668+
rule may result in the last element of out being greater than stop.
669+
670+
Examples:
671+
672+
>>> keras_core.ops.arange(3)
673+
array([0, 1, 2], dtype=int32)
674+
675+
>>> keras_core.ops.arange(3.0)
676+
array([0., 1., 2.], dtype=float32)
677+
678+
>>> keras_core.ops.arange(3, 7)
679+
array([3, 4, 5, 6], dtype=int32)
680+
681+
>>> keras_core.ops.arange(3, 7, 2)
682+
array([3, 5], dtype=int32)
683+
"""
607684
return backend.numpy.arange(start, stop, step=step, dtype=dtype)
608685

609686

@@ -617,6 +694,20 @@ def compute_output_spec(self, x):
617694

618695
@keras_core_export(["keras_core.ops.arccos", "keras_core.ops.numpy.arccos"])
619696
def arccos(x):
697+
"""Trigonometric inverse cosine, element-wise. The inverse of `cos` so that,
698+
if `y = cos(x)`, then `x = arccos(y)`.
699+
700+
Arg:
701+
x: Input tensor.
702+
703+
Returns:
704+
Tensor of the angle of the ray intersecting the unit circle at the given
705+
x-coordinate in radians [0, pi].
706+
707+
Example:
708+
>>> x = keras_core.ops.convert_to_tensor([1, -1])
709+
array([0., 3.1415927], dtype=float32)
710+
"""
620711
if any_symbolic_tensors((x,)):
621712
return Arccos().symbolic_call(x)
622713
return backend.numpy.arccos(x)
@@ -632,6 +723,20 @@ def compute_output_spec(self, x):
632723

633724
@keras_core_export(["keras_core.ops.arcsin", "keras_core.ops.numpy.arcsin"])
634725
def arcsin(x):
726+
"""Inverse sine, element-wise.
727+
728+
Arg:
729+
x: Input tensor.
730+
731+
Returns:
732+
Tensor of the inverse sine of each element in `x`, in radians and in
733+
the closed interval `[-pi/2, pi/2]`.
734+
735+
Example:
736+
>>> x = keras_core.ops.convert_to_tensor([1, -1, 0])
737+
>>> keras_core.ops.arcsin(x)
738+
array([ 1.5707964, -1.5707964, 0.], dtype=float32)
739+
"""
635740
if any_symbolic_tensors((x,)):
636741
return Arcsin().symbolic_call(x)
637742
return backend.numpy.arcsin(x)
@@ -647,6 +752,20 @@ def compute_output_spec(self, x):
647752

648753
@keras_core_export(["keras_core.ops.arctan", "keras_core.ops.numpy.arctan"])
649754
def arctan(x):
755+
"""Trigonometric inverse tangent, element-wise.
756+
757+
Arg:
758+
x: Input tensor.
759+
760+
Returns:
761+
Tensor of the inverse tangent of each element in `x`, in the interval
762+
`[-pi/2, pi/2]`.
763+
764+
Example:
765+
>>> x = keras_core.ops.convert_to_tensor([0, 1])
766+
>>> keras_core.ops.arctan(x)
767+
array([0., 0.7853982], dtype=float32)
768+
"""
650769
if any_symbolic_tensors((x,)):
651770
return Arctan().symbolic_call(x)
652771
return backend.numpy.arctan(x)
@@ -665,6 +784,43 @@ def compute_output_spec(self, x1, x2):
665784

666785
@keras_core_export(["keras_core.ops.arctan2", "keras_core.ops.numpy.arctan2"])
667786
def arctan2(x1, x2):
787+
"""Element-wise arc tangent of `x1/x2` choosing the quadrant correctly.
788+
789+
The quadrant (i.e., branch) is chosen so that `arctan2(x1, x2)` is the
790+
signed angle in radians between the ray ending at the origin and passing
791+
through the point `(1, 0)`, and the ray ending at the origin and passing
792+
through the point `(x2, x1)`. (Note the role reversal: the "y-coordinate"
793+
is the first function parameter, the "x-coordinate" is the second.) By IEEE
794+
convention, this function is defined for `x2 = +/-0` and for either or both
795+
of `x1` and `x2` `= +/-inf`.
796+
797+
Args:
798+
x1: First input tensor.
799+
x2: Second input tensor.
800+
801+
Returns:
802+
Tensor of angles in radians, in the range `[-pi, pi]`.
803+
804+
Examples:
805+
Consider four points in different quadrants:
806+
>>> x = keras_core.ops.convert_to_tensor([-1, +1, +1, -1])
807+
>>> y = keras_core.ops.convert_to_tensor([-1, -1, +1, +1])
808+
>>> keras_core.ops.arctan2(y, x) * 180 / numpy.pi
809+
array([-135., -45., 45., 135.], dtype=float32)
810+
811+
Note the order of the parameters. `arctan2` is defined also when x2=0 and
812+
at several other points, obtaining values in the range `[-pi, pi]`:
813+
>>> keras_core.ops.arctan2(
814+
... keras_core.ops.array([1., -1.]),
815+
... keras_core.ops.array([0., 0.]),
816+
... )
817+
array([ 1.5707964, -1.5707964], dtype=float32)
818+
>>> keras_core.ops.arctan2(
819+
... keras_core.ops.array([0., 0., numpy.inf]),
820+
... keras_core.ops.array([+0., -0., numpy.inf]),
821+
... )
822+
array([0., 3.1415925, 0.7853982], dtype=float32)
823+
"""
668824
if any_symbolic_tensors((x1, x2)):
669825
return Arctan2().symbolic_call(x1, x2)
670826
return backend.numpy.arctan2(x1, x2)
@@ -688,6 +844,29 @@ def compute_output_spec(self, x):
688844

689845
@keras_core_export(["keras_core.ops.argmax", "keras_core.ops.numpy.argmax"])
690846
def argmax(x, axis=None):
847+
"""Returns the indices of the maximum values along an axis.
848+
849+
Args:
850+
x: Input tensor.
851+
axis: By default, the index is into the flattened tensor, otherwise
852+
along the specified axis.
853+
854+
Returns:
855+
Tensor of indices. It has the same shape as `x`, with the dimension
856+
along `axis` removed.
857+
858+
Example:
859+
>>> x = keras_core.ops.arange(6).reshape(2, 3) + 10
860+
>>> x
861+
array([[10, 11, 12],
862+
[13, 14, 15]], dtype=int32)
863+
>>> keras_core.ops.argmax(x)
864+
array(5, dtype=int32)
865+
>>> keras_core.ops.argmax(x, axis=0)
866+
array([1, 1, 1], dtype=int32)
867+
>>> keras_core.ops.argmax(x, axis=1)
868+
array([2, 2], dtype=int32)
869+
"""
691870
if any_symbolic_tensors((x,)):
692871
return Argmax(axis=axis).symbolic_call(x)
693872
return backend.numpy.argmax(x, axis=axis)
@@ -711,6 +890,29 @@ def compute_output_spec(self, x):
711890

712891
@keras_core_export(["keras_core.ops.argmin", "keras_core.ops.numpy.argmin"])
713892
def argmin(x, axis=None):
893+
"""Returns the indices of the minium values along an axis.
894+
895+
Args:
896+
x: Input tensor.
897+
axis: By default, the index is into the flattened tensor, otherwise
898+
along the specified axis.
899+
900+
Returns:
901+
Tensor of indices. It has the same shape as `x`, with the dimension
902+
along `axis` removed.
903+
904+
Example:
905+
>>> x = keras_core.ops.arange(6).reshape(2, 3) + 10
906+
>>> x
907+
array([[10, 11, 12],
908+
[13, 14, 15]], dtype=int32)
909+
>>> keras_core.ops.argmin(x)
910+
array(0, dtype=int32)
911+
>>> keras_core.ops.argmin(x, axis=0)
912+
array([0, 0, 0], dtype=int32)
913+
>>> keras_core.ops.argmin(x, axis=1)
914+
array([0, 0], dtype=int32)
915+
"""
714916
if any_symbolic_tensors((x,)):
715917
return Argmin(axis=axis).symbolic_call(x)
716918
return backend.numpy.argmin(x, axis=axis)
@@ -732,6 +934,37 @@ def compute_output_spec(self, x):
732934

733935
@keras_core_export(["keras_core.ops.argsort", "keras_core.ops.numpy.argsort"])
734936
def argsort(x, axis=-1):
937+
"""Returns the indices that would sort an tensor.
938+
939+
Args:
940+
x: Input tensor.
941+
axis: Axis along which to sort. Default is `-1` (the last axis). If
942+
`None`, the flattened tensor is used.
943+
944+
Returns:
945+
Tensor of indices that sort `x` along the specified `axis`.
946+
947+
Examples:
948+
One dimensional array:
949+
>>> x = keras_core.ops.array([3, 1, 2])
950+
>>> keras_core.ops.argsort(x)
951+
array([1, 2, 0], dtype=int32)
952+
953+
Two-dimensional array:
954+
>>> x = keras_core.ops.array([[0, 3], [3, 2], [4, 5]])
955+
>>> x
956+
array([[0, 3],
957+
[3, 2],
958+
[4, 5]], dtype=int32)
959+
>>> keras_core.ops.argsort(x, axis=0)
960+
array([[0, 1],
961+
[1, 0],
962+
[2, 2]], dtype=int32)
963+
>>> keras_core.ops.argsort(x, axis=1)
964+
array([[0, 1],
965+
[1, 0],
966+
[0, 1]], dtype=int32)
967+
"""
735968
if any_symbolic_tensors((x,)):
736969
return Argsort(axis=axis).symbolic_call(x)
737970
return backend.numpy.argsort(x, axis=axis)
@@ -747,6 +980,22 @@ def compute_output_spec(self, x, dtype=None):
747980

748981
@keras_core_export(["keras_core.ops.array", "keras_core.ops.numpy.array"])
749982
def array(x, dtype=None):
983+
"""Create a tensor.
984+
985+
Args:
986+
x: Input tensor.
987+
dtype: The desired data-type for the tensor.
988+
989+
Returns:
990+
A tensor.
991+
992+
Examples:
993+
>>> keras_core.ops.array([1, 2, 3])
994+
array([1, 2, 3], dtype=int32)
995+
996+
>>> keras_core.ops.array([1, 2, 3], dtype="float32")
997+
array([1., 2., 3.], dtype=float32)
998+
"""
750999
if any_symbolic_tensors((x,)):
7511000
return Array().symbolic_call(x, dtype=dtype)
7521001
return backend.numpy.array(x, dtype=dtype)
@@ -800,6 +1049,57 @@ def compute_output_spec(self, x, weights=None):
8001049

8011050
@keras_core_export(["keras_core.ops.average", "keras_core.ops.numpy.average"])
8021051
def average(x, axis=None, weights=None):
1052+
"""Compute the weighted average along the specified axis.
1053+
1054+
Args:
1055+
x: Input tensor.
1056+
axis: Integer along which to average `x`. The default, `axis=None`,
1057+
will average over all of the elements of the input tendor. If axis
1058+
is negative it counts from the last to the first axis.
1059+
weights: Tensor of wieghts associated with the values in `x`. Each
1060+
value in `x` contributes to the average according to its
1061+
associated weight. The weights array can either be 1-D (in which
1062+
case its length must be the size of a along the given axis) or of
1063+
the same shape as `x`. If `weights=None` (default), then all data
1064+
in `x` are assumed to have a weight equal to one.
1065+
1066+
The 1-D calculation is: `avg = sum(a * weights) / sum(weights)`.
1067+
The only constraint on weights is that `sum(weights)` must not be 0.
1068+
1069+
Returns:
1070+
Return the average along the specified axis.
1071+
1072+
Examples:
1073+
>>> data = keras_core.ops.arange(1, 5)
1074+
>>> data
1075+
array([1, 2, 3, 4], dtype=int32)
1076+
>>> keras_core.ops.average(data)
1077+
array(2.5, dtype=float32)
1078+
>>> keras_core.ops.average(
1079+
... keras_core.ops.arange(1, 11),
1080+
... weights=keras_core.ops.arange(10, 0, -1)
1081+
... )
1082+
array(4., dtype=float32)
1083+
1084+
>>> data = keras_core.ops.arange(6).reshape((3, 2))
1085+
>>> data
1086+
array([[0, 1],
1087+
[2, 3],
1088+
[4, 5]], dtype=int32)
1089+
>>> keras_core.ops.average(
1090+
... data,
1091+
... axis=1,
1092+
... weights=keras_core.ops.array([1./4, 3./4])
1093+
... )
1094+
array([0.75, 2.75, 4.75], dtype=float32)
1095+
>>> keras_core.ops.average(
1096+
... data,
1097+
... weights=keras_core.ops.array([1./4, 3./4])
1098+
... )
1099+
Traceback (most recent call last):
1100+
...
1101+
ValueError: Axis must be specified when shapes of a and weights differ.
1102+
"""
8031103
if any_symbolic_tensors((x,)):
8041104
return Average(axis=axis).symbolic_call(x, weights=weights)
8051105
return backend.numpy.average(x, weights=weights, axis=axis)

0 commit comments

Comments
 (0)