mygrad.power#

class mygrad.power(x1: ArrayLike, x2: ArrayLike, out: Tensor | ndarray | None = None, *, where: Mask = True, dtype: DTypeLikeReals = None, constant: bool | None = None)#

First tensor elements raised to powers from second tensor, element-wise.

Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. Note that an integer type raised to a negative integer power will raise a ValueError.

This docstring was adapted from that of numpy.power [1]

Parameters:
x1ArrayLike

The bases.

x2ArrayLike

The exponents. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output). Non-tensor array-likes are treated as constants.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant.grad will always return None).

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

dtypeOptional[DTypeLikeReals]

The dtype of the resulting tensor.

outOptional[Union[ndarray, Tensor]]

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated tensor is returned.

whereMask

This condition is broadcast over the input. At locations where the condition is True, the out tensor will be set to the ufunc result. Elsewhere, the out tensor will retain its original value. Note that if an uninitialized out tensor is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Returns:
powerTensor

The combination of x1 and x2, element-wise.

See also

float_power

power function that promotes integers to float

References

Examples

Cube each element in a list.

>>> import mygrad as mg
>>> x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> mg.power(x1, 3)
Tensor([  0,   1,   8,  27,  64, 125])

Raise the bases to different exponents.

>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> mg.power(x1, x2)
Tensor([  0.,   1.,   8.,  27.,  16.,   5.])

The effect of broadcasting.

>>> x2 = mg.tensor([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
Tensor([[1, 2, 3, 3, 2, 1],
        [1, 2, 3, 3, 2, 1]])
>>> mg.power(x1, x2)
Tensor([[ 0,  1,  8, 27, 16,  5],
        [ 0,  1,  8, 27, 16,  5]])
Attributes:
identity
signature

Methods

accumulate([axis, dtype, out, constant])

Not implemented

at(indices[, b, constant])

Not implemented

outer(b, *[, dtype, out])

Not Implemented

reduce([axis, dtype, out, keepdims, ...])

Not Implemented

reduceat(indices[, axis, dtype, out])

Not Implemented

resolve_dtypes(dtypes, *[, signature, ...])

Find the dtypes NumPy will use for the operation.

__init__(*args, **kwargs)#

Methods

__init__(*args, **kwargs)

accumulate([axis, dtype, out, constant])

Not implemented

at(indices[, b, constant])

Not implemented

outer(b, *[, dtype, out])

Not Implemented

reduce([axis, dtype, out, keepdims, ...])

Not Implemented

reduceat(indices[, axis, dtype, out])

Not Implemented

resolve_dtypes(dtypes, *[, signature, ...])

Find the dtypes NumPy will use for the operation.

Attributes

identity

nargs

nin

nout

ntypes

signature

types