absolute
functionkeras.ops.absolute(x)
Compute the absolute value element-wise.
keras.ops.abs
is a shorthand for this function.
Arguments
Returns
An array containing the absolute value of each element in x
.
Example
>>> x = keras.ops.convert_to_tensor([-1.2, 1.2])
>>> keras.ops.absolute(x)
array([1.2, 1.2], dtype=float32)
add
functionkeras.ops.add(x1, x2)
Add arguments element-wise.
Arguments
Returns
The tensor containing the element-wise sum of x1
and x2
.
Examples
>>> x1 = keras.ops.convert_to_tensor([1, 4])
>>> x2 = keras.ops.convert_to_tensor([5, 6])
>>> keras.ops.add(x1, x2)
array([6, 10], dtype=int32)
keras.ops.add
also broadcasts shapes:
>>> x1 = keras.ops.convert_to_tensor(
... [[5, 4],
... [5, 6]]
... )
>>> x2 = keras.ops.convert_to_tensor([5, 6])
>>> keras.ops.add(x1, x2)
array([[10 10]
[10 12]], shape=(2, 2), dtype=int32)
all
functionkeras.ops.all(x, axis=None, keepdims=False)
Test whether all array elements along a given axis evaluate to True
.
Arguments
axis=None
) is to perform a logical AND over all the dimensions
of the input array. axis
may be negative, in which case it counts
for the last to the first axis.True
, axes which are reduced are left in the result as
dimensions with size one. With this option, the result will
broadcast correctly against the input array. Defaults to False
.Returns
The tensor containing the logical AND reduction over the axis
.
Examples
>>> x = keras.ops.convert_to_tensor([True, False])
>>> keras.ops.all(x)
array(False, shape=(), dtype=bool)
>>> x = keras.ops.convert_to_tensor([[True, False], [True, True]])
>>> keras.ops.all(x, axis=0)
array([ True False], shape=(2,), dtype=bool)
keepdims=True
outputs a tensor with dimensions reduced to one.
>>> x = keras.ops.convert_to_tensor([[True, False], [True, True]])
>>> keras.ops.all(x, keepdims=True)
array([[False]], shape=(1, 1), dtype=bool)
amax
functionkeras.ops.amax(x, axis=None, keepdims=False)
Returns the maximum of an array or maximum value along an axis.
Arguments
axis=None
), find the maximum value in all the
dimensions of the input array.True
, axes which are reduced are left in the result as
dimensions that are broadcast to the size of the original
input tensor. Defaults to False
.Returns
An array with the maximum value. If axis=None
, the result is a scalar
value representing the maximum element in the entire array. If axis
is
given, the result is an array with the maximum values along
the specified axis.
Examples
>>> x = keras.ops.convert_to_tensor([[1, 3, 5], [2, 3, 6]])
>>> keras.ops.amax(x)
array(6, dtype=int32)
>>> x = keras.ops.convert_to_tensor([[1, 6, 8], [1, 5, 2]])
>>> keras.ops.amax(x, axis=0)
array([1, 6, 8], dtype=int32)
>>> x = keras.ops.convert_to_tensor([[1, 6, 8], [1, 5, 2]])
>>> keras.ops.amax(x, axis=1, keepdims=True)
array([[8], [5]], dtype=int32)
amin
functionkeras.ops.amin(x, axis=None, keepdims=False)
Returns the minimum of an array or minimum value along an axis.
Arguments
axis=None
), find the minimum value in all the
dimensions of the input array.True
, axes which are reduced are left in the result as
dimensions that are broadcast to the size of the original
input tensor. Defaults to False
.Returns
An array with the minimum value. If axis=None
, the result is a scalar
value representing the minimum element in the entire array. If axis
is
given, the result is an array with the minimum values along
the specified axis.
Examples
>>> x = keras.ops.convert_to_tensor([1, 3, 5, 2, 3, 6])
>>> keras.ops.amin(x)
array(1, dtype=int32)
>>> x = keras.ops.convert_to_tensor([[1, 6, 8], [7, 5, 3]])
>>> keras.ops.amin(x, axis=0)
array([1,5,3], dtype=int32)
>>> x = keras.ops.convert_to_tensor([[1, 6, 8], [7, 5, 3]])
>>> keras.ops.amin(x, axis=1, keepdims=True)
array([[1],[3]], dtype=int32)
any
functionkeras.ops.any(x, axis=None, keepdims=False)
Test whether any array element along a given axis evaluates to True
.
Arguments
axis=None
) is to perform a logical OR over all the dimensions
of the input array. axis
may be negative, in which case it counts
for the last to the first axis.True
, axes which are reduced are left in the result as
dimensions with size one. With this option, the result will
broadcast correctly against the input array. Defaults to False
.Returns
The tensor containing the logical OR reduction over the axis
.
Examples
>>> x = keras.ops.convert_to_tensor([True, False])
>>> keras.ops.any(x)
array(True, shape=(), dtype=bool)
>>> x = keras.ops.convert_to_tensor([[True, False], [True, True]])
>>> keras.ops.any(x, axis=0)
array([ True True], shape=(2,), dtype=bool)
keepdims=True
outputs a tensor with dimensions reduced to one.
>>> x = keras.ops.convert_to_tensor([[True, False], [True, True]])
>>> keras.ops.all(x, keepdims=True)
array([[False]], shape=(1, 1), dtype=bool)
append
functionkeras.ops.append(x1, x2, axis=None)
Append tensor x2
to the end of tensor x1
.
Arguments
x2
is appended to tensor x1
.
If None
, both tensors are flattened before use.Returns
A tensor with the values of x2
appended to x1
.
Examples
>>> x1 = keras.ops.convert_to_tensor([1, 2, 3])
>>> x2 = keras.ops.convert_to_tensor([[4, 5, 6], [7, 8, 9]])
>>> keras.ops.append(x1, x2)
array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
When axis
is specified, x1
and x2
must have compatible shapes.
>>> x1 = keras.ops.convert_to_tensor([[1, 2, 3], [4, 5, 6]])
>>> x2 = keras.ops.convert_to_tensor([[7, 8, 9]])
>>> keras.ops.append(x1, x2, axis=0)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=int32)
>>> x3 = keras.ops.convert_to_tensor([7, 8, 9])
>>> keras.ops.append(x1, x3, axis=0)
Traceback (most recent call last):
...
TypeError: Cannot concatenate arrays with different numbers of
dimensions: got (2, 3), (3,).
arange
functionkeras.ops.arange(start, stop=None, step=1, dtype=None)
Return evenly spaced values within a given interval.
arange
can be called with a varying number of positional arguments:
* arange(stop)
: Values are generated within the half-open interval
[0, stop)
(in other words, the interval including start but excluding
stop).
* arange(start, stop)
: Values are generated within the half-open interval
[start, stop)
.
* arange(start, stop, step)
: Values are generated within the half-open
interval [start, stop)
, with spacing between values given by step.
Arguments
step
is not an integer and floating point round-off affects the
length of out
. Defaults to None
.out
, this is the distance between two adjacent values,
out[i+1] - out[i]
. The default step size is 1. If step
is
specified as a position argument, start
must also be given.dtype
is not given, infer the
data type from the other input arguments.Returns
Tensor of evenly spaced values.
For floating point arguments, the length of the result is
ceil((stop - start)/step)
. Because of floating point overflow, this
rule may result in the last element of out being greater than stop.
Examples
>>> keras.ops.arange(3)
array([0, 1, 2], dtype=int32)
>>> keras.ops.arange(3.0)
array([0., 1., 2.], dtype=float32)
>>> keras.ops.arange(3, 7)
array([3, 4, 5, 6], dtype=int32)
>>> keras.ops.arange(3, 7, 2)
array([3, 5], dtype=int32)
arccos
functionkeras.ops.arccos(x)
Trigonometric inverse cosine, element-wise.
The inverse of cos
so that, if y = cos(x)
, then x = arccos(y)
.
Arguments
Returns
Tensor of the angle of the ray intersecting the unit circle at the given
x-coordinate in radians [0, pi]
.
Example
>>> x = keras.ops.convert_to_tensor([1, -1])
>>> keras.ops.arccos(x)
array([0.0, 3.1415927], dtype=float32)
arccosh
functionkeras.ops.arccosh(x)
Inverse hyperbolic cosine, element-wise.
Arguments
Returns
Output tensor of same shape as x.
Example
>>> x = keras.ops.convert_to_tensor([10, 100])
>>> keras.ops.arccosh(x)
array([2.993223, 5.298292], dtype=float32)
arcsin
functionkeras.ops.arcsin(x)
Inverse sine, element-wise.
Arguments
Returns
Tensor of the inverse sine of each element in x
, in radians and in
the closed interval [-pi/2, pi/2]
.
Example
>>> x = keras.ops.convert_to_tensor([1, -1, 0])
>>> keras.ops.arcsin(x)
array([ 1.5707964, -1.5707964, 0.], dtype=float32)
arcsinh
functionkeras.ops.arcsinh(x)
Inverse hyperbolic sine, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
Example
>>> x = keras.ops.convert_to_tensor([1, -1, 0])
>>> keras.ops.arcsinh(x)
array([0.88137364, -0.88137364, 0.0], dtype=float32)
arctan
functionkeras.ops.arctan(x)
Trigonometric inverse tangent, element-wise.
Arguments
Returns
Tensor of the inverse tangent of each element in x
, in the interval
[-pi/2, pi/2]
.
Example
>>> x = keras.ops.convert_to_tensor([0, 1])
>>> keras.ops.arctan(x)
array([0., 0.7853982], dtype=float32)
arctan2
functionkeras.ops.arctan2(x1, x2)
Element-wise arc tangent of x1/x2
choosing the quadrant correctly.
The quadrant (i.e., branch) is chosen so that arctan2(x1, x2)
is the
signed angle in radians between the ray ending at the origin and passing
through the point (1, 0)
, and the ray ending at the origin and passing
through the point (x2, x1)
. (Note the role reversal: the "y-coordinate"
is the first function parameter, the "x-coordinate" is the second.) By IEEE
convention, this function is defined for x2 = +/-0
and for either or both
of x1
and x2
= +/-inf
.
Arguments
Returns
Tensor of angles in radians, in the range [-pi, pi]
.
Examples
Consider four points in different quadrants:
>>> x = keras.ops.convert_to_tensor([-1, +1, +1, -1])
>>> y = keras.ops.convert_to_tensor([-1, -1, +1, +1])
>>> keras.ops.arctan2(y, x) * 180 / numpy.pi
array([-135., -45., 45., 135.], dtype=float32)
Note the order of the parameters. arctan2
is defined also when x2=0 and
at several other points, obtaining values in the range [-pi, pi]
:
>>> keras.ops.arctan2(
... keras.ops.array([1., -1.]),
... keras.ops.array([0., 0.]),
... )
array([ 1.5707964, -1.5707964], dtype=float32)
>>> keras.ops.arctan2(
... keras.ops.array([0., 0., numpy.inf]),
... keras.ops.array([+0., -0., numpy.inf]),
... )
array([0., 3.1415925, 0.7853982], dtype=float32)
arctanh
functionkeras.ops.arctanh(x)
Inverse hyperbolic tangent, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
argmax
functionkeras.ops.argmax(x, axis=None, keepdims=False)
Returns the indices of the maximum values along an axis.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one. Defaults to False
.Returns
Tensor of indices. It has the same shape as x
, with the dimension
along axis
removed.
Example
>>> x = keras.ops.arange(6).reshape(2, 3) + 10
>>> x
array([[10, 11, 12],
[13, 14, 15]], dtype=int32)
>>> keras.ops.argmax(x)
array(5, dtype=int32)
>>> keras.ops.argmax(x, axis=0)
array([1, 1, 1], dtype=int32)
>>> keras.ops.argmax(x, axis=1)
array([2, 2], dtype=int32)
argmin
functionkeras.ops.argmin(x, axis=None, keepdims=False)
Returns the indices of the minimum values along an axis.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one. Defaults to False
.Returns
Tensor of indices. It has the same shape as x
, with the dimension
along axis
removed.
Example
>>> x = keras.ops.arange(6).reshape(2, 3) + 10
>>> x
array([[10, 11, 12],
[13, 14, 15]], dtype=int32)
>>> keras.ops.argmin(x)
array(0, dtype=int32)
>>> keras.ops.argmin(x, axis=0)
array([0, 0, 0], dtype=int32)
>>> keras.ops.argmin(x, axis=1)
array([0, 0], dtype=int32)
argpartition
functionkeras.ops.argpartition(x, kth, axis=-1)
Performs an indirect partition along the given axis.
It returns an array
of indices of the same shape as x
that index data along the given axis
in partitioned order.
Arguments
None
, the flattened array is used.Returns
Array of indices that partition x
along the specified axis
.
argsort
functionkeras.ops.argsort(x, axis=-1)
Returns the indices that would sort a tensor.
Arguments
-1
(the last axis). If
None
, the flattened tensor is used.Returns
Tensor of indices that sort x
along the specified axis
.
Examples
One dimensional array:
>>> x = keras.ops.array([3, 1, 2])
>>> keras.ops.argsort(x)
array([1, 2, 0], dtype=int32)
Two-dimensional array:
>>> x = keras.ops.array([[0, 3], [3, 2], [4, 5]])
>>> x
array([[0, 3],
[3, 2],
[4, 5]], dtype=int32)
>>> keras.ops.argsort(x, axis=0)
array([[0, 1],
[1, 0],
[2, 2]], dtype=int32)
>>> keras.ops.argsort(x, axis=1)
array([[0, 1],
[1, 0],
[0, 1]], dtype=int32)
array
functionkeras.ops.array(x, dtype=None)
Create a tensor.
Arguments
Returns
A tensor.
Examples
>>> keras.ops.array([1, 2, 3])
array([1, 2, 3], dtype=int32)
>>> keras.ops.array([1, 2, 3], dtype="float32")
array([1., 2., 3.], dtype=float32)
average
functionkeras.ops.average(x, axis=None, weights=None)
Compute the weighted average along the specified axis.
Arguments
x
. The default, axis=None
,
will average over all of the elements of the input tensor. If axis
is negative it counts from the last to the first axis.x
. Each
value in x
contributes to the average according to its
associated weight. The weights array can either be 1-D (in which
case its length must be the size of a along the given axis) or of
the same shape as x
. If weights=None
(default), then all data
in x
are assumed to have a weight equal to one.avg = sum(a * weights) / sum(weights)
.
The only constraint on weights is that sum(weights)
must not be 0.Returns
Return the average along the specified axis.
Examples
>>> data = keras.ops.arange(1, 5)
>>> data
array([1, 2, 3, 4], dtype=int32)
>>> keras.ops.average(data)
array(2.5, dtype=float32)
>>> keras.ops.average(
... keras.ops.arange(1, 11),
... weights=keras.ops.arange(10, 0, -1)
... )
array(4., dtype=float32)
>>> data = keras.ops.arange(6).reshape((3, 2))
>>> data
array([[0, 1],
[2, 3],
[4, 5]], dtype=int32)
>>> keras.ops.average(
... data,
... axis=1,
... weights=keras.ops.array([1./4, 3./4])
... )
array([0.75, 2.75, 4.75], dtype=float32)
>>> keras.ops.average(
... data,
... weights=keras.ops.array([1./4, 3./4])
... )
Traceback (most recent call last):
...
ValueError: Axis must be specified when shapes of a and weights differ.
bincount
functionkeras.ops.bincount(x, weights=None, minlength=0, sparse=False)
Count the number of occurrences of each value in a tensor of integers.
Arguments
x
. The default value is None
.
If specified, x
is weighted by it, i.e. if n = x[i]
,
out[n] += weight[i]
instead of the default behavior out[n] += 1
.max(x) + 1
, each value of the output at an index higher than
max(x)
is set to 0.Returns
1D tensor where each element gives the number of occurrence(s) of its
index value in x. Its length is the maximum between max(x) + 1
and
minlength.
Examples
>>> x = keras.ops.array([1, 2, 2, 3], dtype="uint8")
>>> keras.ops.bincount(x)
array([0, 1, 2, 1], dtype=int32)
>>> weights = x / 2
>>> weights
array([0.5, 1., 1., 1.5], dtype=float64)
>>> keras.ops.bincount(x, weights=weights)
array([0., 0.5, 2., 1.5], dtype=float64)
>>> minlength = (keras.ops.max(x).numpy() + 1) + 2 # 6
>>> keras.ops.bincount(x, minlength=minlength)
array([0, 1, 2, 1, 0, 0], dtype=int32)
bitwise_and
functionkeras.ops.bitwise_and(x, y)
Compute the bit-wise AND of two arrays element-wise.
Computes the bit-wise AND of the underlying binary representation of the
integers in the input arrays. This ufunc implements the C/Python operator
&
.
Arguments
Returns
Result tensor.
bitwise_invert
functionkeras.ops.bitwise_invert(x)
Compute bit-wise inversion, or bit-wise NOT, element-wise.
Computes the bit-wise NOT of the underlying binary representation of the
integers in the input arrays. This ufunc implements the C/Python operator
~
.
Arguments
Returns
Result tensor.
bitwise_left_shift
functionkeras.ops.bitwise_left_shift(x, y)
Shift the bits of an integer to the left.
Bits are shifted to the left by appending y
0s at the right of x
.
Since the internal representation of numbers is in binary format, this
operation is equivalent to multiplying x
by 2**y
.
Arguments
Returns
Result tensor.
bitwise_not
functionkeras.ops.bitwise_not(x)
Compute bit-wise inversion, or bit-wise NOT, element-wise.
Computes the bit-wise NOT of the underlying binary representation of the
integers in the input arrays. This ufunc implements the C/Python operator
~
.
Arguments
Returns
Result tensor.
bitwise_or
functionkeras.ops.bitwise_or(x, y)
Compute the bit-wise OR of two arrays element-wise.
Computes the bit-wise OR of the underlying binary representation of the
integers in the input arrays. This ufunc implements the C/Python operator
|
.
Arguments
Returns
Result tensor.
bitwise_right_shift
functionkeras.ops.bitwise_right_shift(x, y)
Shift the bits of an integer to the right.
Bits are shifted to the right y
. Because the internal representation of
numbers is in binary format, this operation is equivalent to dividing x
by
2**y
.
Arguments
Returns
Result tensor.
bitwise_xor
functionkeras.ops.bitwise_xor(x, y)
Compute the bit-wise XOR of two arrays element-wise.
Computes the bit-wise XOR of the underlying binary representation of the
integers in the input arrays. This ufunc implements the C/Python operator
^
.
Arguments
Returns
Result tensor.
broadcast_to
functionkeras.ops.broadcast_to(x, shape)
Broadcast a tensor to a new shape.
Arguments
i
is
interpreted as (i,)
.Returns
A tensor with the desired shape.
Examples
>>> x = keras.ops.array([1, 2, 3])
>>> keras.ops.broadcast_to(x, (3, 3))
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
ceil
functionkeras.ops.ceil(x)
Return the ceiling of the input, element-wise.
The ceil of the scalar x
is the smallest integer i
, such that
i >= x
.
Arguments
Returns
The ceiling of each element in x
, with float dtype.
clip
functionkeras.ops.clip(x, x_min, x_max)
Clip (limit) the values in a tensor.
Given an interval, values outside the interval are clipped to the
interval edges. For example, if an interval of [0, 1]
is specified,
values smaller than 0 become 0, and values larger than 1 become 1.
Arguments
Returns
The clipped tensor.
concatenate
functionkeras.ops.concatenate(xs, axis=0)
Join a sequence of tensors along an existing axis.
Arguments
0
.Returns
The concatenated tensor.
conj
functionkeras.ops.conj(x)
Shorthand for keras.ops.conjugate
.
conjugate
functionkeras.ops.conjugate(x)
Returns the complex conjugate, element-wise.
The complex conjugate of a complex number is obtained by changing the sign of its imaginary part.
keras.ops.conj
is a shorthand for this function.
Arguments
Returns
The complex conjugate of each element in x
.
copy
functionkeras.ops.copy(x)
Returns a copy of x
.
Arguments
Returns
A copy of x
.
correlate
functionkeras.ops.correlate(x1, x2, mode="valid")
Compute the cross-correlation of two 1-dimensional tensors.
Arguments
valid
, same
or full
.
By default the mode is set to valid
, which returns
an output of length max(M, N) - min(M, N) + 1.
same
returns an output of length max(M, N).
full
mode returns the convolution at each point of
overlap, with an output length of N+M-1Returns
Output tensor, cross-correlation of x1
and x2
.
cos
functionkeras.ops.cos(x)
Cosine, element-wise.
Arguments
Returns
The corresponding cosine values.
cosh
functionkeras.ops.cosh(x)
Hyperbolic cosine, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
count_nonzero
functionkeras.ops.count_nonzero(x, axis=None)
Counts the number of non-zero values in x
along the given axis
.
If no axis is specified then all non-zeros in the tensor are counted.
Arguments
None
.Returns
int or tensor of ints.
Examples
>>> x = keras.ops.array([[0, 1, 7, 0], [3, 0, 2, 19]])
>>> keras.ops.count_nonzero(x)
5
>>> keras.ops.count_nonzero(x, axis=0)
array([1, 1, 2, 1], dtype=int64)
>>> keras.ops.count_nonzero(x, axis=1)
array([2, 3], dtype=int64)
cross
functionkeras.ops.cross(x1, x2, axisa=-1, axisb=-1, axisc=-1, axis=None)
Returns the cross product of two (arrays of) vectors.
The cross product of x1
and x2
in R^3 is a vector
perpendicular to both x1
and x2
. If x1
and x2
are arrays of
vectors, the vectors are defined by the last axis of x1
and x2
by default, and these axes can have dimensions 2 or 3.
Where the dimension of either x1
or x2
is 2, the third component of
the input vector is assumed to be zero and the cross product calculated
accordingly.
In cases where both input vectors have dimension 2, the z-component of the cross product is returned.
Arguments
x1
that defines the vector(s). Defaults to -1
.x2
that defines the vector(s). Defaults to -1
.x1
, x2
and the result that
defines the vector(s) and cross product(s). Overrides axisa
,
axisb
and axisc
.Note:
Torch backend does not support two dimensional vectors, or the
arguments axisa
, axisb
and axisc
. Use axis
instead.
Returns
Vector cross product(s).
cumprod
functionkeras.ops.cumprod(x, axis=None, dtype=None)
Return the cumulative product of elements along a given axis.
Arguments
Returns
Output tensor.
cumsum
functionkeras.ops.cumsum(x, axis=None, dtype=None)
Returns the cumulative sum of elements along a given axis.
Arguments
Returns
Output tensor.
diag
functionkeras.ops.diag(x, k=0)
Extract a diagonal or construct a diagonal array.
Arguments
x
is 2-D, returns the k-th diagonal of x
.
If x
is 1-D, return a 2-D tensor with x
on the k-th diagonal.0
. Use k > 0
for diagonals
above the main diagonal, and k < 0
for diagonals below
the main diagonal.Returns
The extracted diagonal or constructed diagonal tensor.
Examples
>>> from keras.src import ops
>>> x = ops.arange(9).reshape((3, 3))
>>> x
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> ops.diag(x)
array([0, 4, 8])
>>> ops.diag(x, k=1)
array([1, 5])
>>> ops.diag(x, k=-1)
array([3, 7])
>>> ops.diag(ops.diag(x)))
array([[0, 0, 0],
[0, 4, 0],
[0, 0, 8]])
diagonal
functionkeras.ops.diagonal(x, offset=0, axis1=0, axis2=1)
Return specified diagonals.
If x
is 2-D, returns the diagonal of x
with the given offset, i.e., the
collection of elements of the form x[i, i+offset]
.
If x
has more than two dimensions, the axes specified by axis1
and axis2
are used to determine the 2-D sub-array whose diagonal
is returned.
The shape of the resulting array can be determined by removing axis1
and axis2
and appending an index to the right equal to the size of
the resulting diagonals.
Arguments
0
.(main diagonal).0
.(first axis).1
(second axis).Returns
Tensor of diagonals.
Examples
>>> from keras.src import ops
>>> x = ops.arange(4).reshape((2, 2))
>>> x
array([[0, 1],
[2, 3]])
>>> x.diagonal()
array([0, 3])
>>> x.diagonal(1)
array([1])
>>> x = ops.arange(8).reshape((2, 2, 2))
>>> x
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> x.diagonal(0, 0, 1)
array([[0, 6],
[1, 7]])
diff
functionkeras.ops.diff(a, n=1, axis=-1)
Calculate the n-th discrete difference along the given axis.
The first difference is given by out[i] = a[i+1] - a[i]
along
the given axis, higher differences are calculated by using diff
recursively.
Arguments
1
.-1
.(last axis).Returns
Tensor of diagonals.
Examples
>>> from keras.src import ops
>>> x = ops.convert_to_tensor([1, 2, 4, 7, 0])
>>> ops.diff(x)
array([ 1, 2, 3, -7])
>>> ops.diff(x, n=2)
array([ 1, 1, -10])
>>> x = ops.convert_to_tensor([[1, 3, 6, 10], [0, 5, 6, 8]])
>>> ops.diff(x)
array([[2, 3, 4],
[5, 1, 2]])
>>> ops.diff(x, axis=0)
array([[-1, 2, 0, -2]])
digitize
functionkeras.ops.digitize(x, bins)
Returns the indices of the bins to which each value in x
belongs.
Arguments
Returns
Output array of indices, of same shape as x
.
Example
>>> x = np.array([0.0, 1.0, 3.0, 1.6])
>>> bins = np.array([0.0, 3.0, 4.5, 7.0])
>>> keras.ops.digitize(x, bins)
array([1, 1, 2, 1])
divide
functionkeras.ops.divide(x1, x2)
Divide arguments element-wise.
keras.ops.true_divide
is an alias for this function.
Arguments
Returns
Output tensor, the quotient x1/x2
, element-wise.
divide_no_nan
functionkeras.ops.divide_no_nan(x1, x2)
Safe element-wise division which returns 0 where the denominator is 0.
Arguments
Returns
The quotient x1/x2
, element-wise, with zero where x2 is zero.
dot
functionkeras.ops.dot(x1, x2)
Dot product of two tensors.
x1
and x2
are 1-D tensors, it is inner product of vectors
(without complex conjugation).x1
and x2
are 2-D tensors, it is matrix multiplication.x1
or x2
is 0-D (scalar), it is equivalent to x1 * x2
.x1
is an N-D tensor and x2
is a 1-D tensor, it is a sum product
over the last axis of x1
and x2
.x1
is an N-D tensor and x2
is an M-D tensor (where M>=2
),
it is a sum product over the last axis of x1
and the second-to-last
axis of x2
: dot(x1, x2)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
.Arguments
Note: Torch backend does not accept 0-D tensors as arguments.
Returns
Dot product of x1
and x2
.
einsum
functionkeras.ops.einsum(subscripts, *operands)
Evaluates the Einstein summation convention on the operands.
Arguments
->
is included as well as subscript labels of the precise
output form.Returns
The calculation based on the Einstein summation convention.
Example
>>> from keras.src import ops
>>> a = ops.arange(25).reshape(5, 5)
>>> b = ops.arange(5)
>>> c = ops.arange(6).reshape(2, 3)
Trace of a matrix:
>>> ops.einsum("ii", a)
60
>>> ops.einsum(a, [0, 0])
60
>>> ops.trace(a)
60
Extract the diagonal:
>>> ops.einsum("ii -> i", a)
array([ 0, 6, 12, 18, 24])
>>> ops.einsum(a, [0, 0], [0])
array([ 0, 6, 12, 18, 24])
>>> ops.diag(a)
array([ 0, 6, 12, 18, 24])
Sum over an axis:
>>> ops.einsum("ij -> i", a)
array([ 10, 35, 60, 85, 110])
>>> ops.einsum(a, [0, 1], [0])
array([ 10, 35, 60, 85, 110])
>>> ops.sum(a, axis=1)
array([ 10, 35, 60, 85, 110])
For higher dimensional tensors summing a single axis can be done with ellipsis:
>>> ops.einsum("...j -> ...", a)
array([ 10, 35, 60, 85, 110])
>>> np.einsum(a, [..., 1], [...])
array([ 10, 35, 60, 85, 110])
Compute a matrix transpose or reorder any number of axes:
>>> ops.einsum("ji", c)
array([[0, 3],
[1, 4],
[2, 5]])
>>> ops.einsum("ij -> ji", c)
array([[0, 3],
[1, 4],
[2, 5]])
>>> ops.einsum(c, [1, 0])
array([[0, 3],
[1, 4],
[2, 5]])
>>> ops.transpose(c)
array([[0, 3],
[1, 4],
[2, 5]])
Matrix vector multiplication:
>>> ops.einsum("ij, j", a, b)
array([ 30, 80, 130, 180, 230])
>>> ops.einsum(a, [0, 1], b, [1])
array([ 30, 80, 130, 180, 230])
>>> ops.einsum("...j, j", a, b)
array([ 30, 80, 130, 180, 230])
empty
functionkeras.ops.empty(shape, dtype=None)
Return a tensor of given shape and type filled with uninitialized data.
Arguments
Returns
The empty tensor.
equal
functionkeras.ops.equal(x1, x2)
Returns (x1 == x2)
element-wise.
Arguments
Returns
Output tensor, element-wise comparison of x1
and x2
.
exp
functionkeras.ops.exp(x)
Calculate the exponential of all elements in the input tensor.
Arguments
Returns
Output tensor, element-wise exponential of x
.
expand_dims
functionkeras.ops.expand_dims(x, axis)
Expand the shape of a tensor.
Insert a new axis at the axis
position in the expanded tensor shape.
Arguments
Returns
Output tensor with the number of dimensions increased.
expm1
functionkeras.ops.expm1(x)
Calculate exp(x) - 1
for all elements in the tensor.
Arguments
Returns
Output tensor, element-wise exponential minus one.
eye
functionkeras.ops.eye(N, M=None, k=0, dtype=None)
Return a 2-D tensor with ones on the diagonal and zeros elsewhere.
Arguments
None
, defaults to N
.Returns
Tensor with ones on the k-th diagonal and zeros elsewhere.
flip
functionkeras.ops.flip(x, axis=None)
Reverse the order of elements in the tensor along the given axis.
The shape of the tensor is preserved, but the elements are reordered.
Arguments
axis=None
, will flip over all of the axes of the input tensor.Returns
Output tensor with entries of axis
reversed.
floor
functionkeras.ops.floor(x)
Return the floor of the input, element-wise.
The floor of the scalar x
is the largest integer i
, such that i <= x
.
Arguments
Returns
Output tensor, element-wise floor of x
.
floor_divide
functionkeras.ops.floor_divide(x1, x2)
Returns the largest integer smaller or equal to the division of inputs.
Arguments
Returns
Output tensor, y = floor(x1/x2)
full
functionkeras.ops.full(shape, fill_value, dtype=None)
Return a new tensor of given shape and type, filled with fill_value
.
Arguments
Returns
Output tensor.
full_like
functionkeras.ops.full_like(x, fill_value, dtype=None)
Return a full tensor with the same shape and type as the given tensor.
Arguments
Returns
Tensor of fill_value
with the same shape and type as x
.
get_item
functionkeras.ops.get_item(x, key)
Return x[key]
.
greater
functionkeras.ops.greater(x1, x2)
Return the truth value of x1 > x2
element-wise.
Arguments
Returns
Output tensor, element-wise comparison of x1
and x2
.
greater_equal
functionkeras.ops.greater_equal(x1, x2)
Return the truth value of x1 >= x2
element-wise.
Arguments
Returns
Output tensor, element-wise comparison of x1
and x2
.
histogram
functionkeras.ops.histogram(x, bins=10, range=None)
Computes a histogram of the data tensor x
.
Arguments
x
.Returns
Example
```python
>>> input_tensor = np.random.rand(8)
>>> keras.ops.histogram(input_tensor)
(array([1, 1, 1, 0, 0, 1, 2, 1, 0, 1], dtype=int32),
array([0.0189519 , 0.10294958, 0.18694726, 0.27094494, 0.35494262,
0.43894029, 0.52293797, 0.60693565, 0.69093333, 0.77493101,
0.85892869]))
----
<span style="float:right;">[[source]](https://github.com/keras-team/keras/tree/v3.7.0/keras/src/ops/numpy.py#L3049)</span>
### `hstack` function
```python
keras.ops.hstack(xs)
Stack tensors in sequence horizontally (column wise).
This is equivalent to concatenation along the first axis for 1-D tensors, and along the second axis for all other tensors.
Arguments
Returns
The tensor formed by stacking the given tensors.
identity
functionkeras.ops.identity(n, dtype=None)
Return the identity tensor.
The identity tensor is a square tensor with ones on the main diagonal and zeros elsewhere.
Arguments
n x n
output tensor.Returns
The identity tensor.
imag
functionkeras.ops.imag(x)
Return the imaginary part of the complex argument.
Arguments
Returns
The imaginary component of the complex argument.
isclose
functionkeras.ops.isclose(x1, x2, rtol=1e-05, atol=1e-08, equal_nan=False)
Return whether two tensors are element-wise almost equal.
Arguments
True
, element-wise NaNs are considered equal.Returns
Output boolean tensor.
isfinite
functionkeras.ops.isfinite(x)
Return whether a tensor is finite, element-wise.
Real values are finite when they are not NaN, not positive infinity, and not negative infinity. Complex values are finite when both their real and imaginary parts are finite.
Arguments
Returns
Output boolean tensor.
isinf
functionkeras.ops.isinf(x)
Test element-wise for positive or negative infinity.
Arguments
Returns
Output boolean tensor.
isnan
functionkeras.ops.isnan(x)
Test element-wise for NaN and return result as a boolean tensor.
Arguments
Returns
Output boolean tensor.
left_shift
functionkeras.ops.left_shift(x, y)
Shift the bits of an integer to the left.
Bits are shifted to the left by appending y
0s at the right of x
.
Since the internal representation of numbers is in binary format, this
operation is equivalent to multiplying x
by 2**y
.
Arguments
Returns
Result tensor.
less
functionkeras.ops.less(x1, x2)
Return the truth value of x1 < x2
element-wise.
Arguments
Returns
Output tensor, element-wise comparison of x1
and x2
.
less_equal
functionkeras.ops.less_equal(x1, x2)
Return the truth value of x1 <= x2
element-wise.
Arguments
Returns
Output tensor, element-wise comparison of x1
and x2
.
linspace
functionkeras.ops.linspace(
start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0
)
Return evenly spaced numbers over a specified interval.
Returns num
evenly spaced samples, calculated over the interval
[start, stop]
.
The endpoint of the interval can optionally be excluded.
Arguments
endpoint
is set to
False
. In that case, the sequence consists of all but the last
of num + 1
evenly spaced samples, so that stop
is excluded.
Note that the step size changes when endpoint
is False
.50
. Must be
non-negative.True
, stop
is the last sample. Otherwise, it is
not included. Defaults to True
.True
, return (samples, step)
, where step
is the
spacing between samples.0
.Note:
Torch backend does not support axis
argument.
Returns
A tensor of evenly spaced numbers.
If retstep
is True
, returns (samples, step)
log
functionkeras.ops.log(x)
Natural logarithm, element-wise.
Arguments
Returns
Output tensor, element-wise natural logarithm of x
.
log10
functionkeras.ops.log10(x)
Return the base 10 logarithm of the input tensor, element-wise.
Arguments
Returns
Output tensor, element-wise base 10 logarithm of x
.
log1p
functionkeras.ops.log1p(x)
Returns the natural logarithm of one plus the x
, element-wise.
Calculates log(1 + x)
.
Arguments
Returns
Output tensor, element-wise natural logarithm of 1 + x
.
log2
functionkeras.ops.log2(x)
Base-2 logarithm of x
, element-wise.
Arguments
Returns
Output tensor, element-wise base-2 logarithm of x
.
logaddexp
functionkeras.ops.logaddexp(x1, x2)
Logarithm of the sum of exponentiations of the inputs.
Calculates log(exp(x1) + exp(x2))
.
Arguments
Returns
Output tensor, element-wise logarithm of the sum of exponentiations of the inputs.
logical_and
functionkeras.ops.logical_and(x1, x2)
Computes the element-wise logical AND of the given input tensors.
Zeros are treated as False
and non-zeros are treated as True
.
Arguments
Returns
Output tensor, element-wise logical AND of the inputs.
logical_not
functionkeras.ops.logical_not(x)
Computes the element-wise NOT of the given input tensor.
Zeros are treated as False
and non-zeros are treated as True
.
Arguments
Returns
Output tensor, element-wise logical NOT of the input.
logical_or
functionkeras.ops.logical_or(x1, x2)
Computes the element-wise logical OR of the given input tensors.
Zeros are treated as False
and non-zeros are treated as True
.
Arguments
Returns
Output tensor, element-wise logical OR of the inputs.
logical_xor
functionkeras.ops.logical_xor(x1, x2)
Compute the truth value of x1 XOR x2
, element-wise.
Arguments
Returns
Output boolean tensor.
logspace
functionkeras.ops.logspace(start, stop, num=50, endpoint=True, base=10, dtype=None, axis=0)
Returns numbers spaced evenly on a log scale.
In linear space, the sequence starts at base ** start
and ends with
base ** stop
(see endpoint
below).
Arguments
endpoint
is False
.
In that case, num + 1
values are spaced over the interval in
log-space, of which all but the last (a sequence of length num
)
are returned.50
.True
, stop
is the last sample. Otherwise, it is not
included. Defaults to True
.10
.Note:
Torch backend does not support axis
argument.
Returns
A tensor of evenly spaced samples on a log scale.
matmul
functionkeras.ops.matmul(x1, x2)
Matrix product of two tensors.
Arguments
Returns
Output tensor, matrix product of the inputs.
max
functionkeras.ops.max(x, axis=None, keepdims=False, initial=None)
Return the maximum of a tensor or maximum along an axis.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one. Defaults to False
.None
.Returns
Maximum of x
.
maximum
functionkeras.ops.maximum(x1, x2)
Element-wise maximum of x1
and x2
.
Arguments
Returns
Output tensor, element-wise maximum of x1
and x2
.
mean
functionkeras.ops.mean(x, axis=None, keepdims=False)
Compute the arithmetic mean along the specified axes.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one.Returns
Output tensor containing the mean values.
median
functionkeras.ops.median(x, axis=None, keepdims=False)
Compute the median along the specified axis.
Arguments
axis=None
which is to compute the median(s) along a flattened
version of the array.True
, the axes which are reduce
are left in the result as dimensions with size one.Returns
The output tensor.
meshgrid
functionkeras.ops.meshgrid(*x, indexing="xy")
Creates grids of coordinates from coordinate vectors.
Given N
1-D tensors T0, T1, ..., TN-1
as inputs with corresponding
lengths S0, S1, ..., SN-1
, this creates an N
N-dimensional tensors
G0, G1, ..., GN-1
each with shape (S0, ..., SN-1)
where the output
Gi
is constructed by expanding Ti
to the result shape.
Arguments
"xy"
or "ij"
. "xy" is cartesian; "ij"
is matrix
indexing of output. Defaults to "xy"
.Returns
Sequence of N tensors.
Example
>>> from keras.src import ops
>>> x = ops.array([1, 2, 3])
>>> y = ops.array([4, 5, 6])
>>> grid_x, grid_y = ops.meshgrid(x, y, indexing="ij")
>>> grid_x
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
>>> grid_y
array([[4, 5, 6],
[4, 5, 6],
[4, 5, 6]])
min
functionkeras.ops.min(x, axis=None, keepdims=False, initial=None)
Return the minimum of a tensor or minimum along an axis.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one. Defaults to False
.None
.Returns
Minimum of x
.
minimum
functionkeras.ops.minimum(x1, x2)
Element-wise minimum of x1
and x2
.
Arguments
Returns
Output tensor, element-wise minimum of x1
and x2
.
mod
functionkeras.ops.mod(x1, x2)
Returns the element-wise remainder of division.
Arguments
Returns
Output tensor, element-wise remainder of division.
moveaxis
functionkeras.ops.moveaxis(x, source, destination)
Move axes of a tensor to new positions.
Other axes remain in their original order.
Arguments
Returns
Tensor with moved axes.
multiply
functionkeras.ops.multiply(x1, x2)
Multiply arguments element-wise.
Arguments
Returns
Output tensor, element-wise product of x1
and x2
.
nan_to_num
functionkeras.ops.nan_to_num(x, nan=0.0, posinf=None, neginf=None)
Replace NaN with zero and infinity with large finite numbers.
Arguments
NaN
entries with.Returns
x
, with non-finite values replaced.
ndim
functionkeras.ops.ndim(x)
Return the number of dimensions of a tensor.
Arguments
Returns
The number of dimensions in x
.
negative
functionkeras.ops.negative(x)
Numerical negative, element-wise.
Arguments
Returns
Output tensor, y = -x
.
nonzero
functionkeras.ops.nonzero(x)
Return the indices of the elements that are non-zero.
Arguments
Returns
Indices of elements that are non-zero.
norm
functionkeras.ops.norm(x, ord=None, axis=None, keepdims=False)
Matrix or vector norm.
This function is able to return one of eight different matrix norms, or one
of an infinite number of vector norms (described below), depending on the
value of the ord
parameter.
Arguments
None
.axis
is an integer, it specifies the axis of x
along which
to compute the vector norms. If axis
is a 2-tuple, it specifies
the axes that hold 2-D matrices, and the matrix norms of these
matrices are computed.True
, the axes which are reduced are left
in the result as dimensions with size one.Note:
For values of ord < 1
, the result is, strictly speaking, not a
mathematical 'norm', but it may still be useful for various numerical
purposes. The following norms can be calculated:
- For matrices:
- ord=None
: Frobenius norm
- ord="fro"
: Frobenius norm
- ord="nuc"
: nuclear norm
- ord=np.inf
: max(sum(abs(x), axis=1))
- ord=-np.inf
: min(sum(abs(x), axis=1))
- ord=0
: not supported
- ord=1
: max(sum(abs(x), axis=0))
- ord=-1
: min(sum(abs(x), axis=0))
- ord=2
: 2-norm (largest sing. value)
- ord=-2
: smallest singular value
- other: not supported
- For vectors:
- ord=None
: 2-norm
- ord="fro"
: not supported
- ord="nuc"
: not supported
- ord=np.inf
: max(abs(x))
- ord=-np.inf
: min(abs(x))
- ord=0
: sum(x != 0)
- ord=1
: as below
- ord=-1
: as below
- ord=2
: as below
- ord=-2
: as below
- other: sum(abs(x)**ord)**(1./ord)
Returns
Norm of the matrix or vector(s).
Example
>>> x = keras.ops.reshape(keras.ops.arange(9, dtype="float32") - 4, (3, 3))
>>> keras.ops.linalg.norm(x)
7.7459664
not_equal
functionkeras.ops.not_equal(x1, x2)
Return (x1 != x2)
element-wise.
Arguments
Returns
Output tensor, element-wise comparison of x1
and x2
.
ones
functionkeras.ops.ones(shape, dtype=None)
Return a new tensor of given shape and type, filled with ones.
Arguments
Returns
Tensor of ones with the given shape and dtype.
ones_like
functionkeras.ops.ones_like(x, dtype=None)
Return a tensor of ones with the same shape and type of x
.
Arguments
Returns
A tensor of ones with the same shape and type as x
.
outer
functionkeras.ops.outer(x1, x2)
Compute the outer product of two vectors.
Given two vectors x1
and x2
, the outer product is:
out[i, j] = x1[i] * x2[j]
Arguments
Returns
Outer product of x1
and x2
.
pad
functionkeras.ops.pad(x, pad_width, mode="constant", constant_values=None)
Pad a tensor.
Arguments
((before_1, after_1), ...(before_N, after_N))
unique pad
widths for each axis.
((before, after),)
yields same before and after pad for
each axis.
(pad,)
or int
is a shortcut for before = after = pad
width for all axes."constant"
, "edge"
, "linear_ramp"
,
"maximum"
, "mean"
, "median"
, "minimum"
,
"reflect"
, "symmetric"
, "wrap"
, "empty"
,
"circular"
. Defaults to "constant"
.mode == "constant"
.
Defaults to 0
. A ValueError
is raised if not None and
mode != "constant"
.Note:
Torch backend only supports modes "constant"
, "reflect"
,
"symmetric"
and "circular"
.
Only Torch backend supports "circular"
mode.
Note:
Tensorflow backend only supports modes "constant"
, "reflect"
and "symmetric"
.
Returns
Padded tensor.
power
functionkeras.ops.power(x1, x2)
First tensor elements raised to powers from second tensor, element-wise.
Arguments
Returns
Output tensor, the bases in x1
raised to the exponents in x2
.
prod
functionkeras.ops.prod(x, axis=None, keepdims=False, dtype=None)
Return the product of tensor elements over a given axis.
Arguments
axis=None
, will compute the product of all elements
in the input tensor.True
, the axes which are reduce
are left in the result as dimensions with size one.Returns
Product of elements of x
over the given axis or axes.
quantile
functionkeras.ops.quantile(x, q, axis=None, method="linear", keepdims=False)
Compute the q-th quantile(s) of the data along the specified axis.
Arguments
axis=None
which is to compute the quantile(s) along a flattened
version of the array."linear"
, "lower"
, "higher"
,
"midpoint"
, and "nearest"
. Defaults to "linear"
.
If the desired quantile lies between two data points i < j
:"linear"
: i + (j - i) * fraction
, where fraction is the
fractional part of the index surrounded by i
and j
."lower"
: i
."higher"
: j
."midpoint"
: (i + j) / 2
"nearest"
: i
or j
, whichever is nearest.True
, the axes which are reduce
are left in the result as dimensions with size one.Returns
The quantile(s). If q
is a single probability and axis=None
, then
the result is a scalar. If multiple probabilities levels are given,
first axis of the result corresponds to the quantiles. The other axes
are the axes that remain after the reduction of x
.
ravel
functionkeras.ops.ravel(x)
Return a contiguous flattened tensor.
A 1-D tensor, containing the elements of the input, is returned.
Arguments
Returns
Output tensor.
real
functionkeras.ops.real(x)
Return the real part of the complex argument.
Arguments
Returns
The real component of the complex argument.
reciprocal
functionkeras.ops.reciprocal(x)
Return the reciprocal of the argument, element-wise.
Calculates 1/x
.
Arguments
Returns
Output tensor, element-wise reciprocal of x
.
repeat
functionkeras.ops.repeat(x, repeats, axis=None)
Repeat each element of a tensor after themselves.
Arguments
Returns
Output tensor.
reshape
functionkeras.ops.reshape(x, newshape)
Gives a new shape to a tensor without changing its data.
Arguments
Returns
The reshaped tensor.
right_shift
functionkeras.ops.right_shift(x, y)
Shift the bits of an integer to the right.
Bits are shifted to the right y
. Because the internal representation of
numbers is in binary format, this operation is equivalent to dividing x
by
2**y
.
Arguments
Returns
Result tensor.
roll
functionkeras.ops.roll(x, shift, axis=None)
Roll tensor elements along a given axis.
Elements that roll beyond the last position are re-introduced at the first.
Arguments
Returns
Output tensor.
round
functionkeras.ops.round(x, decimals=0)
Evenly round to the given number of decimals.
Arguments
0
.Returns
Output tensor.
searchsorted
functionkeras.ops.searchsorted(sorted_sequence, values, side="left")
Perform a binary search, returning indices for insertion of values
into sorted_sequence
that maintain the sorting order.
Arguments
Returns
Tensor of insertion indices of same shape as values
.
select
functionkeras.ops.select(condlist, choicelist, default=0)
Return elements from choicelist
, based on conditions in condlist
.
Arguments
condlist
.False
.Returns
Tensor where the output at position m
is the m
-th element
of the tensor in choicelist
where the m
-th element of the
corresponding tensor in condlist
is True
.
Example
from keras import ops
x = ops.arange(6)
condlist = [x<3, x>3]
choicelist = [x, x**2]
ops.select(condlist, choicelist, 42)
# # Returns tensor([0, 1, 2, 42, 16, 25])
sign
functionkeras.ops.sign(x)
Returns a tensor with the signs of the elements of x
.
Arguments
Returns
Output tensor of same shape as x
.
sin
functionkeras.ops.sin(x)
Trigonometric sine, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
sinh
functionkeras.ops.sinh(x)
Hyperbolic sine, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
size
functionkeras.ops.size(x)
Return the number of elements in a tensor.
Arguments
Returns
Number of elements in x
.
slogdet
functionkeras.ops.slogdet(x)
Compute the sign and natural logarithm of the determinant of a matrix.
Arguments
Returns
A tuple (sign, logabsdet)
. sign
is a number representing
the sign of the determinant. For a real matrix, this is 1, 0, or -1.
For a complex matrix, this is a complex number with absolute value 1
(i.e., it is on the unit circle), or else 0.
logabsdet
is the natural log of the absolute value of the determinant.
sort
functionkeras.ops.sort(x, axis=-1)
Sorts the elements of x
along a given axis in ascending order.
Arguments
None
, the tensor is flattened
before sorting. Defaults to -1
; the last axis.Returns
Sorted tensor.
split
functionkeras.ops.split(x, indices_or_sections, axis=0)
Split a tensor into chunks.
Arguments
axis
. If a 1-D array of sorted integers,
the entries indicate indices at which the tensor will be split
along axis
.0
.Note: A split does not have to result in equal division when using Torch backend.
Returns
A list of tensors.
sqrt
functionkeras.ops.sqrt(x)
Return the non-negative square root of a tensor, element-wise.
Arguments
Returns
Output tensor, the non-negative square root of x
.
square
functionkeras.ops.square(x)
Return the element-wise square of the input.
Arguments
Returns
Output tensor, the square of x
.
squeeze
functionkeras.ops.squeeze(x, axis=None)
Remove axes of length one from x
.
Arguments
Returns
The input tensor with all or a subset of the dimensions of length 1 removed.
stack
functionkeras.ops.stack(x, axis=0)
Join a sequence of tensors along a new axis.
The axis
parameter specifies the index of the new axis in the
dimensions of the result.
Arguments
0
.Returns
The stacked tensor.
std
functionkeras.ops.std(x, axis=None, keepdims=False)
Compute the standard deviation along the specified axis.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one.Returns
Output tensor containing the standard deviation values.
subtract
functionkeras.ops.subtract(x1, x2)
Subtract arguments element-wise.
Arguments
Returns
Output tensor, element-wise difference of x1
and x2
.
sum
functionkeras.ops.sum(x, axis=None, keepdims=False)
Sum of a tensor over the given axes.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one.Returns
Output tensor containing the sum.
swapaxes
functionkeras.ops.swapaxes(x, axis1, axis2)
Interchange two axes of a tensor.
Arguments
Returns
A tensor with the axes swapped.
take
functionkeras.ops.take(x, indices, axis=None)
Take elements from a tensor along an axis.
Arguments
Returns
The corresponding tensor of values.
take_along_axis
functionkeras.ops.take_along_axis(x, indices, axis=None)
Select values from x
at the 1-D indices
along the given axis.
Arguments
Returns
The corresponding tensor of values.
tan
functionkeras.ops.tan(x)
Compute tangent, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
tanh
functionkeras.ops.tanh(x)
Hyperbolic tangent, element-wise.
Arguments
Returns
Output tensor of same shape as x
.
tensordot
functionkeras.ops.tensordot(x1, x2, axes=2)
Compute the tensor dot product along specified axes.
Arguments
x1
and the
first N axes of x2
in order. The sizes of the corresponding
axes must match.
- Or, a list of axes to be summed over, first sequence applying
to x1
, second to x2
. Both sequences must be of the
same length.Returns
The tensor dot product of the inputs.
tile
functionkeras.ops.tile(x, repeats)
Repeat x
the number of times given by repeats
.
If repeats
has length d
, the result will have dimension of
max(d, x.ndim)
.
If x.ndim < d
, x
is promoted to be d-dimensional by prepending
new axes.
If x.ndim > d
, repeats
is promoted to x.ndim
by prepending 1's to it.
Arguments
x
along each axis.Returns
The tiled output tensor.
trace
functionkeras.ops.trace(x, offset=0, axis1=0, axis2=1)
Return the sum along diagonals of the tensor.
If x
is 2-D, the sum along its diagonal with the given offset is
returned, i.e., the sum of elements x[i, i+offset]
for all i
.
If a has more than two dimensions, then the axes specified by axis1
and axis2
are used to determine the 2-D sub-arrays whose traces are
returned.
The shape of the resulting tensor is the same as that of x
with axis1
and axis2
removed.
Arguments
0
.0
.(first axis).1
(second axis).Returns
If x
is 2-D, the sum of the diagonal is returned. If x
has
larger dimensions, then a tensor of sums along diagonals is
returned.
transpose
functionkeras.ops.transpose(x, axes=None)
Returns a tensor with axes
transposed.
Arguments
x
.
By default, the order of the axes are reversed.Returns
x
with its axes permuted.
tri
functionkeras.ops.tri(N, M=None, k=0, dtype=None)
Return a tensor with ones at and below a diagonal and zeros elsewhere.
Arguments
k = 0
is the main diagonal, while k < 0
is below it, and
k > 0
is above. The default is 0.Returns
Tensor with its lower triangle filled with ones and zeros elsewhere.
T[i, j] == 1
for j <= i + k
, 0 otherwise.
tril
functionkeras.ops.tril(x, k=0)
Return lower triangle of a tensor.
For tensors with ndim
exceeding 2, tril
will apply to the
final two axes.
Arguments
0
. the
main diagonal. k < 0
is below it, and k > 0
is above it.Returns
Lower triangle of x
, of same shape and data type as x
.
triu
functionkeras.ops.triu(x, k=0)
Return upper triangle of a tensor.
For tensors with ndim
exceeding 2, triu
will apply to the
final two axes.
Arguments
0
. the
main diagonal. k < 0
is below it, and k > 0
is above it.Returns
Upper triangle of x
, of same shape and data type as x
.
true_divide
functionkeras.ops.true_divide(x1, x2)
Alias for keras.ops.divide
.
trunc
functionkeras.ops.trunc(x)
Return the truncated value of the input, element-wise.
The truncated value of the scalar x
is the nearest integer i
which is
closer to zero than x
is. In short, the fractional part of the signed
number x
is discarded.
Arguments
Returns
The truncated value of each element in x
.
Example
>>> x = ops.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
>>> ops.trunc(x)
array([-1.0, -1.0, -0.0, 0.0, 1.0, 1.0, 2.0])
var
functionkeras.ops.var(x, axis=None, keepdims=False)
Compute the variance along the specified axes.
Arguments
True
, the axes which are reduced are left
in the result as dimensions with size one.Returns
Output tensor containing the variance.
vdot
functionkeras.ops.vdot(x1, x2)
Return the dot product of two vectors.
If the first argument is complex, the complex conjugate of the first argument is used for the calculation of the dot product.
Multidimensional tensors are flattened before the dot product is taken.
Arguments
Returns
Output tensor.
vectorize
functionkeras.ops.vectorize(pyfunc, excluded=None, signature=None)
Turn a function into a vectorized function.
Example
def myfunc(a, b):
return a + b
vfunc = keras.ops.vectorize(myfunc)
y = vfunc([1, 2, 3, 4], 2) # Returns Tensor([3, 4, 5, 6])
Arguments
pyfunc
unmodified."(m,n),(n)->(m)"
for vectorized
matrix-vector multiplication. If provided,
pyfunc
will be called with (and expected to return)
arrays with shapes given by the size of corresponding
core dimensions. By default, pyfunc
is assumed
to take scalars tensors as input and output.Returns
A new function that applies pyfunc
to every element
of its input along axis 0 (the batch axis).
vstack
functionkeras.ops.vstack(xs)
Stack tensors in sequence vertically (row wise).
Arguments
Returns
Tensor formed by stacking the given tensors.
where
functionkeras.ops.where(condition, x1=None, x2=None)
Return elements chosen from x1
or x2
depending on condition
.
Arguments
True
, yield x1
, otherwise yield x2
.condition
is True
.condition
is False
.Returns
A tensor with elements from x1
where condition
is True
, and
elements from x2
where condition
is False
.
zeros
functionkeras.ops.zeros(shape, dtype=None)
Return a new tensor of given shape and type, filled with zeros.
Arguments
Returns
Tensor of zeros with the given shape and dtype.
zeros_like
functionkeras.ops.zeros_like(x, dtype=None)
Return a tensor of zeros with the same shape and type as x
.
Arguments
Returns
A tensor of zeros with the same shape and type as x
.