oneflow.math

oneflow.math.abs(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator returns the absolute value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def abs_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.abs(x)


x = np.array([-1, 2, -3]).astype(np.float32)
out = abs_Job(x)

# out [1. 2. 3.]
oneflow.math.acos(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the acos value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def acos_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.acos(x)


x = np.array([0.5, 0.6, 0.7]).astype(np.float32)
out = acos_Job(x)

# out [1.0471976 0.9272952 0.7953989]
# We take the first value as an example
# (arccos(0.5) * pi) / 180 = 1.0471976
oneflow.math.acosh(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the inverse hyperbolic cosine value of Blob.

The equation is:

\[out = log(x+(x^2-1)^\frac{1}{2})\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob, the range is [1, inf]

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def acosh_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.acosh(x)


x = np.array([2, 3, 4]).astype(np.float32)
out = acosh_Job(x)

# out [1.316958  1.7627473 2.063437 ]
oneflow.math.add(x: Union[int, float, oneflow_api.BlobDesc], y: Union[int, float, oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

Compute \(X + Y\) element-wise, math.add supports broadcasting. The equation is:

\[out = X + Y\]
Parameters
  • x (Union[int, float, oneflow_api.BlobDesc]) – A Blob.

  • y (Union[int, float, oneflow_api.BlobDesc]) – A Blob has the same type of x.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob is added by x and y, and has the same type of x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def addJob(x: tp.Numpy.Placeholder((3, )),
        y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.add(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([1, 1, 1]).astype(np.float32)
out = addJob(x, y)

# out [2., 3., 4.]
oneflow.math.add_n(inputs: Sequence[oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

Add all the input tensors in element-wise.

Parameters
  • inputs (Sequence[oneflow_api.BlobDesc]) – A list of Blob, each Blob has the same shape and type.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The sum of the inputs, has the same shape and type as the elements of inputs.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def add_n_Job(x: tp.Numpy.Placeholder((3, )),
            y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.add_n([x, y])

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([1, 1, 1]).astype(np.float32)
out = add_n_Job(x, y)
print(out)

# out [2., 3., 4.]
oneflow.math.argmax(input: oneflow_api.BlobDesc, axis: int = -1, name: Optional[str] = None) → oneflow_api.BlobDesc

The op computes the index with the largest value of a Blob at specified axis.

Parameters
  • input (oneflow_api.BlobDesc) – Input Blob

  • axis (int, optional) – dimension to be calculated. Defaults to the last dim (-1)

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob(dtype=int32) contains the index with the largest value of input

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def argmax_Job(x: tp.Numpy.Placeholder((2, 5))
)->tp.Numpy:
    return flow.math.argmax(x)

x = np.array([[1, 3, 8, 7, 2],
            [1, 9, 4, 3, 2]], dtype=np.float32)

out = argmax_Job(x)

# out [2 1]
oneflow.math.asin(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the arcsin value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def asin_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.asin(x)


x = np.array([0.5, 0.6, 0.7]).astype(np.float32)
out = asin_Job(x)

# out [0.5235988  0.64350116 0.7753975 ]
# We take the first value as an example
# (arcsin(0.5) * pi) / 180 = 0.5235988
oneflow.math.asinh(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the inverse hyperbolic sine value of Blob.

The equation is:

\[out = log(x+(x^2+1)^\frac{1}{2})\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def asinh_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.asinh(x)


x = np.array([2, 3, 4]).astype(np.float32)
out = asinh_Job(x)

# out [1.4436355 1.8184464 2.0947125]
oneflow.math.atan(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the arctan value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import numpy as np
import oneflow.typing as tp


@flow.global_function()
def atan_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.atan(x)


x = np.array([0.5, 0.6, 0.7]).astype(np.float32)
out = atan_Job(x)

# out [0.4636476  0.5404195  0.61072594]
# We take the first value as an example
# (arctan(0.5) * pi) / 180 = 0.4636476
oneflow.math.atan2(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the values of \(arctan(\frac{x}{y})\).

The equation is:

\[out = arctan(\frac{x}{y})\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def atan2Job(x: tp.Numpy.Placeholder((3,),), y: tp.Numpy.Placeholder((3, ))
)-> tp.Numpy:
    return flow.math.atan2(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([4, 4, 4]).astype(np.float32)
out = atan2Job(x, y)


# out [0.24497867 0.4636476  0.6435011 ]
# We take the first value as an example
# (arctan(1/4) * pi) / 180 = 0.24497867
oneflow.math.atanh(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the inverse hyperbolic tangent value of Blob.

The equation is:

\[out = \frac{1}{2}*log(\frac{1+x}{1-x})\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def atanh_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.atanh(x)


x = np.array([0.5, 0.6, 0.7]).astype(np.float32)
out = atanh_Job(x)

# out [0.54930615 0.6931472  0.8673005 ]
oneflow.math.broadcast_to_compatible_with(x: oneflow_api.BlobDesc, compatible: Sequence[oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

Returns a ‘Blob’ with the shape can be broadcasted by other shapes

Parameters
  • x (oneflow_api.BlobDesc) – a ‘Blob’

  • compatible (Sequence[oneflow_api.BlobDesc]) – Sequence of different shape

  • name (Optional[str], optional) – This operator’s name. Defaults to None.

Returns

A ‘Blob’ with the biggest shape

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def broadcast_to_compatible_with_Job(x: tp.Numpy.Placeholder((4, 1, 1))
)->tp.Numpy:
    blob_a = flow.constant(value=1, dtype=flow.float32, shape=(1, 2, 1))
    blob_b = flow.constant(value=1, dtype=flow.float32, shape=(1, 1, 3))

    return flow.math.broadcast_to_compatible_with(x, [blob_a, blob_b])

x = np.ones(shape=(4, 1, 1), dtype=np.float32)

out = broadcast_to_compatible_with_Job(x)

# out.shape (4, 2, 3)
oneflow.math.ceil(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the ceiling value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def ceil_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.ceil(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = ceil_Job(x)

# out [2. 2. 3.]
oneflow.math.clip_by_value(values: oneflow_api.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow_api.BlobDesc

This op clips Blob values to a specified min value and max value.

The equation is:

\[out = MIN(MAX(x, min), max)\]
Parameters
  • values (oneflow_api.BlobDesc) – Input Blob

  • min_value (Optional[Union[int, float]], optional) – The minimum value to clip by. Defaults to None.

  • max_value (Optional[Union[int, float]], optional) – The maximum value to clip by. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – min_value and max_value cannot be None at the same time

Returns

A clipped Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def clip_by_value_Job(x: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.clip_by_value(x, min_value=-1, max_value=5)

x = np.array([-2, 1, 4, 7], dtype=np.float32)

out = clip_by_value_Job(x)

# out [-1. 1. 4. 5.]
oneflow.math.cos(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the cosine value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import numpy as np
import oneflow.typing as tp


@flow.global_function()
def cos_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.cos(x)


x = np.array([1/3*np.pi, 0.25*np.pi, 1.25*np.pi]).astype(np.float32)
out = cos_Job(x)

# out [ 0.49999997  0.70710677 -0.7071068 ]
oneflow.math.cosh(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes hyperbolic cosine value of Blob.

The equation is:

\[out = \frac{e^x+e^{-x}}{2}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def cosh_Job(x: tp.Numpy.Placeholder((3,))
            ) -> tp.Numpy:
    return flow.math.cosh(x)


x = np.array([1, 2, 3]).astype(np.float32)
out = cosh_Job(x)

# out [ 1.5430806  3.7621958 10.067662 ]
oneflow.math.divide(x: Union[int, float, oneflow_api.BlobDesc], y: Union[int, float, oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

Computes the division of x by y.

The equation is:

\[out = \frac{X}{Y}\]
Parameters
  • x (Union[int, float, oneflow_api.BlobDesc]) – A Blob.

  • y (Union[int, float, oneflow_api.BlobDesc]) – A Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with same shape as input x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def divideJob(x: tp.Numpy.Placeholder((3, )),
            y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.divide(x, y)

x = np.array([25, 16, 9]).astype(np.float32)
y = np.array([10, 4, 2]).astype(np.float32)
out = divideJob(x, y)

# out [2.5, 4., 4.5]
oneflow.math.equal(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the truth value of \({x}=={y}\) element-wise.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def equal_Job(x: tp.Numpy.Placeholder((3, )),
            y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.equal(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([1, 2, 1]).astype(np.float32)
out = equal_Job(x, y)

# out [1 1 0]
oneflow.math.erf(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the Gauss error value of Blob.

The equation is:

\[out = \frac{2}{\sqrt{\pi}}*\int_{0}^{x}e^{-z^2}\mathrm{d}{z}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def erf_Job(x: tp.Numpy.Placeholder((3,))
            ) -> tp.Numpy:
    return flow.math.erf(x)


x = np.array([1, 2, 3]).astype(np.float32)
out = erf_Job(x)

# out [0.8427008 0.9953223 0.9999779]
oneflow.math.erfc(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the \(1-erf(x)\), for more details of erf function please refer to math.erf.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def erfc_Job(x: tp.Numpy.Placeholder((3,))
            ) -> tp.Numpy:
    return flow.math.erfc(x)


x = np.array([1, 2, 3]).astype(np.float32)
out = erfc_Job(x)

# out [1.5729921e-01 4.6777353e-03 2.2090495e-05]
oneflow.math.exp(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the exponential of Blob.

The equation is:

\[out = e^x\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import numpy as np
import oneflow.typing as tp


@flow.global_function()
def exp_Job(x: tp.Numpy.Placeholder((3,))
            ) -> tp.Numpy:
    return flow.math.exp(x)


x = np.array([1, 2, 3]).astype(np.float32)
out = exp_Job(x)

# out [ 2.7182817  7.389056  20.085537 ]
oneflow.math.expm1(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes \(y=e^x-1\).

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def expm1_Job(x: tp.Numpy.Placeholder((3,))
            ) -> tp.Numpy:
    return flow.math.expm1(x)


x = np.array([1, 2, 3]).astype(np.float32)
out = expm1_Job(x)

# out [ 1.7182819  6.389056  19.085537 ]
oneflow.math.floor(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the largest integer not greater than input Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def floor_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.floor(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = floor_Job(x)

# out [1. 1. 2.]
oneflow.math.floordiv(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the result of \(x/y\), rounding toward the most negative integer value

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def floor_div_Job(x: tp.Numpy.Placeholder((3,)),
                y: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.floordiv(x, y)


x = np.array([4, 3, 5]).astype(np.float32)
y = np.array([3, 2, 2]).astype(np.float32)
out = floor_div_Job(x, y)

# out [1. 1. 2.]
oneflow.math.fused_scale_tril(x: oneflow_api.BlobDesc, diagonal: int = 0, fill_value: Union[int, float] = 0, scale: Union[int, float] = 1, name: Optional[str] = None) → oneflow_api.BlobDesc
oneflow.math.gelu(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Gelu activation operator.

The equation is:

\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]
Parameters
  • x (oneflow_api.BlobDesc) – Input Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def geluJob(x: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.gelu(x)

x = np.array([-0.5, 0, 0.5]).astype(np.float32)
out = geluJob(x)

# out [-0.15426877, 0., 0.34573123]
oneflow.math.gelu_grad(x: oneflow_api.BlobDesc, dy: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc
oneflow.math.greater(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the truth value of \(x > y\) element-wise.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def greater_Job(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.greater(x, y)

x = np.array([1, 1, 4]).astype(np.float32)
y = np.array([1, 2, 3]).astype(np.float32)
out = greater_Job(x, y)

# out [0 0 1]
oneflow.math.greater_equal(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the truth value of \(x >= y\) element-wise.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def greater_equal_Job(x: tp.Numpy.Placeholder((3, )),
                    y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.greater_equal(x, y)

x = np.array([1, 1, 4]).astype(np.float32)
y = np.array([1, 2, 3]).astype(np.float32)
out = greater_equal_Job(x, y)

# out [1 0 1]
oneflow.math.in_top_k(targets: oneflow_api.BlobDesc, predictions: oneflow_api.BlobDesc, k: Optional[int], name: Optional[str] = None) → oneflow_api.BlobDesc

Says whether the targets are in the top K predictions.

Parameters
  • targets (oneflow_api.BlobDesc) – A Blob of type int32 or int64.

  • predictions (oneflow_api.BlobDesc) – A Blob of type float32.

  • k (Optional[int], optional) – Number of top elements to look at for computing precision.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob of type bool. Computed Precision at k as a bool Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def intopk_Job(
    targets: tp.Numpy.Placeholder((2,), dtype=flow.int32),
    predictions: tp.Numpy.Placeholder((2, 4), dtype=flow.float32),
) -> tp.Numpy:
    return flow.math.in_top_k(targets, predictions, 1)

targets = np.array([3, 1], dtype=np.int32)
predictions = np.array([[0.0, 1.0, 2.0, 3.0], [3.0, 2.0, 1.0, 0.0],], dtype=np.float32)
out = intopk_Job(targets, predictions)

# out [1 0]
oneflow.math.l2_normalize(input: oneflow_api.BlobDesc, axis: Optional[int] = None, epsilon: float = 1e-12, name: Optional[str] = None) → oneflow_api.BlobDesc

Use L2 norm to normalizes along dimension axis

The equation is:

\[out = \frac{x}{\sqrt{\Sigma{x^2}+\epsilon}}\]
Parameters
  • input (oneflow_api.BlobDesc) – Input Blob

  • axis (Optional[int], optional) – The axis on which to apply L2 normalization. Defaults to None.

  • epsilon (float, optional) – The epsilon value is used to avoid division by zero. Defaults to 1e-12.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The normalized Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def l2_normalize_Job(x: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.l2_normalize(x, axis=0)

x = np.array([1, 2, 3, 4], dtype=np.float32)

out = l2_normalize_Job(x)

# out [0.18257418 0.36514837 0.5477226  0.73029673]
oneflow.math.less(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the truth value of \(x < y\) element-wise.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def less_Job(x: tp.Numpy.Placeholder((3, )),
            y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.less(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([1, 2, 4]).astype(np.float32)
out = less_Job(x, y)

# out [0 0 1]
oneflow.math.less_equal(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the truth value of \(x <= y\) element-wise.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def less_equal_Job(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.less_equal(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([1, 1, 4]).astype(np.float32)
out = less_equal_Job(x, y)

# out [1 0 1]
oneflow.math.lgamma(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the \(Gamma(x)\) value.

The equation is:

\[out = \int_{0}^{\infty}t^{x-1}*e^{-t}\mathrm{d}{t}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def lgamma_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.lgamma(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = lgamma_Job(x)

# out [-0.1081748  -0.12078223  0.4348206 ]
oneflow.math.log(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the log value of input Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def log_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.log(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = log_Job(x)

# out [0.26236424 0.40546513 0.9932518 ]
oneflow.math.log1p(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the \(log(x)+1\) value of input Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def log1p_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.log1p(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = log1p_Job(x)

# out [0.8329091  0.91629076 1.3083328 ]
oneflow.math.log_sigmoid(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the log sigmoid value of input Blob.

The equation is:

\[out = log(\frac{1}{1+e^{-x}})\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def log_sigmoid_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.log_sigmoid(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = log_sigmoid_Job(x)

# out [-0.24100842 -0.20141333 -0.0650436 ]
oneflow.math.logical_and(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Logical AND function.

Each element is calculated by:

\[out = X \land Y\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def logical_and_Job(x: tp.Numpy.Placeholder((3, )),
                    y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.logical_and(x, y)

x = np.array([1, 0, 1]).astype(np.float32)
y = np.array([0, 0, 1]).astype(np.float32)
out = logical_and_Job(x, y)

# out [0 0 1]
oneflow.math.maximum(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the max of x and y element-wise, this op supports broadcasting.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob. Must have the same type of x

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob, has the same type of x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def maximum_Job(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.maximum(x, y)

x = np.array([2, 3, 4]).astype(np.float32)
y = np.array([4, 2, 1]).astype(np.float32)
out = maximum_Job(x, y)

# out [4. 3. 4.]
oneflow.math.minimum(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the min of x and y element-wise, this op supports broadcasting.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob. Must have the same type of x

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob, has the same type of x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def minimum_Job(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.minimum(x, y)

x = np.array([2, 3, 4]).astype(np.float32)
y = np.array([4, 2, 1]).astype(np.float32)
out = minimum_Job(x, y)

# out [2. 2. 1.]
oneflow.math.mod(x: Union[int, float, oneflow_api.BlobDesc], y: Union[int, float, oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

This operator mods two Blobs.

The equation is:

\[out = X \bmod Y\]
Parameters
  • x (Union[int, float, oneflow_api.BlobDesc]) – A Blob

  • y (Union[int, float, oneflow_api.BlobDesc]) – A Blob has the same type of x

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises
  • NotImplementedError – x must be an int or a float

  • NotImplementedError – y must be an int or a float

Returns

A Blob with same type as input x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def modJob(x: tp.Numpy.Placeholder((3, )),
        y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.mod(x, y)

x = np.array([16, 9, 5]).astype(np.float32)
y = np.array([6, 4, 3]).astype(np.float32)
out = modJob(x, y)

# out [4., 1., 2.]
oneflow.math.multiply(x: Union[int, float, oneflow_api.BlobDesc], y: Union[int, float, oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

Compute \(x \times y\) element-wise.

The equation is:

\[out = X \times Y\]
Parameters
  • x (Union[int, float, oneflow_api.BlobDesc]) – A Blob.

  • y (Union[int, float, oneflow_api.BlobDesc]) – A Blob has the same type of x.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob after multiplying, has the same type as x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def multiplyJob(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.multiply(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([2, 3, 3]).astype(np.float32)
out = multiplyJob(x, y)

# out [2., 6., 9.]
oneflow.math.negative(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the negative value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def negative_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.negative(x)


x = np.array([1.3, 1.5, 2.7]).astype(np.float32)
out = negative_Job(x)

# out [-1.3 -1.5 -2.7]
oneflow.math.not_equal(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Returns the truth value of \({x}!={y}\) element-wise.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with int8 type.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def not_equal_Job(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.not_equal(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([1, 2, 1]).astype(np.float32)
out = not_equal_Job(x, y)

# out [0 0 1]
oneflow.math.polyval(coeffs: Union[List, Tuple], x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Computes the elementwise value of a polynomial.

Parameters
  • coeffs (Union[List, Tuple]) – The coefficients of the polynomial.

  • x (oneflow_api.BlobDesc) – A Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob, has the same data type of x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def polyval_Job(
    x: tp.Numpy.Placeholder((3,), dtype=flow.float32)
) -> tp.Numpy:
    coeffs = [1.0, 3.0, -2.0]
    return flow.math.polyval(coeffs, x)

x = np.array([1.0, 2.0, 3.0]).astype(np.float32)
out = polyval_Job(x)

# output [ 2. 8. 16.]
oneflow.math.pow(x: oneflow_api.BlobDesc, y: Union[oneflow_api.BlobDesc, float], name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the Pow result.

The equation is:

\[out = x^y\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (Union[oneflow_api.BlobDesc, float]) – A Blob or float value, the exponential factor of Pow

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

Example 1:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def powJob(x: tp.Numpy.Placeholder((3,), ), y: tp.Numpy.Placeholder((3,))
        ) -> tp.Numpy:
    return flow.math.pow(x, y)


x = np.array([2, 3, 4]).astype(np.float32)
y = np.array([2, 3, 4]).astype(np.float32)
out = powJob(x, y)

# out [  4.  27. 256.]

Example 2:

import oneflow as flow
import oneflow.typing as tp
import numpy as np


@flow.global_function()
def scalar_pow_job(x: tp.Numpy.Placeholder(shape=(3, )))->tp.Numpy:
    with flow.scope.placement("cpu", "0:0"):
        out = flow.math.pow(x, 2.0)
    return out


x = np.array([1, 2, 3]).astype(np.float32)
out = scalar_pow_job(x)

# out [1. 4. 9.]
oneflow.math.reciprocal(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the reciprocal of x.

The equation is:

\[out = \frac{1}{x}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reciprocal_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.reciprocal(x)


x = np.array([1, 2, 4]).astype(np.float32)
out = reciprocal_Job(x)

# out [1.   0.5  0.25]
oneflow.math.reciprocal_no_nan(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the safe reciprocal of x. If x is zero, the reciprocal will be also set to zero.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reciprocal_no_nan_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.reciprocal_no_nan(x)


x = np.array([0, 2, 4]).astype(np.float32)
out = reciprocal_no_nan_Job(x)

# out [0.   0.5  0.25]
oneflow.math.reduce_all(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the logical and of input Blob along the specified axis

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the logical and value is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of logical and value on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

Note

The input Blob dtype is int8

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_all_Job(x: tp.Numpy.Placeholder((3, 3), dtype=flow.int8)
) -> tp.Numpy:
    return flow.math.reduce_all(x, axis=1, keepdims=True)


x = np.array([[1, 0, 0], [0, 0, 0], [1, 1, 1]]).astype(np.int8)
out = reduce_all_Job(x)

# out [[0]
#      [0]
#      [1]]
oneflow.math.reduce_any(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the logical or of input Blob along the specified axis

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the logical and value is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of logical or on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

Note

The input Blob dtype is int8

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_any_Job(x: tp.Numpy.Placeholder((3, 3), dtype=flow.int8)
) -> tp.Numpy:
    return flow.math.reduce_any(x, axis=1, keepdims=True)


x = np.array([[1, 0, 0], [0, 0, 0], [1, 0, 1]]).astype(np.int8)
out = reduce_any_Job(x)

# out [[1]
#      [0]
#      [1]]
oneflow.math.reduce_euclidean_norm(input_tensor: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the Euclidean norm of input Blob along the specified axis

The equation is:

\[out=\sqrt{\sum_{t=0}^{n} x_{t}^2}\]
Parameters
  • input_tensor (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the Euclidean norm is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of Euclidean norm on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_euclidean_norm_Job(x: tp.Numpy.Placeholder((3, 2))
) -> tp.Numpy:
    return flow.math.reduce_euclidean_norm(x, axis=1, keepdims=True)


x = np.array([[3, 4], [5, 12], [8, 15]]).astype(np.float32)
out = reduce_euclidean_norm_Job(x)

# out [[ 5.]
#      [13.]
#      [17.]]
oneflow.math.reduce_logsumexp(input_tensor: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the log of exponential sum of input Blob along the specified axis

The equation is:

\[out = log(\sum_{t=0}^{t=n} e^{x_{t}})\]
Parameters
  • input_tensor (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the log of exponential sum is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of log of exponential sum on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_logsumexp_Job(x: tp.Numpy.Placeholder((3, 2))
) -> tp.Numpy:
    return flow.math.reduce_logsumexp(x, axis=1, keepdims=True)


x = np.array([[0, 0], [1, 1], [2, 2]]).astype(np.float32)
out = reduce_logsumexp_Job(x)

# out [[0.6931472]
#      [1.6931472]
#      [2.6931472]]
oneflow.math.reduce_max(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the maximum value of input Blob along the specified axis

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the maximum value is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of maximum value on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_max_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_max(x, axis=1, keepdims=True)


x = np.array([[2, 1, 4], [5, 3, 7], [7, 4, 9]]).astype(np.float32)
out = reduce_max_Job(x)

# out [[4.]
#      [7.]
#      [9.]]
oneflow.math.reduce_mean(input_blob: oneflow_api.BlobDesc, axis: Union[collections.abc.Sized, int, None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the mean of input Blob along the specified axis

Parameters
  • input_blob (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[collections.Sized, int]], optional) – The dimension along which the mean value is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of average on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_mean_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_mean(x, axis=1, keepdims=True)


x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.float32)
out = reduce_mean_Job(x)

# out [[2.]
#      [5.]
#      [8.]]
oneflow.math.reduce_min(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the minimum value of input Blob along the specified axis

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the minimum value is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of minimum value on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_min_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_min(x, axis=1, keepdims=True)


x = np.array([[2, 1, 3], [5, 3, 6], [7, 4, 9]]).astype(np.float32)
out = reduce_min_Job(x)

# out [[1.]
#      [3.]
#      [4.]]
oneflow.math.reduce_prod(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the product of input Blob along the specified axis

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the product is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of product value on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_product_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_prod(x, axis=1, keepdims=True)


x = np.array([[1, 2, 3], [3, 4, 5], [6, 3, 2]]).astype(np.float32)
out = reduce_product_Job(x)

# out [[ 6.]
#      [60.]
#      [36.]]
oneflow.math.reduce_std(input_tensor: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the standard deviation of input Blob along the specified axis

The equation is:

\[out=\sqrt{\frac{1}{n}*\sum_{i=1}^{n}(x_i-mean)^2}\]
Parameters
  • input_tensor (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the standard deviation is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of standard deviation on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_std_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_std(x, axis=1, keepdims=True)


x = np.array([[0, 5, 10], [5, 5, 5], [12, 3, 0]]).astype(np.float32)
out = reduce_std_Job(x)

# out [[4.0824833]
#      [0.       ]
#      [5.0990195]]
oneflow.math.reduce_sum(input_tensor: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the sum of elements across dimensions of a tensor

Parameters
  • input_tensor (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the sum value is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of sum on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_sum_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_sum(x, axis=1, keepdims=True)


x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.float32)
out = reduce_sum_Job(x)

# out [[ 6.]
#      [15.]
#      [24.]]
oneflow.math.reduce_variance(input_tensor: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the variance of input Blob along the specified axis

The equation is:

\[out=\frac{1}{n}*\sum_{i=1}^{n}(x_i-mean)^2\]
Parameters
  • input_tensor (oneflow_api.BlobDesc) – A Blob

  • axis (Optional[Union[int, Sequence[int]]], optional) – The dimension along which the variance is computed. Defaults to None.

  • keepdims (bool, optional) – Whether to keep the reduced dimension in the output Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result of variance on the specified axis of input Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def reduce_variance_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.math.reduce_variance(x, axis=1, keepdims=True)


x = np.array([[0, 5, 10], [5, 5, 5], [12, 3, 0]]).astype(np.float32)
out = reduce_variance_Job(x)

# out [[16.666668]
#      [ 0.      ]
#      [26.      ]]
oneflow.math.reduced_shape_elem_cnt(input_blob: oneflow_api.BlobDesc, axis: Optional[Sequence[int]] = None, dtype: Optional[oneflow.python.framework.dtype.dtype] = None, name: Optional[str] = None) → oneflow_api.BlobDesc

Computes the product of input_blob’s dimensions along the parameter axis. By default, all the dimensions will be computed.

Parameters
  • input_blob (oneflow_api.BlobDesc) – Input Blob

  • axis (Optional[Sequence[int]], optional) – The dimensions along which the op is performed. Defaults to None.

  • dtype (Optional[dtype_util.dtype], optional) – The data type. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob

Return type

oneflow_api.BlobDesc

For example:

# Example 1:
import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def elem_cnt_Job(x: tp.Numpy.Placeholder((3, 4, 5))
)->tp.Numpy:
    return flow.math.reduced_shape_elem_cnt(x, axis=[0, 1])

x = np.ones(shape=(3, 4, 5), dtype=np.float32)
out = elem_cnt_Job(x) # 3 x 4 = 12

# out [12]

# Example 2:
import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def elem_cnt_Job(x: tp.Numpy.Placeholder((3, 4, 5))
)->tp.Numpy:
    return flow.math.reduced_shape_elem_cnt(x)

x = np.ones(shape=(3, 4, 5), dtype=np.float32)
out = elem_cnt_Job(x) # 3 x 4 x 5 = 60

# out [60]
oneflow.math.relu(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Relu activation

The equation is:

\[out = max(X, 0)\]
Parameters
  • x (oneflow_api.BlobDesc) – Input Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

An activated Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def reluJob(x: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.relu(x)

x = np.array([-1, 0, 5]).astype(np.float32)
out = reluJob(x)

# out [0., 0., 5.]
oneflow.math.rint(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the closest integer to Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def rint_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.rint(x)


x = np.array([1.49999, 1.500001, 2.7]).astype(np.float32)
out = rint_Job(x)

# out [1. 2. 3.]
oneflow.math.round(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator rounds the value of Blob to the nearest integer.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def round_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.round(x)


x = np.array([1.49999, 1.500001, 2.7]).astype(np.float32)
out = round_Job(x)

# out [1. 2. 3.]
oneflow.math.rsqrt(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the reciprocal of square root value of Blob.

The equation is:

\[out=\frac{1}{\sqrt{x}}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def rsqrt_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.rsqrt(x)


x = np.array([4, 16, 25]).astype(np.float32)
out = rsqrt_Job(x)

# out [0.5  0.25 0.2 ]
oneflow.math.sigmoid(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

Sigmoid activation

The equation is:

\[out = \frac{1}{1 + e^{-x}}\]
Parameters
  • x (oneflow_api.BlobDesc) – Input Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

An activated Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def sigmoidJob(x: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.sigmoid(x)

x = np.array([-1, 0, 1]).astype(np.float32)
out = sigmoidJob(x)

# out [0.26894143, 0.5, 0.7310586]
oneflow.math.sigmoid_grad(y: oneflow_api.BlobDesc, dy: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc
oneflow.math.sigmoid_v2(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the sigmoid value of Blob.

The equation is:

\[out=\frac{1}{1+e^{-x}}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def sigmoidv2_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.sigmoid_v2(x)

x = np.array([-0.5, 0, 0.5]).astype(np.float32)
out = sigmoidv2_Job(x)

# out [0.37754068 0.5        0.62245935]
oneflow.math.sign(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator returns the sign of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def sign_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.sign(x)


x = np.array([-2, 0, 2]).astype(np.float32)
out = sign_Job(x)

# out [-1.  0.  1.]
oneflow.math.sin(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the sin value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def sin_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.sin(x)


x = np.array([-1/6*np.pi, 0, 1/6*np.pi]).astype(np.float32)
out = sin_Job(x)

# out [-0.5  0.   0.5]
oneflow.math.sinh(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the hyperbolic sine value of Blob.

The equation is:

\[out =\frac{e^x-e^{-x}}{2}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def sinh_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.sinh(x)


x = np.array([-1, 0, 1]).astype(np.float32)
out = sinh_Job(x)

# out [-1.1752012  0.         1.1752012]
oneflow.math.softplus(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the softplus value of Blob.

The equation is:

\[out = log(e^x+1)\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def softplus_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.softplus(x)


x = np.array([-1, 0, 1]).astype(np.float32)
out = softplus_Job(x)

# out [0.31326166 0.6931472  1.3132616 ]
oneflow.math.sqrt(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the sqrt root value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def sqrt_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.sqrt(x)


x = np.array([4, 16, 25]).astype(np.float32)
out = sqrt_Job(x)

# out [2. 4. 5.]
oneflow.math.square(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the square value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def square_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.square(x)


x = np.array([2, 3, 4]).astype(np.float32)
out = square_Job(x)

# out [ 4.  9. 16.]
oneflow.math.squared_difference(x: Union[int, float, oneflow_api.BlobDesc], y: Union[int, float, oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

This op computes \((x - y)^2\) element-wise.

Parameters
  • x (Union[int, float, oneflow_api.BlobDesc]) – A Blob

  • y (Union[int, float, oneflow_api.BlobDesc]) – A Blob with the same type of x

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def squared_difference_Job(x: tp.Numpy.Placeholder((4, )),
                        y: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.squared_difference(x, y)

x = np.array([1, 2, 3, 4], dtype=np.float32)
y = np.array([2, 4, 6, 8], dtype=np.float32)

out = squared_difference_Job(x, y)

# out [ 1.  4.  9. 16.]
oneflow.math.subtract(x: Union[int, float, oneflow_api.BlobDesc], y: Union[int, float, oneflow_api.BlobDesc], name: Optional[str] = None) → oneflow_api.BlobDesc

Compute \(X - Y\) element-wise.

The equation is:

\[out = X - Y\]
Parameters
  • x (Union[int, float, oneflow_api.BlobDesc]) – A Blob.

  • y (Union[int, float, oneflow_api.BlobDesc]) – A Blob has the same type of x.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob after subtracting, has the same type as x.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def subtractJob(x: tp.Numpy.Placeholder((3, )),
                y: tp.Numpy.Placeholder((3, ))
)->tp.Numpy:
    return flow.math.subtract(x, y)

x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([2, 4, 1]).astype(np.float32)
out = subtractJob(x, y)

# out [-1., -2., 2.]
oneflow.math.tan(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the tan value of Blob.

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def tan_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.tan(x)


x = np.array([-1/4*np.pi, 0, 1/4*np.pi]).astype(np.float32)
out = tan_Job(x)

# out [-1.  0.  1.]
oneflow.math.tanh(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the hyperbolic tangent value of Blob.

The equation is:

\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def tanh_Job(x: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.tanh(x)


x = np.array([-1, 0, 1]).astype(np.float32)
out = tanh_Job(x)

# out [-0.7615942  0.         0.7615942]
oneflow.math.tanh_v2(x: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the hyperbolic tangent value of Blob.

The equation is:

\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]
Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

oneflow.math.top_k(input: oneflow_api.BlobDesc, axis: int = -1, k: int = 1, sorted: bool = True, name: Optional[str] = None) → oneflow_api.BlobDesc

Finds the indices of the k largest entries at specified axis, the difference between other framework is that oneflow only return the indices.

Parameters
  • input (oneflow_api.BlobDesc) – The input Blob

  • axis (int, optional) – dimension to be calculated. Defaults to the last dim (-1)

  • k (int, optional) – Number of top elements to look for along the last dimension. Defaults to 1.

  • sorted (bool, optional) – If true the resulting k elements will be sorted by the values in descending order. Defaults to True.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob(dtype=int32) contains the indices of the k largest elements.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def topk_Job(x: tp.Numpy.Placeholder((5, ))
)->tp.Numpy:
    return flow.math.top_k(x, 2)

x = np.array([1, 3, 8, 7, 2], dtype=np.float32)
out = topk_Job(x)

# out [2 3]
oneflow.math.tril(x: oneflow_api.BlobDesc, diagonal: int = 0, fill_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow_api.BlobDesc

Compute lower triangle of an matrix.

Parameters
  • x (oneflow_api.BlobDesc) – Input Blob.

  • diagonal (int) – Diagonal offset, when diagonal > 0, diagonal offset up, otherwise, offset downward.

  • fill_value (Union[int, float]) – The value filled into the upper triangle.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Attention

The dimension of x must greater or equal to 2.

Returns

The lower triangle blob of input.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp
@flow.global_function()
def tril_Job(x: tp.Numpy.Placeholder((4, 4))
)->tp.Numpy:
    return flow.math.tril(x, 0)
x = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
              dtype=np.float32)
out = tril_Job(x).get()

# output [[1, 0, 0, 0],
          [1, 2, 0, 0],
          [1, 2, 3, 0],
          [1, 2, 3, 4]]
oneflow.math.two_stage_reduce_max(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc
oneflow.math.two_stage_reduce_min(x: oneflow_api.BlobDesc, axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, name: Optional[str] = None) → oneflow_api.BlobDesc
oneflow.math.unsorted_batch_segment_sum(data: oneflow_api.BlobDesc, segment_ids: oneflow_api.BlobDesc, num_segments: int, name: Optional[str] = None) → oneflow_api.BlobDesc

It is similar with unsorted_segment_sum, the difference is that unsorted_batch_segment_sum brings a batch axis. We can do the segment sum in different batch of data.

For example, the segment id is like:

[[0 0 0 1 2 2 3 3],
 [0 0 1 1 2 3 3 3]]
Parameters
  • data (oneflow_api.BlobDesc) – Input Blob

  • segment_ids (oneflow_api.BlobDesc) – A Blob with shape (d0, d1). The d0, d1 are the first and second dimension of data.

  • num_segments (int) – num_segments should equal the number of distinct segment IDs.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def unsorted_batch_segment_sum_Job(data: tp.Numpy.Placeholder((3, 4)),
                                segment_ids: tp.Numpy.Placeholder((3, 4), dtype=flow.int32)
)->tp.Numpy:
    return flow.math.unsorted_batch_segment_sum(data, segment_ids, 2)

input_blob = np.array([[1, 2, 3, 4],
                    [1, 2, 3 ,4],
                    [1, 2, 3, 4]]).astype(np.float32)
segment_ids = np.array([[0, 0, 0, 1],
                        [0, 0, 1, 0],
                        [0, 1, 0, 0]]).astype(np.int32)
out = unsorted_batch_segment_sum_Job(input_blob, segment_ids)

# out [[6. 4.]
#      [7. 3.]
#      [8. 2.]]
oneflow.math.unsorted_segment_sum(data: oneflow_api.BlobDesc, segment_ids: oneflow_api.BlobDesc, num_segments: int, axis: int = 0, name: Optional[str] = None) → oneflow_api.BlobDesc

Computes the sum along segments of a Blob.

Parameters
  • data (oneflow_api.BlobDesc) – Input Blob

  • segment_ids (oneflow_api.BlobDesc) – A Blob should be the size of the first dimension, with consecutive IDs in the range 0 to k (k < d0).

  • num_segments (int) – num_segments should equal the number of distinct segment IDs.

  • axis (int, optional) – The axis of data. Defaults to 0.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with the same type of data.

Return type

oneflow_api.BlobDesc

For example:

# Example 1:
import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def unsorted_segment_sumJob(data: tp.Numpy.Placeholder((3, 4)),
                            segment_ids: tp.Numpy.Placeholder((4, ), dtype=flow.int32)
)->tp.Numpy:
    return flow.math.unsorted_segment_sum(data, segment_ids, num_segments=2, axis=1)

input_blob = np.array([[1, 2, 3, 4],
                       [5, 6, 7 ,8],
                       [9, 10, 11, 12]]).astype(np.float32)
segment_ids = np.array([0, 1, 0, 1]).astype(np.int32)
out = unsorted_segment_sumJob(input_blob, segment_ids)

# out [[ 4.  6.]
#      [12. 14.]
#      [20. 22.]]

# Example 2
import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def unsorted_segment_sumJob(data: tp.Numpy.Placeholder((3, 4)),
                            segment_ids: tp.Numpy.Placeholder((3, ), dtype=flow.int32)
)->tp.Numpy:
    return flow.math.unsorted_segment_sum(data, segment_ids, num_segments=2, axis=0)

input_blob = np.array([[1, 2, 3, 4],
                       [5, 6, 7 ,8],
                       [9, 10, 11, 12]]).astype(np.float32)
segment_ids = np.array([0, 1, 0]).astype(np.int32)
out = unsorted_segment_sumJob(input_blob, segment_ids)

#  out [[10. 12. 14. 16.]
#       [ 5.  6.  7.  8.]]
oneflow.math.unsorted_segment_sum_like(data: oneflow_api.BlobDesc, segment_ids: oneflow_api.BlobDesc, like: oneflow_api.BlobDesc, axis: int = 0, name: Optional[str] = None) → oneflow_api.BlobDesc

Computes the sum along segments of a Blob, the output shape is the same as the like Blob.

Parameters
  • data (oneflow_api.BlobDesc) – Input Blob

  • segment_ids (oneflow_api.BlobDesc) – A Blob should be the size of the first dimension, with consecutive IDs in the range 0 to k (k < d0).

  • like (oneflow_api.BlobDesc) – The input Blob which specifies shape

  • axis (int, optional) – The axis of data. Defaults to 0.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def unsorted_segment_sum_like_Job(data: tp.Numpy.Placeholder((3, 4)),
                                segment_ids: tp.Numpy.Placeholder((3, ), dtype=flow.int32),
                                like: tp.Numpy.Placeholder((2, 4), dtype=flow.float32)
)->tp.Numpy:
    return flow.math.unsorted_segment_sum_like(data, segment_ids, like, axis=0)

input_blob = np.array([[1, 2, 3, 4],
                    [5, 6, 7 ,8],
                    [9, 10, 11, 12]]).astype(np.float32)
segment_ids = np.array([0, 1, 0]).astype(np.int32)
like = np.zeros(shape=(2, 4), dtype=np.float32)

out = unsorted_segment_sum_like_Job(input_blob, segment_ids, like)

# out [[10. 12. 14. 16.]
#      [ 5.  6.  7.  8.]]
oneflow.math.xdivy(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the result of \(x/y\)

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def xdivy_Job(x: tp.Numpy.Placeholder((3,)),
                y: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.xdivy(x, y)


x = np.array([4, 3, 5]).astype(np.float32)
y = np.array([3, 2, 2]).astype(np.float32)
out = xdivy_Job(x, y)

# out [1.3333334 1.5       2.5      ]
oneflow.math.xlogy(x: oneflow_api.BlobDesc, y: oneflow_api.BlobDesc, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator computes the result of \(x*log(y)\)

Parameters
  • x (oneflow_api.BlobDesc) – A Blob

  • y (oneflow_api.BlobDesc) – A Blob

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def xlogy_Job(x: tp.Numpy.Placeholder((3,)),
            y: tp.Numpy.Placeholder((3,))
) -> tp.Numpy:
    return flow.math.xlogy(x, y)


x = np.array([2, 2, 2]).astype(np.float32)
y = np.array([4, 8, 16]).astype(np.float32)
out = xlogy_Job(x, y)

# out [2.7725887 4.158883  5.5451775]