oneflow.experimental

Experimental features

class oneflow.experimental.BuiltinOp(op_type_name, op_name=None)
Attr(attr_name, attr_value, attr_type_name=None)

Set value of op’s attribute.

Parameters
  • attr_name (str) – attribute name of op

  • attr_value (Any) – attribute value of op

Raises

ValueError – raised when value is not idential to op’s attribute type.

Returns

[description]

Return type

[type]

Build()

Explicitly complete the construction of the builtin op

Returns

the completed builtin op

Input(input_name, num=1)

Set input blob of op

Parameters
  • input_name (str) – input name of blob

  • num (int, optional) – Defaults to 1.

Returns

self

Output(output_name, num=1)

Set output blob of op

Parameters
  • output_name (str) – name of output blob

  • num (int, optional) – Defaults to 1.

Returns

self

property op

access the builtin op

Returns

the builtin op

class oneflow.experimental.CustomOpModule(op_module_name, module_path='')
build_load()
cpp_def()
cpp_kernel()
gpu_kernel()
py_api()
py_kernel()
class oneflow.experimental.DeprecatedFixedTensorDef(*args, **kwargs)
class oneflow.experimental.DeprecatedMirroredTensorDef(*args, **kwargs)
oneflow.experimental.ExecutionConfig

alias of oneflow.python.framework.function_util.FunctionConfig

oneflow.experimental.FixedTensorDef

alias of oneflow.python.framework.input_blob_def.DeprecatedFixedTensorDef

class oneflow.experimental.FunctionConfig

OneFlow function’s configurations.

property all_reduce_fp16
property all_reduce_group_min_mbyte
property all_reduce_group_num
property all_reduce_group_size_warmup
property all_reduce_lazy_ratio
property allow_cpu_return_op
property concurrency_width
property cudnn_buf_limit_mbyte
property cudnn_conv_enable_pseudo_half
property cudnn_conv_enable_true_half
property cudnn_conv_force_bwd_data_algo
property cudnn_conv_force_bwd_filter_algo
property cudnn_conv_force_fwd_algo
property cudnn_conv_heuristic_search_algo
property cudnn_conv_use_deterministic_algo_only
property default_data_type
property default_distribute_strategy
property default_initializer_conf
property default_logical_view
property default_placement_scope
property disable_all_reduce_sequence
property do_parallel_cast_before_widening_type_cast
property enable_all_reduce_group
property enable_auto_mixed_precision
property enable_cudnn
property enable_cudnn_conv_pseudo_half
property enable_cudnn_fused_normalization_add_relu
property enable_float_compute_for_half_gemm
property enable_fuse_add_to_output
property enable_fuse_cast_scale
property enable_fuse_model_update_ops
property enable_gradients_stats_aggregation
property enable_inplace
property enable_inplace_in_reduce_struct
property enable_keep_header_only
property enable_nccl
property enable_non_distributed_optimizer
property enable_qat
property enable_quantization_aware_training
property enable_reused_mem
property enable_true_half_config_when_conv
property exp_run_conf
property indexed_slices_optimizer_conf
property non_distributed_optimizer_group_size_mbyte
property optimizer_placement_optimization_mode
property optimizer_placement_optimization_threshold
property prune_amp_white_identity_ops
property prune_cast_to_static_shape_ops
property prune_parallel_cast_ops
property qat
property static_mem_alloc_algo_white_list
property static_mem_alloc_policy_white_list
property tensorrt
property train
property use_boxing_v2
property use_nccl_inter_node_communication
property use_tensorrt
property use_xla_jit
oneflow.experimental.MirroredTensorDef

alias of oneflow.python.framework.input_blob_def.DeprecatedMirroredTensorDef

class oneflow.experimental.Model(*args, **kwargs)

A high level API for model training and validation.

configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple.

fit(training_config: Optional[oneflow.python.framework.model.TrainingConfig] = None, validation_config: Optional[oneflow.python.framework.model.ValidationConfig] = None, checkpoint_config: Optional[oneflow.python.framework.model.CheckpointConfig] = None, callbacks: Union[oneflow.python.framework.model.Callback, List[oneflow.python.framework.model.Callback], None] = None, max_steps: int = 100)

Runs the full training and validation routine.

method_overrided(method_name: str = None) → bool
training_step(*args, **kwargs)

Operates on a single batch of data from the training set and return loss.

validation_step(*args, **kwargs)

Operates on a single batch of data from the validation set.

class oneflow.experimental.PlacementSymbol
Containing(self: oneflow._oneflow_internal.PlacementSymbol, arg0: oneflow._oneflow_internal.PlacementSymbol) → bool
property device_tag
property hierarchy
property machine_id2device_id_list
property parallel_conf
property parallel_num
property symbol_id
class oneflow.experimental.Size
count(self: oneflow._oneflow_internal.Size, arg0: int) → int
index(self: oneflow._oneflow_internal.Size, arg0: int, start: int = 0, end: int = 20) → int
numel(self: oneflow._oneflow_internal.Size) → int
class oneflow.experimental.Tensor(*args, dtype=None, device=None, requires_grad=False, placement=None, sbp=None, is_consistent=False, is_lazy=False, data_initializer=None, determining_initializer=None)
add(y)

Computes the addition of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = x + y

For example:

import oneflow.experimental as flow

# element-wise add
x = flow.Tensor(np.random.randn(2,3))
y = flow.Tensor(np.random.randn(2,3))
out = flow.add(x,y).numpy()
print(out.shape) # (2,3)

# scalar add
x = 5
y = flow.Tensor(np.random.randn(2,3))
out = flow.add(x,y).numpy()
print(out.shape) # (2,3)

# broadcast add
x = flow.Tensor(np.random.randn(1,1))
y = flow.Tensor(np.random.randn(2,3))
out = flow.add(x,y).numpy()
print(out.shape) # (2,3)
argmax(dim: int = None, keepdim: bool = False)

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

Parameters
  • input (oneflow.Tensor) – Input Tensor

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

  • keepdim (bool optional) – whether the output tensor has dim retained or not. Ignored if dim=None.

Returns

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

Return type

oneflow.Tensor

For example:

import oneflow as flow
import numpy as np

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

out = flow.argmax(flow.Tensor(x))

# out [2 1]
backward(gradient=None, retain_graph=False, create_graph=False)
cast(dtype)

The operation takes input tensor x and casts it to the output with dtype

Parameters
  • x (oneflow.Tensor) – A Tensor

  • dtype (flow.dtype) – Data type of the output tensor

Returns

A Tensor with specific dtype.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

np_arr = np.random.randn(2, 3, 4, 5).astype(np.float32)
input = flow.Tensor(np_arr, dtype=flow.float32)
output = flow.cast(input, flow.int8)

# equal to np_arr.astype(np.int8)
copy_(**kwargs)
cos()

Returns a new tensor with the cosine of the elements of input. .. math:

\text{out}_{i} = \cos(\text{input}_{i})
Parameters

input (Tensor) –

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np arr = np.array([1.4309, 1.2706, -0.8562, 0.9796]) input = flow.Tensor(arr, dtype=flow.float32) output = flow.cos(input) # [0.13944048 0.29570782 0.6553126 0.5573547 ]

property data
data_ptr()
detach(**kwargs)
determine(determining_initializer=None)
property device
dim(idx)
div(y)

Computes the division of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = \frac{X}{Y}
Parameters
  • x (Union[int, float, flow.Tensor]) –

  • y (Union[int, float, flow.Tensor]) –

For example: .. code-block:: python

import oneflow.experimental as flow

# element-wise divide x = flow.Tensor(np.random.randn(2,3)) y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3)

# scalar divide x = 5 y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3)

# broadcast divide x = flow.Tensor(np.random.randn(1,1)) y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3)

property dtype
element_size()
eq(other)

Computes element-wise equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Args: input (Tensor): the tensor to compare other (Tensor): the tensor to compare

Returns: A boolean tensor that is True where input is equal to other and False elsewhere

For example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
other = flow.Tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

y = flow.eq(input, other)
# [1 1 1 0]
exp()

This operator computes the exponential of Tensor.

The equation is:

\[out = e^x\]
Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import numpy as np
import oneflow as flow

x = flow.Tensor(np.array([1, 2, 3]).astype(np.float32))
y = x.exp().numpy()

# y [ 2.7182817  7.389056  20.085537 ]
expand(expand_size)

This operator expand the input tensor to a larger size.

Passing -1 as the size for a dimension means not changing the size of that dimension.

Tensor can be also expanded to a larger number of dimensions and the new ones will be appended at the front.

For the new dimensions, the size cannot be set to -1.

Parameters
  • x (oneflow.Tensor) – The input Tensor.

  • expand_size (Sequence[int]) – The desired expanded size.

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

x = np.array([[[[0, 1]],
               [[2, 3]],
               [[4, 5]]]]).astype(np.int32)

input = flow.Tensor(x)

out = flow.expand(input, expand_size=[1, 3, 2, 2])

# out shape: [1, 3, 2, 2]
# [[[[0, 1],
#    [0, 1]],
#   [[2, 3],
#    [2, 3]],
#   [[4, 5],
#    [4, 5]]]]
fill_(value)
flatten(start_dim: int = 0, end_dim: int = -1)

Flattens a contiguous range of dims into a tensor.

Parameters
  • start_dim – first dim to flatten (default = 0).

  • end_dim – last dim to flatten (default = -1).

For example:

import oneflow as flow
input = flow.Tensor(32, 1, 5, 5)
output = input.flatten(start_dim=1)
# output = flow.flatten(input, start_dim=1)
output.size()
# out flow.Size([32, 25])
gelu()

Gelu activation operator.

The equation is:

\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]
Parameters

x (oneflow.Tensor) – Input Tensor

Returns

A Tensor.

Return type

oneflow.Tensor

For example:

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

x = np.array([-0.5, 0, 0.5]).astype(np.float32)
input = flow.Tensor(x)
gelu = flow.nn.GELU()

out = gelu(input)
# out [-0.15426877, 0., 0.34573123]
get_device()
property grad
property grad_fn
gt(y)

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

Parameters
  • x (oneflow.Tensor) – A Tensor

  • y (oneflow.Tensor) – A Tensor

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

Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
input2 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)

out = flow.gt(input1, input2).numpy()
# out shape (2, 6, 5, 3)
property is_consistent
property is_cuda
property is_determined
property is_lazy
property is_leaf
kaiming_normal_(a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')
kaiming_uniform_(a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')
log()

Returns a new tensor with the natural logarithm of the elements of input. .. math:

y_{i} = \log_{e} (x_{i})
Parameters

input (Tensor) –

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np arr = np.random.randn(2, 3, 4, 5) input = flow.Tensor(arr, dtype=flow.float32) output = flow.log(input) # equal to np.log(input)

lt(y)

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

Parameters
  • x (oneflow.Tensor) – A Tensor

  • y (oneflow.Tensor) – A Tensor

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

Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32)
input2 = flow.Tensor(np.array([1, 2, 4]).astype(np.float32), dtype=flow.float32)

out = flow.gt(input1, input2).numpy

# out [0 0 1]
masked_fill(mask, value)

Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.

Parameters
  • mask (BoolTensor) –

  • value (float) –

For example:

import oneflow.experimental as flow
import numpy as np

in_arr = np.array(
    [[[-0.13169311,  0.97277078,  1.23305363,  1.56752789],
    [-1.51954275,  1.87629473, -0.53301206,  0.53006478],
    [-1.38244183, -2.63448052,  1.30845795, -0.67144869]],

    [[ 0.41502161,  0.14452418,  0.38968   , -1.76905653],
    [ 0.34675095, -0.7050969 , -0.7647731 , -0.73233418],
    [-1.90089858,  0.01262963,  0.74693893,  0.57132389]]]
)

fill_value = 8.7654321 # random value e.g. -1e9 3.1415
input = flow.Tensor(in_arr, dtype=flow.float32)
mask = flow.Tensor((in_arr > 0).astype(np.int8), dtype=flow.int)

output = input.masked_fill(mask, fill_value)
#  [[[-0.13169311  8.765432    8.765432    8.765432  ]
#   [-1.5195427   8.765432   -0.53301203  8.765432  ]
#   [-1.3824419  -2.6344805   8.765432   -0.6714487 ]]

#  [[ 8.765432    8.765432    8.765432   -1.7690566 ]
#   [ 8.765432   -0.7050969  -0.7647731  -0.7323342 ]
#   [-1.9008986   8.765432    8.765432    8.765432  ]]]
matmul(b)

This operator applies matrix multiplication to two Tensor.

Parameters
Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.random.randn(2, 6), dtype=flow.float32)
input2 = flow.Tensor(np.random.randn(6, 5), dtype=flow.float32)
of_out = flow.matmul(input1, input2)

# of_out.shape (2, 5)
mean(dim=None, keepdim=False)

Computes the mean of row of elements in a tensor in the given axis, if the axis is None, mean of all elements will be caculated.

For example:

import oneflow.experimental as flow

input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
out = flow.mean(input)
# out: [3.5]
print(out.numpy())

input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
out = flow.mean(input, axis=0)
# out: [2.5 3.5 4.5]
print(out.numpy())

input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
out = flow.mean(input, axis=1)
# out: [ 2. 5.]
print(out.numpy())
mul(y)

Computes the multiplication of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = x \times y

For example:

import oneflow.experimental as flow

# element-wise multiply
x = flow.Tensor(np.random.randn(2,3))
y = flow.Tensor(np.random.randn(2,3))
out = flow.mul(x,y).numpy()
print(out.shape) # (2,3)

# scalar mutiply
x = 5
y = flow.Tensor(np.random.randn(2,3))
out = flow.mul(x,y).numpy()
print(out.shape) # (2,3)

# broadcast mutiply
x = flow.Tensor(np.random.randn(1,1))
y = flow.Tensor(np.random.randn(2,3))
out = flow.mul(x,y).numpy()
print(out.shape) # (2,3)
property ndim
ndimension()
negative()

This operator computes the negative value of Tensor.

Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(
    np.array([1.0, -1.0, 2.3]).astype(np.float32), dtype=flow.float32
)
out = flow.negative(input).numpy()

# out [-1.0, 1.0, -2.3]
nelemenet()
normal_(mean=0, std=1)
numpy(**kwargs)
property placement
pow(exponent)

Takes the power of each element in input with exponent and returns a tensor with the result. exponent can be either a single float number or a single int number.

For example:

import oneflow.experimental as flow
import numpy as np

x = flow.Tensor(np.array([1, 2, 3, 4, 5, 6]))
out = flow.pow(x, 2).numpy()
print(out) # [1, 4, 9, 16, 25, 36]
reciprocal()

Computes the safe reciprocal of x. If x is zero, the reciprocal will be also set to zero.

For example:

import oneflow.experimental as flow
x = flow.Tensor(np.array([[1, 2, 3], [4, 5, 6]]))
out = flow.reciprocal()(x)
# out [[1.         0.5        0.33333334]
       [0.25       0.2        0.16666667]]
repeat(sizes)

This operator repeat the input tensor to a larger size along the specified dimensions.

Parameters
  • x (oneflow.Tensor) – The input Tensor.

  • size (Sequence[int]) – The number of times to repeat this tensor along each dimension

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

x = np.array([[[[0, 1]],
               [[2, 3]],
               [[4, 5]]]]).astype(np.int32)

input = flow.Tensor(x)

out = flow.repeat(input, sizes=[1, 1, 2, 2]).numpy()

# out shape: [1, 3, 2, 4]
# [[[[0. 1. 0. 1.]
# [0. 1. 0. 1.]]

# [[2. 3. 2. 3.]
# [2. 3. 2. 3.]]

# [[4. 5. 4. 5.]
# [4. 5. 4. 5.]]]]
property requires_grad
requires_grad_(requires_grad=True)
reshape(shape: Sequence[int] = None)

This operator reshapes a Tensor.

We can set one dimension in shape as -1, the operator will infer the complete shape.

Parameters
  • x – A Tensor.

  • shape – Shape of the output tensor.

Returns

A Tensor, has the same type as x.

For example:

import numpy as np
import oneflow.experimental as flow

x = np.array(
    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
).astype(np.float32)
input = flow.Tensor(x)

y = flow.reshape(input, shape=[2, 2, 2, -1]).numpy().shape

# y (2, 2, 2, 2)
retain_grad()
property sbp
set_data_initializer(data_initializer)
set_is_consistent(is_consistent)
set_is_lazy(is_lazy)
set_placement(placement)
set_sbp(sbp)
property shape
sigmoid()

Applies the element-wise function:

\[\text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}\]
Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Output: \((N, *)\), same shape as the input

For example:

import oneflow.experimental as flow
import numpy as np

x = flow.Tensor(
    np.array(
        [
            [0.81733328, 0.43621480, 0.10351428],
            [-1.15555191, -0.67776406, 0.27372134],
        ]
    )
)
y = x.sigmoid()
# [[0.69366997, 0.60735673, 0.52585548],
# [0.23947647, 0.33676055, 0.56800622]]
sin()

Returns a new tensor with the sine of the elements of input. .. math:

\text{out}_{i} = \sin(\text{input}_{i})
Parameters

input (Tensor) –

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np arr = np.array([-0.5461, 0.1347, -2.7266, -0.2746]) input = flow.Tensor(arr, dtype=flow.float32) output = flow.sin(input) # [-0.51935846 0.13429303 -0.40318328 -0.27116194]

size()
softmax(dim=None)

Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.

Softmax is defined as:

\[\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]

When the input Tensor is a sparse tensor then the unspecifed values are treated as -inf.

Shape:
  • Input: \((*)\) where * means, any number of additional dimensions

  • Output: \((*)\), same shape as the input

Returns

a Tensor of the same dimension and shape as the input with values in the range [0, 1]

Parameters

dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

For example:

import oneflow as flow
import numpy as np

m = flow.nn.Softmax(dim = 2)
x = flow.Tensor(
    np.array(
        [[[[-0.46716809,  0.40112534,  0.61984003],
        [-1.31244969, -0.42528763,  1.47953856]]],

        [[[ 1.02978742, -0.49383053,  1.88214159],
        [ 1.35351622, -1.46251285, -1.40751374]]]]
    )
)
y = m(x)
# [[[[0.6995764  0.6955959  0.29740235]
# [0.3004236  0.30440408 0.7025977 ]]]

# [[[0.4197673  0.7248568  0.96407217]
# [0.58023274 0.27514324 0.03592779]]]]
sqrt()

Returns a new tensor with the square-root of the elements of input.

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
Parameters

input

import oneflow.experimental as flow
import numpy as np

arr = np.random.randn(3, 2, 5, 7)
input = flow.Tensor(arr)
output = flow.sqrt(input)
# output equal to np.sqrt(arr)
square()

Returns a new tensor with the square of the elements of input.

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
Parameters

input

import oneflow.experimental as flow
import numpy as np

arr = np.random.randn(3, 2, 5, 7)
input = flow.Tensor(arr)
output = flow.square(input)
# output equal to np.square(arr)
squeeze(axis: Optional[Sequence[int]] = None)

This operator removes the specified dimention which size is 1 of the input Tensor. If the axis is not specified, this operator will remove all the dimention which size is 1 of the input Tensor.

The amount of element in return value is the same as Tensor input.

Parameters
  • input (oneflow.Tensor) – The input Tensor.

  • axis (Optional[Sequence[int]], optional) – The axis. Defaults to None.

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

Example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(np.array([[[[1, 1, 1]]]]).astype(np.int32))
out = flow.squeeze(input, axis=[1, 2]).numpy().shape

# out.shape (1, 3)
std(dim, unbiased=True, keepdim=False)

Returns the standard-deviation of each row of the input tensor in the dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Parameters
  • input (Tensor) –

  • (int or tuple of python (dim) – ints) – the dimension or dimensions to reduce.

  • unbiased (bool) –

  • keepdim (bool) –

For example:

import oneflow.experimental as flow
import numpy as np

arr = np.random.randn(2, 3, 4, 5)
input = flow.Tensor(arr)
output = flow.std(input, dim=2)

# equal to numpy np.std(arr, axis=2)
sub(y)

Computes the subtraction of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = x - y

For example:

import oneflow.experimental as flow

# element-wise subtract
x = flow.Tensor(np.random.randn(2,3))
y = flow.Tensor(np.random.randn(2,3))
out = flow.sub(x,y).numpy()
print(out.shape) # (2,3)

# scalar subtract
x = 5
y = flow.Tensor(np.random.randn(2,3))
out = flow.sub(x,y).numpy()
print(out.shape) # (2,3)

# broadcast subtract
x = flow.Tensor(np.random.randn(1,1))
y = flow.Tensor(np.random.randn(2,3))
out = flow.sub(x,y).numpy()
print(out.shape) # (2,3)
sum(dim=None, keepdims=False)

Computes the sum of row of elements in a tensor in the given axis, if the axis is None, sum of all elements will be caculated. For example:

import oneflow.experimental as flow
input = flow.Tensor(np.random.randn(4, 5, 6), dtype=flow.float32)
of_out = flow.sum(input, dim=(2,1))
tanh()

This operator computes the hyperbolic tangent value of Tensor.

The equation is:

\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]
Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow as flow
import numpy as np

x = np.array([-1, 0, 1]).astype(np.float32)
input = flow.Tensor(x)
tanh = flow.nn.Tanh()
out = tanh(input).numpy()

# out [-0.7615942  0.         0.7615942]
tolist()
transpose(perm: Sequence[int] = None)

This operator transposes the specified axis of input Tensor. :param tensor: The input tensor. :type tensor: oneflow.Tensor :param perm: The list of dimension permutation. Defaults to None. :type perm: Sequence[int], optional

Returns

A transposed tensor.

Return type

oneflow.Tensor

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np

input = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) out = flow.transpose(input, perm=(0, 2, 3, 1))

# out.shape (2, 5, 3, 6)

uniform_(a=0, b=1)
unsqueeze(dim)

Returns a new tensor with a dimension of size one inserted at the specified position.

The returned tensor shares the same underlying data with this tensor.

A dim value within the range [-input.ndimension() - 1, input.ndimension() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.ndimension() + 1.

Parameters
  • input (Tensor) –

  • dim (int) – the index at which to insert the singleton dimension

For example:

import numpy as np
import oneflow as flow

x = flow.Tensor(np.random.rand(2, 3, 4))
y = x.unsqueeze(2)
where(x, y)

Return a tensor of elements selected from either x or y, depending on condition. If the element in condition is larger than 0,

it will take the x element, else it will take the y element

The tensors condition, x, y must be broadcastable. it will take the x element, else it will take the y element. :param condition: When 1 (nonzero), yield x, otherwise yield y :type condition: IntTensor :param x: value (if :attr:x is a scalar) or values selected at indices

where condition is True

Parameters

y (Tensor or Scalar) – value (if :attr:x is a scalar) or values selected at indices where condition is False

Returns

A tensor of shape equal to the broadcasted shape of condition, x, y

Return type

Tensor

For example: .. code-block:: python

import flow.experimental as flow

x = flow.Tensor(

np.array([[-0.4620, 0.3139], [0.3898, -0.7197], [0.0478, -0.1657]]), dtype=flow.float32,

) y = flow.Tensor(np.ones(shape=(3, 2)), dtype=flow.float32) condition = flow.Tensor(np.array([[0, 1], [1, 0], [1, 0]]), dtype=flow.int32) of_out = condition.where(x, y) # of_out # [[1. 0.3139] # [0.3898 1. ] # [0.0478 1. ]]

xavier_normal_(gain=1.0, *, data_format='NCHW')
xavier_uniform_(gain=1.0, *, data_format='NCHW')
oneflow.experimental.acc(one: oneflow._oneflow_internal.BlobDesc, max_acc_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.add(x, y)

Computes the addition of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = x + y

For example:

import oneflow.experimental as flow

# element-wise add
x = flow.Tensor(np.random.randn(2,3))
y = flow.Tensor(np.random.randn(2,3))
out = flow.add(x,y).numpy()
print(out.shape) # (2,3)

# scalar add
x = 5
y = flow.Tensor(np.random.randn(2,3))
out = flow.add(x,y).numpy()
print(out.shape) # (2,3)

# broadcast add
x = flow.Tensor(np.random.randn(1,1))
y = flow.Tensor(np.random.randn(2,3))
out = flow.add(x,y).numpy()
print(out.shape) # (2,3)
oneflow.experimental.amp_white_identity(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.arange(start=1, end=1, step=1)

Returns a 1-D tensor of size \(\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1\) with values from start to end with step step. Step is the gap between two values in the tensor.

\[\]

text{out}_{i+1} = text{out}_i + text{step}.

Args: start (float): the starting value for the set of points. Default: 0. end (float): the ending value for the set of points step (float): the gap between each pair of adjacent points. Default: 1.

Keyword args: dtype: If dtype is not given, the dtype is inferred to be the default dtype.

For example:

import oneflow as flow
y = flow.arange(0, 5)
# [0, 1, 2, 3, 4]
oneflow.experimental.argmax(input, dim: int = None, keepdim: bool = False)

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

Parameters
  • input (oneflow.Tensor) – Input Tensor

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

  • keepdim (bool optional) – whether the output tensor has dim retained or not. Ignored if dim=None.

Returns

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

Return type

oneflow.Tensor

For example:

import oneflow as flow
import numpy as np

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

out = flow.argmax(flow.Tensor(x))

# out [2 1]
oneflow.experimental.argwhere(condition: oneflow._oneflow_internal.BlobDesc, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator finds the indices of input Blob condition elements that are non-zero. It returns a List. Each element in the output is a coordinate that points to a non-zero element in the condition.

Parameters
  • condition (oneflow._oneflow_internal.BlobDesc) – The input Blob.

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

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

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def argwhere_Job(x: tp.Numpy.Placeholder(shape=(2, 3), dtype=flow.float32),
) -> tp.ListNumpy:
    return flow.argwhere(x)


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

# out [array([[0, 1],
#             [1, 0],
#             [1, 2]], dtype=int32)]
oneflow.experimental.assign(ref, value, dtype=None, name=None)
oneflow.experimental.broadcast_like(x, like_tensor, broadcast_axes: None)
oneflow.experimental.broadcast_to_compatible_with(x: oneflow._oneflow_internal.BlobDesc, compatible: Sequence[oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

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

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

  • compatible (Sequence[oneflow._oneflow_internal.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._oneflow_internal.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.experimental.builtin_op

alias of oneflow.python.ops.builtin_ops.BuiltinOp

oneflow.experimental.cast(x, dtype)

The operation takes input tensor x and casts it to the output with dtype

Parameters
  • x (oneflow.Tensor) – A Tensor

  • dtype (flow.dtype) – Data type of the output tensor

Returns

A Tensor with specific dtype.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

np_arr = np.random.randn(2, 3, 4, 5).astype(np.float32)
input = flow.Tensor(np_arr, dtype=flow.float32)
output = flow.cast(input, flow.int8)

# equal to np_arr.astype(np.int8)
oneflow.experimental.cast_to_current_logical_view(x: oneflow._oneflow_internal.BlobDesc) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.cast_to_static_shape(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns a Blob that has identical content and data type to input Blob, and whose shape is converted from dynamic to static

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob which has dynamic shape.

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

Returns

The result Blob which is identical to input blob but has static shape.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def cast_to_static_shape_func(
    x: tp.ListNumpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    return flow.cast_to_static_shape(x)

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

out = cast_to_static_shape_func(x)

# out [[1 1 1]
#      [2 2 2]
#      [3 3 3]]
oneflow.experimental.cat(inputs, dim=0)

Concatenate two or more Tensor s at specified axis.

Analogous to numpy.concatenate

Parameters
  • inputs – a list of Tensor

  • dim – a int.

Returns

A Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
input2 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
input3 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)

out = flow.cat([input1, input2, input3], dim=1)

# out.shape (2, 18, 5, 3)
oneflow.experimental.categorical_ordinal_encode(table: oneflow._oneflow_internal.BlobDesc, size: oneflow._oneflow_internal.BlobDesc, input_tensor: oneflow._oneflow_internal.BlobDesc, hash_precomputed: bool = True, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator maintains a hash table to encode the categorical ordinal Blob. It converts a discrete input value into a continuous integer ID.

Parameters
  • table (oneflow._oneflow_internal.BlobDesc) – The hash table, you can assign it as a variable.

  • size (oneflow._oneflow_internal.BlobDesc) – The size of hash table.

  • input_tensor (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • hash_precomputed (bool, optional) – We currently only support the ‘True’ mode. The internal hash value will no longer be computed. Defaults to True.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def categorical_ordinal_encode_Job(x: tp.Numpy.Placeholder((3, 3), dtype=flow.int32)
) -> tp.Numpy:
    dtype = x.dtype
    with flow.scope.namespace("categorical_ordinal_encode"):
        table = flow.get_variable(
            name="Table",
            shape=(16,),
            dtype=dtype,
            initializer=flow.constant_initializer(0, dtype=dtype),
            trainable=False,
            reuse=False,
        )
        size = flow.get_variable(
            name="Size",
            shape=(1,),
            dtype=dtype,
            initializer=flow.constant_initializer(0, dtype=dtype),
            trainable=False,
            reuse=False,
        )
        return flow.categorical_ordinal_encode(
            table=table, size=size, input_tensor=x, name="Encode",
        )

x = np.array([[7, 0, 2],
            [1, 7, 2],
            [0, 1, 7]]).astype(np.int32)

out = categorical_ordinal_encode_Job(x)

# out [[1 0 2]
#      [3 1 2]
#      [0 3 1]]
oneflow.experimental.clamp(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.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._oneflow_internal.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._oneflow_internal.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.experimental.clear_default_session() → None

Clear the default session. All compiled OneFlow functions will be deleted.

oneflow.experimental.clip(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.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._oneflow_internal.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._oneflow_internal.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.experimental.clip_by_scalar(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.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._oneflow_internal.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._oneflow_internal.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.experimental.clip_by_value(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.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._oneflow_internal.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._oneflow_internal.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.experimental.combined_margin_loss(x: oneflow._oneflow_internal.BlobDesc, label: oneflow._oneflow_internal.BlobDesc, m1: float = 1, m2: float = 0, m3: float = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.concat(inputs: Optional[Sequence[oneflow._oneflow_internal.BlobDesc]] = None, axis: int = 0, max_dim_size: Optional[int] = None, name: Optional[str] = None, values: Optional[Sequence[oneflow._oneflow_internal.BlobDesc]] = None) → oneflow._oneflow_internal.BlobDesc

Concatenate two or more Blob s at specified axis.

Analogous to numpy.concatenate

Parameters
  • inputs – a list of Blob

  • axis – a int. 0 by default

  • max_dim_size – hint of max dimension size along the given axis

  • name – name of this operator. None by default

  • values – deprecated param, use inputs instead

Returns

A Blob

For example:

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


@flow.global_function()
def concat_Job() -> tp.Numpy:
    constant_blob_1 = flow.constant(value=1.5,
                                    shape=(1, 3, 3, 4),
                                    dtype=flow.float,
                                    name="blob1")
    constant_blob_2 = flow.constant(value=2.5,
                                    shape=(1, 3, 3, 4),
                                    dtype=flow.float,
                                    name="blob2")
    return flow.concat(inputs=[constant_blob_1, constant_blob_2],
                    axis=3)


out = concat_Job()

# out.shape (1, 3, 3, 8)
oneflow.experimental.consistent_user_op_builder(op_name)
oneflow.experimental.consistent_user_op_module_builder(op_type_name)
oneflow.experimental.constant(value: Union[int, float], dtype: Optional[oneflow._oneflow_internal.dtype] = None, shape: Optional[Sequence[int]] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a constant Blob.

Parameters
  • value (Union[int, float]) – The constant value of Blob.

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

  • shape (Optional[Sequence[int]], optional) – The shape of Blob. Defaults to None.

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

Raises

NotImplementedError – The data type of value should be int or float.

Returns

The result blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def constant_Job() -> tp.Numpy:
    constant_blob = flow.constant(value=1.5,
                                shape=(1, 3, 3),
                                dtype=flow.float)
    return constant_blob


out = constant_Job()

# out [[[1.5 1.5 1.5]
#       [1.5 1.5 1.5]
#       [1.5 1.5 1.5]]]
oneflow.experimental.constant_initializer(value: float = 0, dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blob with constant values.

Parameters
  • value (float, optional) – A Python scalar. All elements of the initialized variable . Defaults to 0.

  • dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Raises

NotImplementedError – Do not support such data type.

Returns

An InitializerConf object.

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def constant_Job() -> None:
    init = flow.constant_initializer(2.5)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
constant_Job()

# out [2.5 2.5 2.5]

Example 2:

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


@flow.global_function()
def conv2d_constant_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.constant_initializer(0.01)
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_constant_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.constant_like(like: oneflow._oneflow_internal.BlobDesc, value: Union[int, float], dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a constant Blob that has the same shape as like.

Parameters
  • like (oneflow._oneflow_internal.BlobDesc) – A Blob.

  • value (Union[int, float]) – The constant value of Blob.

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

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

Raises

NotImplementedError – The data type of value should be int or float.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def constant_like_Job() -> tp.Numpy:
    constant_blob = flow.constant(value=1.5,
                                shape=(1, 3, 3),
                                dtype=flow.float)
    constant_like_blob = flow.constant_like(like=constant_blob,
                                            value=5.5,
                                            dtype=flow.float)
    return constant_like_blob


out = constant_like_Job()

# out [[[5.5 5.5 5.5]
#       [5.5 5.5 5.5]
#       [5.5 5.5 5.5]]]
oneflow.experimental.constant_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], constant_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using an input constant value.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

  • constant_value (Union[int, float]) – The constant value used for padding. Defaults to Zero.

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

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3), const_value)
) -> tp.Numpy:
    return flow.constant_pad2d(x, padding=[2, 2, 1, 1], const_value)


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
const_value = 1.5
out = pad_Job(x, const_value)

# out [[[[ 1.5  1.5  1.5  1.5  1.5  1.5  1.5]
#    [ 1.5  1.5  0.  1.  2.  1.5  1.5]
#    [ 1.5  1.5  3.  4.  5.  1.5  1.5]
#    [ 1.5  1.5  6.  7.  8.  1.5  1.5]
#    [ 1.5  1.5  1.5  1.5  1.5  1.5  1.5]]

#   [[ 1.5  1.5  1.5  1.5  1.5  1.5  1.5.]
#    [ 1.5  1.5  9.  10.  11.  1.5  1.5]
#    [ 1.5  1.5  12.  13.  14.  1.5  1.5]
#    [ 1.5  1.5  15.  16.  17.  1.5  1.5]
#    [ 1.5  1.5  1.5  1.5  1.5  1.5  1.5]]]]
oneflow.experimental.constant_scalar(value: Union[int, float], dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a constant scalar Blob.

Parameters
  • value (Union[int, float]) – The constant value of Blob.

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

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

Returns

The result blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def constant_scalar_Job() -> tp.Numpy:
    constant_scalar = flow.constant_scalar(value=2.5,
                                        dtype=flow.float)
    return constant_scalar


out = constant_scalar_Job()

# out [2.5]
oneflow.experimental.convert_oneflow_dtype_to_numpy_dtype(oneflow_dtype: oneflow._oneflow_internal.dtype)
oneflow.experimental.cos(tensor)

Returns a new tensor with the cosine of the elements of input. .. math:

\text{out}_{i} = \cos(\text{input}_{i})
Parameters

input (Tensor) –

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np arr = np.array([1.4309, 1.2706, -0.8562, 0.9796]) input = flow.Tensor(arr, dtype=flow.float32) output = flow.cos(input) # [0.13944048 0.29570782 0.6553126 0.5573547 ]

oneflow.experimental.count_not_finite(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.ctc_loss(log_probs: oneflow._oneflow_internal.BlobDesc, targets: oneflow._oneflow_internal.BlobDesc, input_lengths: oneflow._oneflow_internal.BlobDesc, target_lengths: oneflow._oneflow_internal.BlobDesc, blank: int = 0, reduction: str = 'mean', zero_infinity: bool = False, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Computes the CTC(Connectionist Temporal Classification) loss. This operator implements the CTC loss as presented in (Graves et al., 2006).

Parameters
  • log_probs (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [input_length, batch_size, num_labels]. The logarithmized probabilities of the outputs (e.g. obtained with flow.nn.logsoftmax()).

  • targets (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [batch_size, max_target_length]. It represent the target sequences. Each element in the target sequence is a class index. And the target index cannot be blank (default=0).

  • input_lengths (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [batch_size]. It represent the lengths of the inputs. And the lengths are specified for each sequence to achieve masking under the assumption that sequences are padded to equal lengths.

  • target_lengths (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [batch_size]. It represent lengths of the targets. Lengths are specified for each sequence to achieve masking under the assumption that sequences are padded to equal lengths.

  • blank (int, optional) – Blank label. Defaults to 0.

  • reduction (str, optional) – The reduce type, it can be the one of “none”, “mean”, “sum”. “none”: no reduction will be applied, “mean”: the output losses will be divided by the target lengths and then the mean over the batch is taken, “sum”: the output will be summed. Defaults to “mean”.

  • zero_infinity (bool, optional) – Whether to zero infinite losses and the associated gradients. Infinite losses mainly occur when the inputs are too short to be aligned to the targets. Defaults to False.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def ctc_loss_job(
    log_probs: tp.Numpy.Placeholder(shape=(5, 2, 3)),
    targets: tp.Numpy.Placeholder(shape=(2, 3), dtype=flow.int32),
    input_lengths: tp.Numpy.Placeholder(shape=(2,), dtype=flow.int32),
    target_lengths: tp.Numpy.Placeholder(shape=(2,), dtype=flow.int32),
) -> tp.Numpy:
    loss = flow.ctc_loss(
        log_probs, targets, input_lengths, target_lengths, blank=0, reduction="none"
    )
    return loss


log_probs = np.array(
    [
        [[-1.1031, -0.7998, -1.5200], [-0.9808, -1.1363, -1.1908]],
        [[-1.2258, -1.0665, -1.0153], [-1.1135, -1.2331, -0.9671]],
        [[-1.3348, -0.6611, -1.5118], [-0.9823, -1.2355, -1.0941]],
        [[-1.3850, -1.3273, -0.7247], [-0.8235, -1.4783, -1.0994]],
        [[-0.9049, -0.8867, -1.6962], [-1.4938, -1.3630, -0.6547]],
    ]
).astype(np.float32)
targets = np.array([[1, 2, 2], [1, 2, 2]]).astype("int32")
input_lengths = np.array([5, 5]).astype("int32")
target_lengths = np.array([3, 3]).astype("int32")
loss = ctc_loss_job(log_probs, targets, input_lengths, target_lengths)

# loss [3.918017 2.907672]
oneflow.experimental.current_global_function_desc() → oneflow.python.framework.function_desc.FunctionDesc
oneflow.experimental.current_machine_id()

Get machine id of current machine/node

Returns

[description]

Return type

[type]

oneflow.experimental.current_resource() → oneflow.core.job.resource_pb2.Resource
Get current resources, such as:machine nums, cpu/gpu device nums,

epoch network threed num, rdma params…

Returns

[description]

Return type

resource_util.Resource

oneflow.experimental.current_scope()

Return current scope

oneflow.experimental.custom_op_module

alias of oneflow.python.ops.util.custom_op_module.CustomOpModule

class oneflow.experimental.device
property index
property type
oneflow.experimental.diag(input: oneflow._oneflow_internal.BlobDesc, diagonal: Optional[int] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator compute diagonal.

If input is a vector, then returns a square matrix with the elements of input as the diagonal. If input is a matrix, then returns a vector with the diagonal elements of input. :param input: The input Blob. :type input: remote_blob_util.BlobDef :param diagonal: The diagonal to consider. If diagonal = 0, it is the main diagonal. If diagonal > 0, it is above the main diagonal. If diagonal < 0, it is below the main diagonal. Defaults to 0. :type diagonal: Optional[int], 0

Returns

The result Blob.

Return type

remote_blob_util.BlobDef

For example:

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


@flow.global_function()
def Diag_Job(input: tp.Numpy.Placeholder((3, 3), dtype=flow.float32),) -> tp.Numpy:
    return flow.diag(input)


input = np.array([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0],
                  [7.0, 8.0, 9.0],], dtype=np.float32)
out = Diag_Job(input)
# out [1. 5. 9.]
oneflow.experimental.dim_gather(input: oneflow._oneflow_internal.BlobDesc, dim: int, index: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator gathers elements from input according to index along with the axis dim.

Take a 3-D blob as example, the output is specified by:

output[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
output[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
output[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2

The shape of input and index should be the same except in the dim dimension.

That is, if input is a n-dimension blob with shape \((x_0, x_1, \dots, x_{i-1}, x_i, x_{i+1}, \dots, x_n)\), and dim = i, then index must be a n-dimension blob with shape \((x_0, x_1, \dots, x_{i-1}, k, x_{i+1}, \dots, x_n)\) where \(k \geq 1\).

The return Blob output will have the same shape with index.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input blob

  • dim (int) – The axis along which to index

  • index (oneflow._oneflow_internal.BlobDesc) – The index blob of elements to gather

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

Returns

The elements gathered from input will be returned as the output Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def dim_gather_Job(input: tp.Numpy.Placeholder((2, 2), dtype=flow.float64),
                index:tp.Numpy.Placeholder((2, 2), dtype=flow.int32))->tp.Numpy:
    return flow.dim_gather(input, 1, index)

input = np.array([[1, 2], [3, 4]]).astype(np.float64)
index = np.array([[1, 0], [0, 1]]).astype(np.int32)

out = dim_gather_Job(input, index)
# output
# [[2. 1.]
#  [3. 4.]]
oneflow.experimental.distributed_partial_fc_sample(weight: oneflow._oneflow_internal.BlobDesc, label: oneflow._oneflow_internal.BlobDesc, num_sample: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.div(x, y)

Computes the division of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = \frac{X}{Y}
Parameters
  • x (Union[int, float, flow.Tensor]) –

  • y (Union[int, float, flow.Tensor]) –

For example: .. code-block:: python

import oneflow.experimental as flow

# element-wise divide x = flow.Tensor(np.random.randn(2,3)) y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3)

# scalar divide x = 5 y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3)

# broadcast divide x = flow.Tensor(np.random.randn(1,1)) y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3)

class oneflow.experimental.dtype
property bytes
property is_complex
property is_floating_point
property is_signed
oneflow.experimental.dtypes()
oneflow.experimental.dynamic_reshape(x: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reshapes a dynamic blob.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • shape (Sequence[int]) – The output shape.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def dynamic_reshape_Job(x: tp.Numpy.Placeholder(shape=(1, 3, 64, 64), dtype=flow.float32)
) -> tp.Numpy:
    reshape_out1 = flow.dynamic_reshape(x, (-1, 64))
    variable1 = flow.get_variable(
        "var1",
        shape=(64, 32),
        dtype=flow.float,
        initializer=flow.random_uniform_initializer(minval=-10, maxval=10),
        trainable=True,
    )
    matmul_tensor = flow.matmul(reshape_out1, variable1)
    reshape_out2 = flow.dynamic_reshape(matmul_tensor, (-1, 8, 4))
    return reshape_out2

x = np.random.rand(1, 3, 64, 64).astype(np.float32)
out = dynamic_reshape_Job(x)

# out.shape (192, 8, 4)
oneflow.experimental.eager_assign_121(ref, value)
oneflow.experimental.eager_execution_enabled() → bool

Get current setting of the job, if enable eager execution mode ,then return True

Returns

[description]

Return type

bool

oneflow.experimental.eager_nccl_all_reduce(x: oneflow._oneflow_internal.BlobDesc, parallel_conf: str, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.elem_cnt(inputs: oneflow._oneflow_internal.BlobDesc, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns the amount of elements in input Blob.

Parameters
  • inputs (oneflow._oneflow_internal.BlobDesc) – The input Blob.

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

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

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def elem_cnt_Job(x: tp.Numpy.Placeholder(shape=(5, ), dtype=flow.float32),
) -> tp.ListNumpy:
    return flow.elem_cnt(inputs=x, dtype=flow.int32)

x = np.array([10, 20, -30, 40, 50]).astype(np.float32)
out = elem_cnt_Job(x)

# [array([5], dtype=int32)]
oneflow.experimental.empty_initializer(dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf
oneflow.experimental.enable_eager_execution(val: bool = True) → None

If True, job will execute in eager mode, else use lazy mode(static graph).

Parameters

val (bool, optional) – Whether eager execution or not. Defaults to True.

oneflow.experimental.enable_typing_check(val: bool = True) → None

enable typing check for global_function

oneflow.experimental.eq(input, other)

Computes element-wise equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Args: input (Tensor): the tensor to compare other (Tensor): the tensor to compare

Returns: A boolean tensor that is True where input is equal to other and False elsewhere

For example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
other = flow.Tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

y = flow.eq(input, other)
# [1 1 1 0]
oneflow.experimental.equal(input, other)

Computes element-wise equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Args: input (Tensor): the tensor to compare other (Tensor): the tensor to compare

Returns: A boolean tensor that is True where input is equal to other and False elsewhere

For example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
other = flow.Tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

y = flow.eq(input, other)
# [1 1 1 0]
oneflow.experimental.exp(x)

This operator computes the exponential of Tensor.

The equation is:

\[out = e^x\]
Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import numpy as np
import oneflow as flow

x = flow.Tensor(np.array([1, 2, 3]).astype(np.float32))
y = x.exp().numpy()

# y [ 2.7182817  7.389056  20.085537 ]
oneflow.experimental.expand(x, expand_size)

This operator expand the input tensor to a larger size.

Passing -1 as the size for a dimension means not changing the size of that dimension.

Tensor can be also expanded to a larger number of dimensions and the new ones will be appended at the front.

For the new dimensions, the size cannot be set to -1.

Parameters
  • x (oneflow.Tensor) – The input Tensor.

  • expand_size (Sequence[int]) – The desired expanded size.

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

x = np.array([[[[0, 1]],
               [[2, 3]],
               [[4, 5]]]]).astype(np.int32)

input = flow.Tensor(x)

out = flow.expand(input, expand_size=[1, 3, 2, 2])

# out shape: [1, 3, 2, 2]
# [[[[0, 1],
#    [0, 1]],
#   [[2, 3],
#    [2, 3]],
#   [[4, 5],
#    [4, 5]]]]
oneflow.experimental.expand_dims(input: oneflow._oneflow_internal.BlobDesc, axis: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator inserts a dimention at the specified axis in the input Blob. The size of new dimension can only be 1, and the amount of element in return value is the same as Blob input.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • axis (int) – The specified dimension index.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def expand_dim_Job(x: tp.Numpy.Placeholder(shape=(1, 3, 3), dtype=flow.int32),
) -> tp.Numpy:
    return flow.expand_dims(input=x,
                            axis=2)


x = np.array([[[1, 1, 1],
            [1, 1, 1],
            [1, 1, 1]]]).astype(np.int32)
out = expand_dim_Job(x)

# out.shape (1, 3, 1, 3)
oneflow.experimental.find_or_create_module(module_name: str, create: Callable[[], None], reuse: bool = False)
oneflow.experimental.flatten(input, start_dim: int = 0, end_dim: int = -1)

Flattens a contiguous range of dims into a tensor.

Parameters
  • start_dim – first dim to flatten (default = 0).

  • end_dim – last dim to flatten (default = -1).

For example:

import oneflow as flow
input = flow.Tensor(32, 1, 5, 5)
output = input.flatten(start_dim=1)
# output = flow.flatten(input, start_dim=1)
output.size()
# out flow.Size([32, 25])
oneflow.experimental.function_config

alias of oneflow.python.framework.function_util.FunctionConfig

oneflow.experimental.gather_nd(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator is a high-dimensional extension of gather, indices is a K-dimensional tensor, which is regarded as a index of input Blob params.

Each element defines a slice of params:

\[output[(i_0,i_1,...,i_{K-2})] = param[indices(i_{0},i_{1},...,i_{K-2})]\]
Parameters
  • params (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • indices (oneflow._oneflow_internal.BlobDesc) – The slice indices.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

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


@flow.global_function()
def gather_nd_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
                indice: tp.Numpy.Placeholder(shape=(2, 1), dtype=flow.int32)
) -> tp.Numpy:
    gather_nd_blob = flow.gather_nd(params=x,
                                    indices=indice)
    return gather_nd_blob


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

# out [[1. 2. 3.]
#      [7. 8. 9.]]

Example 2:

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


@flow.global_function()
def gather_nd_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
                indice: tp.Numpy.Placeholder(shape=(2, 2), dtype=flow.int32)
) -> tp.Numpy:
    gather_nd_blob = flow.gather_nd(params=x,
                                    indices=indice)
    return gather_nd_blob


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

# out [3. 8.]

Example3:

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


@flow.global_function()
def gather_nd_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
                indice: tp.Numpy.Placeholder(shape=(3, 2), dtype=flow.int32)
) -> tp.Numpy:
    gather_nd_blob = flow.gather_nd(params=x,
                                    indices=indice)
    return gather_nd_blob


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

# out [2. 4. 9.]
oneflow.experimental.gelu(x)

Gelu activation operator.

The equation is:

\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]
Parameters

x (oneflow.Tensor) – Input Tensor

Returns

A Tensor.

Return type

oneflow.Tensor

For example:

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

x = np.array([-0.5, 0, 0.5]).astype(np.float32)
input = flow.Tensor(x)
gelu = flow.nn.GELU()

out = gelu(input)
# out [-0.15426877, 0., 0.34573123]
oneflow.experimental.gen_tensor_buffer(shape: Sequence[int], shape_list: Sequence[Sequence[int]], value_list: Sequence[float], data_type: Optional[oneflow._oneflow_internal.dtype] = oneflow.float32, dynamic_out: Optional[bool] = False, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator generates a tensor buffer blob.

Parameters
  • shape (Sequence[int]) – shape of output blob

  • shape_list (Sequence[Sequence[int]]) – shapes for tensor buffer in output blob

  • value_list (Sequence[float]) – values for tensor buffer in output blob

  • data_type (Optional[flow.dtype]) – data type for tensor buffer in output blob

  • dynamic_out (Optional[bool]) – if output is a dynamic blob

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

Returns

The result Blob.

Return type

BlobDesc

For example: .. code-block:: python

import oneflow as flow

@flow.global_function(function_config=func_config) def GenTensorBufferJob():

with flow.scope.placement(“cpu”, “0:0”):

x = flow.gen_tensor_buffer([(2,)], [(2, 1), (1, 2)], [0.0, 1.0]) y = flow.tensor_buffer_to_list_of_tensors(x, (100, 100), flow.float, True) return y

# y_0.shape (2, 1), y_1.shape (1. 2)

oneflow.experimental.get_all_variables() → Dict[str, oneflow._oneflow_internal.EagerConsistentBlob]

Get all variables of all jobs as a dict.

oneflow.experimental.get_interface_blob_value(op_name)
oneflow.experimental.get_job_set()
oneflow.experimental.get_variable(name: str, shape: Optional[Sequence[int]] = None, dtype: Optional[oneflow._oneflow_internal.dtype] = oneflow.float32, initializer: Optional[oneflow.core.job.initializer_conf_pb2.InitializerConf] = None, regularizer: Optional[oneflow.core.job.regularizer_conf_pb2.RegularizerConf] = None, trainable: Optional[bool] = None, model_name: Optional[str] = None, random_seed: Optional[int] = None, parallel_distribution: Union[Sequence[oneflow._oneflow_internal.distribute.Distribute], Sequence[str], str, None] = None, distribute: Optional[oneflow._oneflow_internal.distribute.Distribute] = None, reuse: bool = True) → oneflow._oneflow_internal.BlobDesc

Create a variable or retrieve an existing one.

Parameters
  • name – Name of this variable. One variable could be shared by multiple OneFlow functions. None by default

  • shape – Shape of the variable. None by default

  • dtype – Data type of the variable. None by default

  • initializer – A initializer object. For instance, a ones_initializer(). None by default

  • trainable – A bool to indicate if this variable is trainable. True by default

  • model_name – A string. ‘weight’ or ‘bias’. None by default

  • random_seed – Random seed for random initializers. None by default

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def variable_Job() -> None:
    init = flow.constant_initializer(1.25)
    variable = flow.get_variable(
        "variable-weight",
        shape=(1, 3, 2, 2),
        initializer=init,
        trainable=True
    )
    flow.watch(variable, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
variable_Job()

# out [[[[1.25 1.25]
#        [1.25 1.25]]

#       [[1.25 1.25]
#        [1.25 1.25]]

#       [[1.25 1.25]
#        [1.25 1.25]]]]

Example 2:

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


def conv2d(input, filters, kernel_size, strides, padding, name):
    input_shape = input.shape
    weight_initializer = flow.truncated_normal(0.1)
    weight_regularizer = flow.regularizers.l2(0.0005)
    weight_shape = (filters,
                    input_shape[1],
                    kernel_size[0],
                    kernel_size[1])

    weight = flow.get_variable(
        name + "-weight",
        shape=weight_shape,
        initializer=weight_initializer,
        regularizer=weight_regularizer,
    )
    return flow.nn.conv2d(input, weight, strides, padding, name=name)


@flow.global_function()
def conv2d_Job(x: tp.Numpy.Placeholder((1, 64, 32, 32))
) -> tp.Numpy:
    conv = conv2d(x,
                filters=128,
                kernel_size=[3, 3],
                strides=2,
                padding='SAME',
                name="ConvLayer")
    return conv


x = np.random.randn(1, 64, 32, 32).astype(np.float32)
out = conv2d_Job(x)

# out.shape (1, 128, 16, 16)
oneflow.experimental.global_function(type: str = 'predict', function_config: oneflow.python.framework.function_util.FunctionConfig = None) → Callable[[Callable], Callable]

Creates a callable OneFlow global function from a Python function.

For instance:

@oneflow.global_function(flow.FunctionConfig())
def train():
    # your model
Parameters

function_config (FunctionConfig, optional) – a FunctionConfig object. Defaults to FunctionConfig().

Returns

a callable which is called to execute the compiled function

Return type

Callable[[Callable], Callable]

oneflow.experimental.glorot_normal_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier normal distribution.

It also can be called as oneflow.glorot_normal_initializer.

The equation is:

\[W\sim N(0, \sqrt{\frac{{2}}{{n_j+n_{j+1}}}})\]

\(N\) means normal distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_normal_Job() -> None:
    init = flow.xavier_normal_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_normal_Job()

# out [[ 0.5908121  -0.10804518 -0.6148571 ]
#      [ 1.4007381  -0.08172473  0.36579943]
#      [-0.6461796  -0.15923311  0.33653972]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_normal_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.glorot_uniform_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier uniform distribution.

It also can be called as oneflow.glorot_uniform_initializer.

The equation is:

\[W\sim U(-\sqrt{\frac{{6}}{{n_j+n_{j+1}}}},\sqrt{\frac{{6}}{{n_j+n_{j+1}}}})\]

\(U\) means uniform distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_uniform_Job() -> None:
    init = flow.xavier_uniform_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_uniform_Job()

# out [[-0.14424723 -0.9532095  -0.08723891]
#      [-0.8011227  -0.29729813 -0.26769108]
#      [ 0.9208976  -0.5971756  -0.15077025]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_uniform_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_uniform_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_uniform_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.gt(x, y)

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

Parameters
  • x (oneflow.Tensor) – A Tensor

  • y (oneflow.Tensor) – A Tensor

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

Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
input2 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)

out = flow.gt(input1, input2).numpy()
# out shape (2, 6, 5, 3)
oneflow.experimental.hierarchical_parallel_cast(input: oneflow._oneflow_internal.BlobDesc, parallel_distribution: Sequence[str], grad_mode: Optional[str] = None, grad_parallel_distribution: Sequence[str] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.identity(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns a Blob that has identical content and data type to input Blob.

Analogous to tf.identity

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def identity_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.int32),
) -> tp.Numpy:
    return flow.identity(x)

x = np.array([[1, 1, 1],
            [2, 2, 2],
            [3, 3, 3]]).astype(np.int32)
out = identity_Job(x)

# out [[1 1 1]
#      [2 2 2]
#      [3 3 3]]
oneflow.experimental.identity_n(inputs: Sequence[oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → List[oneflow._oneflow_internal.BlobDesc]

This operator is similar to oneflow.identity. The difference is that the input and output of identity_n is List.

Parameters
  • inputs (Iterable[oneflow._oneflow_internal.BlobDesc]) – A List of input Blob.

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

Returns

A list of result Blob.

Return type

List[oneflow._oneflow_internal.BlobDesc]

For example:

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


@flow.global_function()
def identity_Job(x: tp.Numpy.Placeholder(shape=(1, 3), dtype=flow.int32),
                y: tp.Numpy.Placeholder(shape=(1, 3), dtype=flow.int32),
                z: tp.Numpy.Placeholder(shape=(1, 3), dtype=flow.int32)
) -> List[tp.Numpy]:
    return flow.identity_n([x, y, z])


x = np.array([[1, 1, 1]]).astype(np.int32)
y = np.array([[2, 2, 2]]).astype(np.int32)
z = np.array([[3, 3, 3]]).astype(np.int32)
out = identity_Job(x, y, z)

# out[0] [[1, 1, 1]]
# out[1] [[2, 2, 2]]
# out[2] [[3, 3, 3]]
oneflow.experimental.image_batch_align(images: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], dtype: oneflow._oneflow_internal.dtype, alignment: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator aligns the shape for a batch of images.

The aligned shape is computed as:

\[ \begin{align}\begin{aligned}& shape_{width} = int(\frac{(shape_{width}+alignment-1)}{alignment})*alignment\\& shape_{height} = int(\frac{(shape_{height}+alignment-1)}{alignment})*alignment\end{aligned}\end{align} \]
Parameters
  • images (oneflow._oneflow_internal.BlobDesc) – The images.

  • shape (Sequence[int]) – The maximum static shape of input images.

  • dtype (flow.dtype) – The data type.

  • alignment (int) – The align factor.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _of_image_batch_align(images, input_shape, output_shape, alignment):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_batch_align_job(
        images_def: tp.ListListNumpy.Placeholder(shape=input_shape, dtype=flow.float)
    ) -> tp.ListNumpy:
        # Convert to tensor buffer
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        image = flow.image_batch_align(
            images_buffer, shape=output_shape[1:], dtype=flow.float, alignment=alignment
        )
        return image

    image = image_batch_align_job([images])
    return image[0]


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

def _roundup(x, n):
    # compute the aligned shape
    return int((x + n - 1) / n) * n

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg', './img/2.jpg', './img/3.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [3, 349, 367, 3]
    alignment = 16 # alignment factor
    aligned_image_shape = [
        img_shape[0],
        _roundup(img_shape[1], alignment),
        _roundup(img_shape[2], alignment),
        img_shape[3],
    ]
    image = _of_image_batch_align(img, tuple(img_shape), aligned_image_shape, alignment)
oneflow.experimental.image_decode(images_bytes_buffer: oneflow._oneflow_internal.BlobDesc, dtype: oneflow._oneflow_internal.dtype = oneflow.uint8, color_space: str = 'BGR', name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator decode the image.

Parameters
  • images_bytes_buffer (oneflow._oneflow_internal.BlobDesc) – The input Blob. Its type should be kTensorBuffer. More details please refer to the code example.

  • dtype (flow.dtype, optional) – The data type. Defaults to flow.uint8.

  • color_space (str, optional) – The color space. Defaults to “BGR”.

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

Returns

The decoded image list.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp
import numpy as np
from PIL import Image


def _of_image_decode(images):
    image_files = [open(im, "rb") for im in images]
    images_bytes = [imf.read() for imf in image_files]
    static_shape = (len(images_bytes), max([len(bys) for bys in images_bytes]))
    for imf in image_files:
        imf.close()

    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_decode_job(
        images_def: tp.ListListNumpy.Placeholder(shape=static_shape, dtype=flow.int8)
    )->tp.ListListNumpy:
        # convert to tensor buffer
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        decoded_images_buffer = flow.image_decode(images_buffer)
        # Remember to set a shape
        # convert back to tensor list
        return flow.tensor_buffer_to_tensor_list(
            decoded_images_buffer, shape=(640, 640, 3), dtype=flow.uint8
        )

    images_np_arr = [
        np.frombuffer(bys, dtype=np.byte).reshape(1, -1) for bys in images_bytes
    ]
    decoded_images = image_decode_job([images_np_arr])
    return decoded_images[0]


if __name__ == "__main__":
    img = _of_image_decode(['./img/1.jpg'])
    print(img[0].shape) # Our image shape is (1, 349, 367, 3)
oneflow.experimental.image_flip(image: oneflow._oneflow_internal.BlobDesc, flip_code: Union[int, oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator flips the images.

The flip code corresponds to the different flip mode:

0 (0x00): Non Flip

1 (0x01): Horizontal Flip

16 (0x10): Vertical Flip

17 (0x11): Both Horizontal and Vertical Flip

Parameters
  • image (oneflow._oneflow_internal.BlobDesc) – The input images.

  • flip_code (Union[int, oneflow._oneflow_internal.BlobDesc]) – The flip code.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _of_image_flip(images, image_shape, flip_code):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_flip_job(
        images_def: tp.ListListNumpy.Placeholder(shape=image_shape, dtype=flow.float)
    ) -> tp.ListListNumpy:
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        flip_images = flow.image_flip(images_buffer, flip_code)
        return flow.tensor_buffer_to_tensor_list(
            flip_images, shape=image_shape[1:], dtype=flow.float
        )

    image_tensor = image_flip_job([images])
    return image_tensor[0]


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg', './img/2.jpg', './img/3.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [3, 349, 367, 3]
    image = _of_image_flip(img,
                   tuple(img_shape),
                   flip_code=1)
oneflow.experimental.image_normalize(image: oneflow._oneflow_internal.BlobDesc, std: Sequence[float], mean: Sequence[float], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator normalizes the image.

Parameters
  • image (oneflow._oneflow_internal.BlobDesc) – The input image.

  • std (Sequence[float]) – The standard deviation of the images.

  • mean (Sequence[float]) – The mean value of the images.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _of_image_normalize(images, image_shape, std, mean):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_normalize_job(
        images_def: tp.ListListNumpy.Placeholder(shape=image_shape, dtype=flow.float)
    ) -> tp.ListListNumpy:
        # Convert to tensor buffer
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        # Normalize the imagess
        norm_images = flow.image_normalize(images_buffer, std, mean)
        # Convert back to tensor list
        return flow.tensor_buffer_to_tensor_list(
            norm_images, shape=image_shape[1:], dtype=flow.float
        )

    image_tensor = image_normalize_job([images])
    return image_tensor[0]


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg', './img/2.jpg', './img/3.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [3, 349, 367, 3]
    image = _of_image_normalize(img,
                                tuple(img_shape),
                                std=(102.9801, 115.9465, 122.7717),
                                mean=(1.0, 1.0, 1.0))
oneflow.experimental.image_random_crop(input_blob: oneflow._oneflow_internal.BlobDesc, num_attempts: int = 10, seed: Optional[int] = None, random_area: Sequence[float] = None, random_aspect_ratio: Sequence[float] = None, name: str = 'ImageRandomCrop') → oneflow._oneflow_internal.BlobDesc

This operator crops the input image randomly.

Parameters
  • input_blob (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • num_attempts (int, optional) – The maximum number of random cropping attempts. Defaults to 10.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • random_area (Sequence[float], optional) – The random cropping area. Defaults to None.

  • random_aspect_ratio (Sequence[float], optional) – The random scaled ratio. Defaults to None.

  • name (str, optional) – The name for the operation. Defaults to “ImageRandomCrop”.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

def _of_image_random_crop(images, image_static_shape):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_random_crop_job(images_def: tp.ListListNumpy.Placeholder(shape=image_static_shape, dtype=flow.float)
    ) -> tp.ListListNumpy:
        # The input Blob type should be "kTensorBuffer"
        # So we use oneflow.tensor_list_to_tensor_buffer to convert
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        # Do the random crop
        random_crop_buffer = flow.image.random_crop(
            images_buffer,
            random_area=[0.15, 0.80],
            random_aspect_ratio=[0.75, 1.55],
        )
        # We convert back to "tensorlist" type
        random_crop_images = flow.tensor_buffer_to_tensor_list(
            random_crop_buffer,
            shape=(image_static_shape[1], image_static_shape[2], image_static_shape[-1]),
            dtype=flow.float,
        )
        return random_crop_images

    random_crop_images = image_random_crop_job([images])

    return random_crop_images

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg'])
    img_shape = _get_images_static_shape(img) # In example is (1, 234, 346, 3)
    random_crop_images = _of_image_random_crop(img, tuple(img_shape))
    # random_crop_images.shape is (234, 346, 3)
oneflow.experimental.image_resize(image: oneflow._oneflow_internal.BlobDesc, target_size: Union[int, Sequence[int]] = None, min_size: Optional[int] = None, max_size: Optional[int] = None, keep_aspect_ratio: bool = False, resize_side: str = 'shorter', channels: int = 3, dtype: Optional[oneflow._oneflow_internal.dtype] = None, interpolation_type: str = 'auto', name: Optional[str] = None, color_space: Optional[str] = None, interp_type: Optional[str] = None, resize_shorter: int = 0, resize_x: int = 0, resize_y: int = 0) → Union[oneflow._oneflow_internal.BlobDesc, Sequence[oneflow._oneflow_internal.BlobDesc]]

Resize images to target size.

Parameters
  • image – A Tensor consists of images to be resized.

  • target_size – A list or tuple when keep_aspect_ratio is false or an int when keep_aspect_ratio is true. When keep_aspect_ratio is false, target_size has a form of (target_width, target_height) that image will resize to. When keep_aspect_ratio is true, the longer side or shorter side of the image will be resized to target size.

  • min_size – An int, optional. Only works when keep_aspect_ratio is true and resize_side is “longer”. If min_size is not None, the shorter side must be greater than or equal to min_size. Default is None.

  • max_size – An int, optional. Only works when keep_aspect_ratio is true and resize_side is “shorter”. If max_size is not None, the longer side must be less than or equal to max_size. Default is None.

  • keep_aspect_ratio – A bool. If is false, indicate that image will be resized to fixed width and height, otherwise image will be resized keeping aspect ratio.

  • resize_side – A str of “longer” or “shorter”. Only works when keep_aspect_ratio is True. If resize_side is “longer”, the longer side of image will be resized to target_size. If resize_side is “shorter”, the shorter side of image will be resized to target_size.

  • channels – An int. how many channels an image has

  • dtypeoneflow.dtype. Indicate output resized image data type.

  • interpolation_type – A str of “auto”, “bilinear”, “nearest_neighbor”, “bicubic” or “area”. Indicate interpolation method used to resize image.

  • name – A str, optional. Name for the operation.

  • color_space – Deprecated, a str of “RGB”, “BGR” or “GRAY”. Please use channels instead.

  • interp_type – Deprecated, s str of “Linear”, “Cubic” or “NN”. Please use interpolation_type instead.

  • resize_shorter – Deprecated, a int. Indicate target size that the shorter side of image will resize to. Please use target_size and resize_side instead.

  • resize_x – Deprecated, a int. Indicate the target size that the width of image will resize to. Please use target_size instead.

  • resize_y – Deprecated, a int. Indicate the target size that the height of image will resize to. Please use target_size instead.

Returns

Tuple of resized images Blob, width and height scales Blob and new width and height Blob (new width and height Blob will be None when keep_aspect_ratio is false). If deprecated params are used, a single resized images Blob will be returned.

For example:

import oneflow as flow
import oneflow.typing as tp
from typing import Tuple


@flow.global_function(type="predict")
def ofrecord_reader_job() -> Tuple[tp.Numpy, tp.Numpy]:
    batch_size = 16
    color_space = "RGB"
    # our ofrecord file path is "./dataset/part-0"
    ofrecord = flow.data.ofrecord_reader(
        "./imgdataset",
        batch_size=batch_size,
        data_part_num=1,
        part_name_suffix_length=-1,
        part_name_prefix='part-',
        random_shuffle=True,
        shuffle_after_epoch=True,
    )
    image = flow.data.OFRecordImageDecoderRandomCrop(
            ofrecord, "encoded", color_space=color_space
        )
    res_image, scale, new_size = flow.image.Resize(
            image, target_size=(224, 224)
        )
    label = flow.data.OFRecordRawDecoder(
        ofrecord, "class/label", shape=(1, ), dtype=flow.int32
    )

    return res_image, label

if __name__ == "__main__":
    images, labels = ofrecord_reader_job()
    # image.shape (16, 224, 224, 3)
oneflow.experimental.image_target_resize(images: oneflow._oneflow_internal.BlobDesc, target_size: int, min_size: Optional[int] = None, max_size: Optional[int] = None, resize_side: str = 'shorter', interpolation_type: str = 'auto', name: Optional[str] = None) → Sequence[oneflow._oneflow_internal.BlobDesc]

This operator resizes image to target size.

Parameters
  • images (oneflow._oneflow_internal.BlobDesc) – The input Blob. Its type should be kTensorBuffer. More details please refer to the code example.

  • target_size (int) – An int, the target size.

  • min_size (Optional[int], optional) – If min_size is not None, the shorter side must be greater than or equal to min_size. Default is None. Defaults to None.

  • max_size (Optional[int], optional) – If max_size is not None, the longer side must be less than or equal to max_size. Defaults to None.

  • resize_side (str, optional) – A str of “longer” or “shorter”. Only works when keep_aspect_ratio is True. If resize_side is “longer”, the longer side of image will be resized to target_size. If resize_side is “shorter”, the shorter side of image will be resized to target_size. Defaults to “shorter”.

  • interpolation_type (str, optional) – A str of “auto”, “bilinear”, “nearest_neighbor”, “bicubic” or “area”. Indicate interpolation method used to resize image. Defaults to “auto”.

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

Returns

A Sequence includes the result Blob.

Return type

Sequence[oneflow._oneflow_internal.BlobDesc]

For example:

import oneflow as flow
import oneflow.typing as tp
from typing import Tuple
import numpy as np
import cv2


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

def _of_image_target_resize(images, image_static_shape, target_size, max_size):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_target_resize_job(images_def: tp.ListListNumpy.Placeholder(shape=image_static_shape, dtype=flow.float)
    ) -> Tuple[tp.ListListNumpy, tp.ListNumpy, tp.ListNumpy]:
        # The input Blob type should be "kTensorBuffer"
        # So we use oneflow.tensor_list_to_tensor_buffer to convert
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)

        resized_images_buffer, size, scale = flow.image_target_resize(
            images_buffer,
            target_size=target_size,
            max_size=max_size,
            resize_side="shorter",
        )
        # We convert back to "tensorlist" type
        resized_images = flow.tensor_buffer_to_tensor_list(
            resized_images_buffer,
            shape=(target_size, max_size, image_static_shape[-1]),
            dtype=flow.float,
        )
        return resized_images, size, scale

    resized_images, size, scale = image_target_resize_job([images])
    resized_image = resized_images[0]
    size = size[0]
    scale = scale[0]

    return resized_images, size, scale

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [1, 349, 367, 3]
    target_size = 256
    max_size = 512
    resized_images, size, scale = _of_image_target_resize(img, tuple(img_shape), target_size, max_size)
    # Here the shorter side is "349", we resize it to target_size(256)
    # The scale is 256 / 349 = 0.73
    # The longer side will be resized to 367 * scale = 269
    # get the first element from the resized_images (its type is `list.list`)
    print(resized_images[0][0].shape) # (1, 256, 269, 3)
oneflow.experimental.in_top_k(targets: oneflow._oneflow_internal.BlobDesc, predictions: oneflow._oneflow_internal.BlobDesc, k: Optional[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Says whether the targets are in the top K predictions.

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

  • predictions (oneflow._oneflow_internal.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._oneflow_internal.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.experimental.indexed_slices_reduce_sum(indices: oneflow.python.framework.input_blob_def.ArgBlobDef, values: oneflow.python.framework.input_blob_def.ArgBlobDef, name: Optional[str] = None) → Tuple[oneflow._oneflow_internal.BlobDesc]
oneflow.experimental.inter_job_reuse_mem_strategy(strategy_str: str, job_set: Optional[oneflow.core.job.job_set_pb2.JobSet] = None, **kwargs: _VT) → None

Set memory sharing strategy for job set.

Parameters
  • strategy_str – An optional string from: mem_sharing_priority, parallelism_priority

  • custom_parallelism. (or) –

  • job_set – A JobSet object. If None, set default job set.

oneflow.experimental.is_deprecated(func_or_class)
oneflow.experimental.kaiming_initializer(shape: Sequence[int], distribution: str = 'random_normal', mode: str = 'fan_in', nonlinearity: str = 'leaky_relu', negative_slope: float = 0.0, data_format: str = 'NCHW') → None

Initialize weight according to the method described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015), using a normal or uniform distribution.

When distribution is “random_normal”

The equation is:

\[W \sim N(0, \sqrt{\frac{{2}}{{n}}})\]

When distribution is “random_uniform”

The equation is:

\[W \sim U(-\sqrt{\frac{{6}}{{n}}}, \sqrt{\frac{{6}}{{n}}})\]

If mode is “fan_in”, the “n” is the number of input units in the weight Blob.

If mode is “fan_out”, the “n” is the number of output units in the weight Blob.

if mode is “fan_avg”, the “n” is the average of the number of input and output units in the weight Blob

Parameters
  • shape (Sequence[int]) – Blob shape.

  • distribution (str, optional) – ‘random_normal’ or ‘random_uniform’. Defaults to “random_normal”.

  • mode (str, optional) – ‘fan_in’, ‘fan_out’ or ‘fan_avg’. Defaults to “fan_in”.

  • nonlinearity (str, optional) – None, ‘tanh’, ‘sigmoid’, ‘relu’ or ‘leaky_relu’. Defaults to “leaky_relu”.

  • negative_slope (float, optional) – The negative slope of leaky_relu. Defaults to 0.0.

  • data_format (str, optional) – ‘NCHW’, ‘NHWC’. Defaults to “NCHW”.

Raises

NotImplementedError – Only support normal and uniform distribution

Returns

flow.random_normal_initializer or flow.random_uniform_initializer

Return type

[type]

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def kaiming_Job() -> None:
    init = flow.kaiming_initializer(shape=(3, 3),
                                    mode="fan_avg",
                                    nonlinearity="relu")
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
kaiming_Job()

# out [[ 0.54521346  0.32585594  1.3474437 ]
#      [ 0.30729076 -0.19158769  0.2709008 ]
#      [-0.95830524 -0.05093324  0.28178614]]

Example 2:

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


@flow.global_function()
def conv2d_kaiming_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.kaiming_initializer(shape=(1, 256, 32, 32))
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_kaiming_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.load(path: str) → Union[Dict[str, oneflow.python.framework.check_point_v2.FileBackendVariableBlob], oneflow.python.framework.check_point_v2.FileBackendVariableBlob]

Load variable(s) from file system.

oneflow.experimental.load_variables(value_dict: Dict[str, Union[oneflow._oneflow_internal.EagerBlobTrait, oneflow.python.framework.check_point_v2.FileBackendVariableBlob, numpy.ndarray, oneflow.Tensor]], ignore_mismatch: bool = True)

Load value in value_dict into oneflow variables. For example, if value_dict is {‘x’, np.ones(x_shape)}, the value of variable “x” will all ones. If ignore_mismatch is False, an exception will be raised when there is a name in value_dict not belonging to any variable.

oneflow.experimental.log(tensor)

Returns a new tensor with the natural logarithm of the elements of input. .. math:

y_{i} = \log_{e} (x_{i})
Parameters

input (Tensor) –

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np arr = np.random.randn(2, 3, 4, 5) input = flow.Tensor(arr, dtype=flow.float32) output = flow.log(input) # equal to np.log(input)

oneflow.experimental.logical_slice(x: oneflow._oneflow_internal.BlobDesc, slice_tup_list: Sequence[Tuple[int, int, int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.logical_slice_assign(x: oneflow._oneflow_internal.BlobDesc, value: oneflow._oneflow_internal.BlobDesc, slice_tup_list: Sequence[Tuple[int, int, int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.lt(x, y)

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

Parameters
  • x (oneflow.Tensor) – A Tensor

  • y (oneflow.Tensor) – A Tensor

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

Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32)
input2 = flow.Tensor(np.array([1, 2, 4]).astype(np.float32), dtype=flow.float32)

out = flow.gt(input1, input2).numpy

# out [0 0 1]
oneflow.experimental.masked_fill(tensor, mask, value)

Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.

Parameters
  • mask (BoolTensor) –

  • value (float) –

For example:

import oneflow.experimental as flow
import numpy as np

in_arr = np.array(
    [[[-0.13169311,  0.97277078,  1.23305363,  1.56752789],
    [-1.51954275,  1.87629473, -0.53301206,  0.53006478],
    [-1.38244183, -2.63448052,  1.30845795, -0.67144869]],

    [[ 0.41502161,  0.14452418,  0.38968   , -1.76905653],
    [ 0.34675095, -0.7050969 , -0.7647731 , -0.73233418],
    [-1.90089858,  0.01262963,  0.74693893,  0.57132389]]]
)

fill_value = 8.7654321 # random value e.g. -1e9 3.1415
input = flow.Tensor(in_arr, dtype=flow.float32)
mask = flow.Tensor((in_arr > 0).astype(np.int8), dtype=flow.int)

output = input.masked_fill(mask, fill_value)
#  [[[-0.13169311  8.765432    8.765432    8.765432  ]
#   [-1.5195427   8.765432   -0.53301203  8.765432  ]
#   [-1.3824419  -2.6344805   8.765432   -0.6714487 ]]

#  [[ 8.765432    8.765432    8.765432   -1.7690566 ]
#   [ 8.765432   -0.7050969  -0.7647731  -0.7323342 ]
#   [-1.9008986   8.765432    8.765432    8.765432  ]]]
oneflow.experimental.matmul(a, b)

This operator applies matrix multiplication to two Tensor.

Parameters
Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input1 = flow.Tensor(np.random.randn(2, 6), dtype=flow.float32)
input2 = flow.Tensor(np.random.randn(6, 5), dtype=flow.float32)
of_out = flow.matmul(input1, input2)

# of_out.shape (2, 5)
oneflow.experimental.mean(input_tensor, dim=None, keepdim=False)

Computes the mean of row of elements in a tensor in the given axis, if the axis is None, mean of all elements will be caculated.

For example:

import oneflow.experimental as flow

input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
out = flow.mean(input)
# out: [3.5]
print(out.numpy())

input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
out = flow.mean(input, axis=0)
# out: [2.5 3.5 4.5]
print(out.numpy())

input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
out = flow.mean(input, axis=1)
# out: [ 2. 5.]
print(out.numpy())
oneflow.experimental.mul(x, y)

Computes the multiplication of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = x \times y

For example:

import oneflow.experimental as flow

# element-wise multiply
x = flow.Tensor(np.random.randn(2,3))
y = flow.Tensor(np.random.randn(2,3))
out = flow.mul(x,y).numpy()
print(out.shape) # (2,3)

# scalar mutiply
x = 5
y = flow.Tensor(np.random.randn(2,3))
out = flow.mul(x,y).numpy()
print(out.shape) # (2,3)

# broadcast mutiply
x = flow.Tensor(np.random.randn(1,1))
y = flow.Tensor(np.random.randn(2,3))
out = flow.mul(x,y).numpy()
print(out.shape) # (2,3)
oneflow.experimental.multi_count_not_finite(x: Optional[Sequence[oneflow._oneflow_internal.BlobDesc]] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.neg(x)

This operator computes the negative value of Tensor.

Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(
    np.array([1.0, -1.0, 2.3]).astype(np.float32), dtype=flow.float32
)
out = flow.negative(input).numpy()

# out [-1.0, 1.0, -2.3]
oneflow.experimental.negative(x)

This operator computes the negative value of Tensor.

Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(
    np.array([1.0, -1.0, 2.3]).astype(np.float32), dtype=flow.float32
)
out = flow.negative(input).numpy()

# out [-1.0, 1.0, -2.3]
class oneflow.experimental.no_grad
oneflow.experimental.object_bbox_flip(bbox: oneflow._oneflow_internal.BlobDesc, image_size: oneflow._oneflow_internal.BlobDesc, flip_code: Union[int, oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator flips the object bounding box.

The flip code corresponds to the different flip mode:

0 (0x00): Non Flip

1 (0x01): Horizontal Flip

16 (0x10): Vertical Flip

17 (0x11): Both Horizontal and Vertical Flip

Parameters
  • bbox (oneflow._oneflow_internal.BlobDesc) – The bounding box.

  • image_size (oneflow._oneflow_internal.BlobDesc) – The size of input image.

  • flip_code (Union[int, oneflow._oneflow_internal.BlobDesc]) – The flip code.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _of_object_bbox_flip(bbox_list, image_size, flip_code):
    bbox_shape = _get_bbox_static_shape(bbox_list)
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def object_bbox_flip_job(
        bbox_def: tp.ListListNumpy.Placeholder(
            shape=tuple(bbox_shape), dtype=flow.float
        ),
        image_size_def: tp.ListNumpy.Placeholder(
            shape=image_size.shape, dtype=flow.int32
        ),
    ) -> tp.ListListNumpy:
        bbox_buffer = flow.tensor_list_to_tensor_buffer(bbox_def)
        flip_bbox = flow.object_bbox_flip(bbox_buffer, image_size_def, flip_code)
        return flow.tensor_buffer_to_tensor_list(
            flip_bbox, shape=bbox_shape[1:], dtype=flow.float
        )

    input_bbox_list = [np.expand_dims(bbox, axis=0) for bbox in bbox_list]
    bbox_tensor = object_bbox_flip_job([input_bbox_list], [image_size])
    return bbox_tensor[0]


def _get_bbox_static_shape(bbox_list):
    bbox_shapes = [bbox.shape for bbox in bbox_list]
    bbox_static_shape = np.amax(bbox_shapes, axis=0)
    assert isinstance(
        bbox_static_shape, np.ndarray
    ), "bbox_shapes: {}, bbox_static_shape: {}".format(
        str(bbox_shapes), str(bbox_static_shape)
    )
    bbox_static_shape = bbox_static_shape.tolist()
    bbox_static_shape.insert(0, len(bbox_list))
    return bbox_static_shape

if __name__ == "__main__":
    bbox = np.array([[[20.0, 40.0, 80.0, 160.0],
                    [30.0, 50.0, 70.0, 100.0]]]).astype(np.single) # [x1, y1, x2, y2]
    image_size = np.array([[480, 620]]).astype(np.int32)
    bbox_flip =  _of_object_bbox_flip(bbox,
                                    image_size,
                                    flip_code=1) # Horizontal Flip
    print(bbox_flip[0][0])

    # [[399.  40. 459. 160.]
    #  [409.  50. 449. 100.]]
oneflow.experimental.object_bbox_scale(bbox: oneflow._oneflow_internal.BlobDesc, scale: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator scales the input image and the corresponding bounding box. It returns the scaled bounding box.

Parameters
  • bbox (oneflow._oneflow_internal.BlobDesc) – The bounding box.

  • scale (oneflow._oneflow_internal.BlobDesc) – The scale factor.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2
from typing import Tuple


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return images


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    image_static_shape.insert(0, len(image_shapes))
    return image_static_shape


def _get_bbox_static_shape(bbox_list):
    bbox_shapes = [bbox.shape for bbox in bbox_list]
    bbox_static_shape = np.amax(bbox_shapes, axis=0)
    assert isinstance(
        bbox_static_shape, np.ndarray
    ), "bbox_shapes: {}, bbox_static_shape: {}".format(
        str(bbox_shapes), str(bbox_static_shape)
    )
    bbox_static_shape = bbox_static_shape.tolist()
    bbox_static_shape.insert(0, len(bbox_list))
    return bbox_static_shape


def _of_target_resize_bbox_scale(images, bbox_list, target_size, max_size):
    image_shape = _get_images_static_shape(images)
    bbox_shape = _get_bbox_static_shape(bbox_list)

    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def target_resize_bbox_scale_job(
        image_def: tp.ListListNumpy.Placeholder(
            shape=tuple(image_shape), dtype=flow.float
        ),
        bbox_def: tp.ListListNumpy.Placeholder(
            shape=tuple(bbox_shape), dtype=flow.float
        ),
    ) -> Tuple[tp.ListListNumpy, tp.ListNumpy]:
        images_buffer = flow.tensor_list_to_tensor_buffer(image_def)
        resized_images_buffer, new_size, scale = flow.image_target_resize(
            images_buffer, target_size=target_size, max_size=max_size
        )
        bbox_buffer = flow.tensor_list_to_tensor_buffer(bbox_def)
        scaled_bbox = flow.object_bbox_scale(bbox_buffer, scale)
        scaled_bbox_list = flow.tensor_buffer_to_tensor_list(
            scaled_bbox, shape=bbox_shape[1:], dtype=flow.float
        )
        return scaled_bbox_list, new_size

    input_image_list = [np.expand_dims(image, axis=0) for image in images]
    input_bbox_list = [np.expand_dims(bbox, axis=0) for bbox in bbox_list]
    output_bbox_list, output_image_size = target_resize_bbox_scale_job(
        [input_image_list], [input_bbox_list]
    )
    return output_bbox_list[0], output_image_size[0]


if __name__ == "__main__":
    images = _read_images_by_cv(['./img/1.jpg', './img/2.jpg'])
    bbox = np.array([[[20.0, 40.0, 80.0, 160.0],
                    [30.0, 50.0, 70.0, 100.0]],
                    [[26.0, 40.0, 86.0, 160.0],
                    [36.0, 56.0, 76.0, 106.0]]]).astype(np.single) # [x1, y1, x2, y2]
    bbox, size = _of_target_resize_bbox_scale(images, bbox, 280, 350)
    print(bbox[0])
    print(bbox[1])

    # [[[ 16.0218    32.09169   64.0872   128.36676 ]
    #   [ 24.032698  40.114613  56.076298  80.229225]]]

    # [[[ 24.186047  37.170418  80.       148.68167 ]
    #   [ 33.488373  52.038586  70.69768   98.5016  ]]]
oneflow.experimental.object_segmentation_polygon_flip(poly: oneflow._oneflow_internal.BlobDesc, image_size: oneflow._oneflow_internal.BlobDesc, flip_code: Union[int, oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator flips the segmentation points in image.

The flip code corresponds to the different flip mode:

0 (0x00): Non Flip

1 (0x01): Horizontal Flip

16 (0x10): Vertical Flip

17 (0x11): Both Horizontal and Vertical Flip

Parameters
  • poly (oneflow._oneflow_internal.BlobDesc) – The poly segmentation points.

  • image_size (oneflow._oneflow_internal.BlobDesc) – The image size.

  • flip_code (Union[int, oneflow._oneflow_internal.BlobDesc]) – The filp code.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _of_object_segm_poly_flip(poly_list, image_size, flip_code):
    poly_shape = _get_segm_poly_static_shape(poly_list)

    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def object_segm_poly_flip_job(
        poly_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_shape), dtype=flow.float
        ),
        image_size_def: tp.ListNumpy.Placeholder(
            shape=image_size.shape, dtype=flow.int32
        ),
    ) -> tp.ListListNumpy:
        poly_buffer = flow.tensor_list_to_tensor_buffer(poly_def)
        flip_poly = flow.object_segmentation_polygon_flip(
            poly_buffer, image_size_def, flip_code
        )
        return flow.tensor_buffer_to_tensor_list(
            flip_poly, shape=poly_shape[1:], dtype=flow.float
        )

    input_poly_list = [np.expand_dims(poly, axis=0) for poly in poly_list]
    poly_tensor = object_segm_poly_flip_job([input_poly_list], [image_size])
    return poly_tensor[0]


def _get_segm_poly_static_shape(poly_list):
    poly_shapes = [poly.shape for poly in poly_list]
    poly_static_shape = np.amax(poly_shapes, axis=0)
    assert isinstance(
        poly_static_shape, np.ndarray
    ), "poly_shapes: {}, poly_static_shape: {}".format(
        str(poly_shapes), str(poly_static_shape)
    )
    poly_static_shape = poly_static_shape.tolist()
    poly_static_shape.insert(0, len(poly_list))
    return poly_static_shape

if __name__ == "__main__":
    segm_poly_list = []
    segmentations = [[[20.0, 40.0], [80.0, 160.0], [100.0, 210.0]], # Image 1 segmentation point
                    [[25.0, 45.0], [85.0, 165.0], [105.0, 215.0]]] # Image 2 segmentation point
    for segmentation in segmentations:
        polygon = []
        for seg in segmentation:
            polygon.extend(seg)
        poly_array = np.array(polygon, dtype=np.single).reshape(-1, 2) # Reshape it
        segm_poly_list.append(poly_array)

    image_size = np.array([[480, 620], # Image 1 size
                        [640, 640]]).astype(np.int32) # Image 2 size
    of_segm_poly_list = _of_object_segm_poly_flip(
        segm_poly_list, image_size, flip_code=1
    ) # Horizontal Flip
    print(of_segm_poly_list[0])
    print(of_segm_poly_list[1])

    # of_segm_poly_list[0]
    # [[[460.  40.]
    #   [400. 160.]
    #   [380. 210.]]]

    # of_segm_poly_list[1]
    # [[[615.  45.]
    #   [555. 165.]
    #   [535. 215.]]]
oneflow.experimental.object_segmentation_polygon_scale(poly: oneflow._oneflow_internal.BlobDesc, scale: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator scales the segmentation points in the images.

Parameters
  • poly (oneflow._oneflow_internal.BlobDesc) – The poly segmentation points.

  • scale (oneflow._oneflow_internal.BlobDesc) – The image scale.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2
from typing import Tuple


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return images


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    image_static_shape.insert(0, len(image_shapes))
    return image_static_shape


def _get_segm_poly_static_shape(poly_list):
    poly_shapes = [poly.shape for poly in poly_list]
    poly_static_shape = np.amax(poly_shapes, axis=0)
    assert isinstance(
        poly_static_shape, np.ndarray
    ), "poly_shapes: {}, poly_static_shape: {}".format(
        str(poly_shapes), str(poly_static_shape)
    )
    poly_static_shape = poly_static_shape.tolist()
    poly_static_shape.insert(0, len(poly_list))
    return poly_static_shape


def _get_bbox_static_shape(bbox_list):
    bbox_shapes = [bbox.shape for bbox in bbox_list]
    bbox_static_shape = np.amax(bbox_shapes, axis=0)
    assert isinstance(
        bbox_static_shape, np.ndarray
    ), "bbox_shapes: {}, bbox_static_shape: {}".format(
        str(bbox_shapes), str(bbox_static_shape)
    )
    bbox_static_shape = bbox_static_shape.tolist()
    bbox_static_shape.insert(0, len(bbox_list))
    return bbox_static_shape


def _of_object_segm_poly_scale(images, poly_list, target_size, max_size):
    image_shape = _get_images_static_shape(images)
    print(image_shape)
    poly_shape = _get_segm_poly_static_shape(poly_list)
    print("Poly shape is ", poly_shape)
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def object_segm_poly_scale_job(
        image_def: tp.ListListNumpy.Placeholder(
            shape=tuple(image_shape), dtype=flow.float
        ),
        poly_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_shape), dtype=flow.float
        ),
    ) -> Tuple[tp.ListListNumpy, tp.ListNumpy]:
        images_buffer = flow.tensor_list_to_tensor_buffer(image_def)
        resized_images_buffer, new_size, scale = flow.image_target_resize(
            images_buffer, target_size=target_size, max_size=max_size
        )
        poly_buffer = flow.tensor_list_to_tensor_buffer(poly_def)
        scaled_poly = flow.object_segmentation_polygon_scale(poly_buffer, scale)
        scaled_poly_list = flow.tensor_buffer_to_tensor_list(
            scaled_poly, shape=poly_shape[1:], dtype=flow.float
        )
        return scaled_poly_list, new_size

    input_image_list = [np.expand_dims(image, axis=0) for image in images]
    input_poly_list = [np.expand_dims(poly, axis=0) for poly in poly_list]

    output_poly_list, output_image_size = object_segm_poly_scale_job(
        [input_image_list], [input_poly_list]
    )

    return output_poly_list[0], output_image_size

if __name__ == "__main__":
    images = _read_images_by_cv(['./img/1.jpg', './img/2.jpg'])
    segm_poly_list = []
    segmentations = [[[20.0, 40.0], [80.0, 160.0], [100.0, 210.0]], # Image 1 segmentation point
                    [[25.0, 45.0], [85.0, 165.0], [105.0, 215.0]]] # Image 2 segmentation point

    for segmentation in segmentations:
        polygon = []
        for seg in segmentation:
            polygon.extend(seg)
        poly_array = np.array(polygon, dtype=np.single).reshape(-1, 2) # Reshape it
        segm_poly_list.append(poly_array)

    bbox, size = _of_object_segm_poly_scale(images, segm_poly_list, 280, 350)
oneflow.experimental.object_segmentation_polygon_to_mask(poly: oneflow._oneflow_internal.BlobDesc, poly_index: oneflow._oneflow_internal.BlobDesc, image_size: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator converts the poly segment points to the segment mask array.

Parameters
  • poly (oneflow._oneflow_internal.BlobDesc) – The poly segment points.

  • poly_index (oneflow._oneflow_internal.BlobDesc) – The poly segment index.

  • image_size (oneflow._oneflow_internal.BlobDesc) – The input image size.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2
from typing import Tuple


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return images


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    image_static_shape.insert(0, len(image_shapes))
    return image_static_shape


def _get_segm_poly_static_shape(poly_list, poly_index_list):
    assert len(poly_list) == len(poly_index_list)
    num_images = len(poly_list)
    max_poly_elems = 0
    for poly, poly_index in zip(poly_list, poly_index_list):
        assert len(poly.shape) == 2
        assert len(poly_index.shape) == 2, str(poly_index.shape)
        assert poly.shape[0] == poly_index.shape[0]
        assert poly.shape[1] == 2
        assert poly_index.shape[1] == 3
        max_poly_elems = max(max_poly_elems, poly.shape[0])
    return [num_images, max_poly_elems, 2], [num_images, max_poly_elems, 3]

def _segm_poly_to_tensor(img_segm_poly_list):
    poly_array_list = []
    poly_index_array_list = []
    for img_idx, segm_poly_list in enumerate(img_segm_poly_list):
        img_poly_elem_list = []
        img_poly_index_list = []

        for obj_idx, poly_list in enumerate(segm_poly_list):
            for poly_idx, poly in enumerate(poly_list):
                img_poly_elem_list.extend(poly)
                for pt_idx, pt in enumerate(poly):
                    if pt_idx % 2 == 0:
                        img_poly_index_list.append([pt_idx / 2, poly_idx, obj_idx])

        img_poly_array = np.array(img_poly_elem_list, dtype=np.single).reshape(-1, 2)
        assert img_poly_array.size > 0, segm_poly_list
        poly_array_list.append(img_poly_array)

        img_poly_index_array = np.array(img_poly_index_list, dtype=np.int32)
        assert img_poly_index_array.size > 0, segm_poly_list
        poly_index_array_list.append(img_poly_index_array)

    return poly_array_list, poly_index_array_list


def _of_poly_to_mask_pipline(
    images, poly_list, poly_index_list, num_segms_list, target_size, max_size
):
    print(len(images))
    print(len(poly_list))

    assert len(images) == len(poly_list)
    assert len(poly_list) == len(poly_index_list)
    image_shape = _get_images_static_shape(images)
    poly_shape, poly_index_shape = _get_segm_poly_static_shape(
        poly_list, poly_index_list
    )
    max_num_segms = max(num_segms_list)

    func_config = flow.FunctionConfig()
    func_config.default_logical_view(flow.scope.mirrored_view())
    func_config.default_data_type(flow.float)


    @flow.global_function(function_config=func_config)
    def poly_to_mask_job(
        image_def: tp.ListListNumpy.Placeholder(
            shape=tuple(image_shape), dtype=flow.float
        ),
        poly_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_shape), dtype=flow.float
        ),
        poly_index_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_index_shape), dtype=flow.int32
        ),
    ) -> Tuple[tp.ListListNumpy, tp.ListListNumpy]:
        images_buffer = flow.tensor_list_to_tensor_buffer(image_def)
        resized_images_buffer, new_size, scale = flow.image_target_resize(
            images_buffer, target_size=target_size, max_size=max_size
        )
        poly_buffer = flow.tensor_list_to_tensor_buffer(poly_def)
        poly_index_buffer = flow.tensor_list_to_tensor_buffer(poly_index_def)
        scaled_poly_buffer = flow.object_segmentation_polygon_scale(poly_buffer, scale)
        mask_buffer = flow.object_segmentation_polygon_to_mask(
            scaled_poly_buffer, poly_index_buffer, new_size
        )
        mask_list = flow.tensor_buffer_to_tensor_list(
            mask_buffer, shape=(max_num_segms, target_size, max_size), dtype=flow.int8
        )
        scaled_poly_list = flow.tensor_buffer_to_tensor_list(
            scaled_poly_buffer, shape=poly_shape[1:], dtype=flow.float
        )
        return mask_list, scaled_poly_list

    input_image_list = [np.expand_dims(image, axis=0) for image in images]
    input_poly_list = [np.expand_dims(poly, axis=0) for poly in poly_list]
    input_poly_index_list = [
        np.expand_dims(poly_index, axis=0) for poly_index in poly_index_list
    ]

    output_mask_list, output_poly_list = poly_to_mask_job(
        [input_image_list], [input_poly_list], [input_poly_index_list]
    )

    return output_mask_list[0], output_poly_list[0]

if __name__ == "__main__":
    images = _read_images_by_cv(['./img/1.jpg', './img/2.jpg'])
    segm_poly_list = []

    segmentations = [[[20.0, 40.0, 80.0, 160.0, 100.0, 210.0, 120.0, 215.0]], # Image 1 segmentation point
                    [[24.0, 42.0, 86.0, 168.0, 103.0, 223.0, 125.0, 235.0]]] # Image 2 segmentation point

    for segmentation in segmentations:
        polygon = []
        for seg in segmentation:
            polygon.extend(seg)

        poly_array = np.array(polygon, dtype=np.single).reshape(-1, 2) # Reshape it
        segm_poly_list.append([poly_array])

    poly_list, poly_index_list = _segm_poly_to_tensor(segm_poly_list)
    num_segms_list = [len(segm_poly_list) for segm_poly_list in segm_poly_list]
    target_size = 280
    max_size = 350
    of_mask_list, of_scaled_poly_list = _of_poly_to_mask_pipline(
        images, poly_list, poly_index_list, num_segms_list, target_size, max_size
    )
    of_mask_list = [
        mask_array.reshape(-1, mask_array.shape[-2], mask_array.shape[-1])
        for mask_array in of_mask_list
    ] # reshape it
oneflow.experimental.one_hot(indices: oneflow._oneflow_internal.BlobDesc, depth: int, on_value: Union[int, float] = 1, off_value: Union[int, float] = 0, axis: int = -1, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator generates a onehot Blob from input Blob.

If input Blob’s rank is N, the corresponding onehot Blob’s rank is N+1. The new axis is generated on the specified dimension according to the parameter axis.

The locations represented by indices take value on_value, while other locations take off_value

Parameters
  • indices (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • depth (int) – The length of onehot Blob.

  • on_value (Union[int, float], optional) – The fill value when indices[i] == i. Defaults to 1.

  • off_value (Union[int, float], optional) – The fill value when indice[i] != i. Defaults to 0.

  • axis (int, optional) – The specified dimension that the new axis is generated on. Defaults to -1.

  • dtype (Optional[flow.dtype], optional) – The output data type, it can be “oneflow.int32”, “oneflow.int64”, “oneflow.float”, “oneflow.double”. Defaults to None.

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

Note

The data type of input blob should be int32 or int64

For example:

Example 1:

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


@flow.global_function()
def onehot_Job(x: tp.Numpy.Placeholder((4, ), dtype=flow.int32)
) -> tp.Numpy:
    return flow.one_hot(indices=x,
                        depth=5,
                        axis=-1,
                        dtype=flow.int32)


x = np.array([0, 3, 1, 2]).astype(np.int32)
out = onehot_Job(x)

# out [[1 0 0 0 0]
#      [0 0 0 1 0]
#      [0 1 0 0 0]
#      [0 0 1 0 0]]

Example 2:

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


@flow.global_function()
def onehot_Job(x: tp.Numpy.Placeholder((4, ), dtype=flow.int32)
) -> tp.Numpy:
    return flow.one_hot(indices=x,
                        depth=5,
                        axis=0,
                        dtype=flow.int32)


x = np.array([0, 3, 1, 2]).astype(np.int32)
out = onehot_Job(x)

# out [[1 0 0 0]
#      [0 0 1 0]
#      [0 0 0 1]
#      [0 1 0 0]
#      [0 0 0 0]]
Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

oneflow.experimental.ones(size, dtype=None)

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.

Parameters
  • size (an integer or tuple of integer values) – defining the shape of the output tensor.

  • be a variable number of arguments or a collection like a list or tuple. (Can) –

For example:

import oneflow.experimental as flow

y = flow.ones(5)
# [1. 1. 1. 1. 1. ]
oneflow.experimental.ones_initializer(dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blobs initialized to 1.

Parameters

dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Returns

constant_initializer

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def ones_Job() -> None:
    init = flow.ones_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
ones_Job()

# out [1. 1. 1.]

Example 2:

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


@flow.global_function()
def conv2d_one_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.ones_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_one_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.ones_like(other)

Returns a tensor filled with the scalar value 1, with the same size as input. flow.ones_like(input) is equivalent to flow.ones(input.shape, dtype=input.dtype)

Parameters

other (Tensor) – The size of input will determine size of the output tensor.

For example:

import oneflow.experimental as flow
import numpy as np

x = flow.Tensor(np.random.rand([5]))
y = flow.ones_like(x)
# [1. 1. 1. 1. 1. ]
oneflow.experimental.pack(input: oneflow._oneflow_internal.BlobDesc, pack_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.pad(x: oneflow._oneflow_internal.BlobDesc, paddings: Sequence[int], constant_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator pads the input blob with constant value that user specifies. User can set the amount of padding by setting the parameter paddings.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob

  • paddings (Sequence[int]) – A list of integers to specify the padding width, its length must equal with the length of x.shape.

  • constant_value (Union[int, float], optional) – The constant value to pad. Defaults to 0.

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

Raises

ValueError – The parameter paddings must be a tuple or a list.

Returns

The Blob after padding.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.pad(x,
                    paddings=((2, 2), (1, 1)),
                    constant_value=5)


x = np.array([[1, 1, 1],
            [1, 1, 1],
            [1, 1, 1]]).astype(np.float32)
out = pad_Job(x)

# out [[5. 5. 5. 5. 5.]
#      [5. 5. 5. 5. 5.]
#      [5. 1. 1. 1. 5.]
#      [5. 1. 1. 1. 5.]
#      [5. 1. 1. 1. 5.]
#      [5. 5. 5. 5. 5.]
#      [5. 5. 5. 5. 5.]]
oneflow.experimental.pad_grad(x: oneflow._oneflow_internal.BlobDesc, paddings: Sequence[int], constant_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.parallel_cast(input: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None, distribute: Optional[oneflow._oneflow_internal.distribute.Distribute] = None, gradient_distribute: Optional[oneflow._oneflow_internal.distribute.Distribute] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.placement

alias of oneflow._oneflow_internal.PlacementSymbol

oneflow.experimental.pow(tensor, exponent)

Takes the power of each element in input with exponent and returns a tensor with the result. exponent can be either a single float number or a single int number.

For example:

import oneflow.experimental as flow
import numpy as np

x = flow.Tensor(np.array([1, 2, 3, 4, 5, 6]))
out = flow.pow(x, 2).numpy()
print(out) # [1, 4, 9, 16, 25, 36]
oneflow.experimental.random_normal_initializer(mean: float = 0.0, stddev: float = 1.0, seed: Optional[int] = None, dtype: Optional[oneflow._oneflow_internal.dtype] = None) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blob with a normal distribution.

Parameters
  • mean (float, optional) – A python scalar. Mean of the random values to generate.. Defaults to 0.0.

  • stddev (float, optional) – A python scalar. Standard deviation of the random values to generate. Defaults to 1.0.

  • seed (Optional[int], optional) – None. Not support yet. Defaults to None.

  • dtype (Optional[flow.dtype], optional) – . Defaults to None.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def random_normal_Job() -> None:
    init = flow.random_normal_initializer(mean=1, stddev=1)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
random_normal_Job()

# out [1.4190257 2.7663114 1.7114428]

Example 2:

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


@flow.global_function()
def conv2d_random_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.random_normal_initializer(mean=0, stddev=1)

    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_random_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.random_uniform_initializer(minval: float = 0, maxval: float = 1, dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blobs with a uniform distribution.

Parameters
  • minval (float, optional) – A python scalar. Lower bound of the range of random values to generate. Defaults to 0.

  • maxval (float, optional) – A python scalar. Upper bound of the range of random values to generate. Defaults to 1.

  • dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Raises

NotImplementedError – Do not support such data type.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def random_uniform_Job() -> None:
    init = flow.random_uniform_initializer(minval=0, maxval=0.5)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
random_uniform_Job()

# out [0.07557311 0.3943565  0.31875622]

Example 2:

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


@flow.global_function()
def conv2d_random_uniform_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.random_uniform_initializer(minval=0, maxval=0.5)

    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_random_uniform_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.reciprocal(x)

Computes the safe reciprocal of x. If x is zero, the reciprocal will be also set to zero.

For example:

import oneflow.experimental as flow
x = flow.Tensor(np.array([[1, 2, 3], [4, 5, 6]]))
out = flow.reciprocal()(x)
# out [[1.         0.5        0.33333334]
       [0.25       0.2        0.16666667]]
oneflow.experimental.reflection_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using the reflection of the input boundary.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

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

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3))
) -> tp.Numpy:
    return flow.reflection_pad2d(x, padding=[2, 2, 1, 1])


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
out = pad_Job(x)

# out [[[[ 5.  4.  3.  4.  5.  4.  3.]
#    [ 2.  1.  0.  1.  2.  1.  0.]
#    [ 5.  4.  3.  4.  5.  4.  3.]
#    [ 8.  7.  6.  7.  8.  7.  6.]
#    [ 5.  4.  3.  4.  5.  4.  3.]]

#   [[ 14. 13. 12. 13. 14. 13. 12.]
#    [ 11. 10.  9. 10. 11. 10.  9.]
#    [ 14. 13. 12. 13. 14. 13. 12.]
#    [ 17. 16. 15. 16. 17. 16. 15.]
#    [ 14. 13. 12. 13. 14. 13. 12.]]]]
oneflow.experimental.repeat(x, sizes)

This operator repeat the input tensor to a larger size along the specified dimensions.

Parameters
  • x (oneflow.Tensor) – The input Tensor.

  • size (Sequence[int]) – The number of times to repeat this tensor along each dimension

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

import oneflow.experimental as flow
import numpy as np

x = np.array([[[[0, 1]],
               [[2, 3]],
               [[4, 5]]]]).astype(np.int32)

input = flow.Tensor(x)

out = flow.repeat(input, sizes=[1, 1, 2, 2]).numpy()

# out shape: [1, 3, 2, 4]
# [[[[0. 1. 0. 1.]
# [0. 1. 0. 1.]]

# [[2. 3. 2. 3.]
# [2. 3. 2. 3.]]

# [[4. 5. 4. 5.]
# [4. 5. 4. 5.]]]]
oneflow.experimental.replication_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using the replication of the input boundary.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

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

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3))
) -> tp.Numpy:
    return flow.reflection_pad2d(x, padding=[2, 2, 1, 1])


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
out = pad_Job(x)

# out [[[[ 0.  0.  0.  1.  2.  2.  2.]
#    [ 0.  0.  0.  1.  2.  2.  2.]
#    [ 3.  3.  3.  4.  5.  5.  5.]
#    [ 6.  6.  6.  7.  8.  8.  8.]
#    [ 6.  6.  6.  7.  8.  8.  8.]]

#   [[ 9.  9.  9.  10.  11.  11.  11.]
#    [ 9.  9.  9.  10.  11.  11.  11.]
#    [ 12.  12.  12.  13.  14.  14.  14.]
#    [ 15.  15.  15.  16.  17.  17.  17.]
#    [ 15.  15.  15.  16.  17.  17.  17.]]]]
oneflow.experimental.reshape(x, shape: Sequence[int] = None)

This operator reshapes a Tensor.

We can set one dimension in shape as -1, the operator will infer the complete shape.

Parameters
  • x – A Tensor.

  • shape – Shape of the output tensor.

Returns

A Tensor, has the same type as x.

For example:

import numpy as np
import oneflow.experimental as flow

x = np.array(
    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
).astype(np.float32)
input = flow.Tensor(x)

y = flow.reshape(input, shape=[2, 2, 2, -1]).numpy().shape

# y (2, 2, 2, 2)
oneflow.experimental.reshape_like(x: oneflow._oneflow_internal.BlobDesc, like: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reshapes the Blob x to be the same as Blob like .

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • like (oneflow._oneflow_internal.BlobDesc) – A Blob.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def reshape_like_Job(x: tp.Numpy.Placeholder(shape=(4, 4), dtype=flow.float32)
) -> tp.Numpy:
    like_blob = flow.constant(value=1,
                            dtype=flow.int8,
                            shape=(2, 2, 4))
    reshape_like_blob = flow.reshape_like(x,
                                        like=like_blob)
    return reshape_like_blob


x = np.array([[1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12],
            [13, 14, 15, 16]]).astype(np.float32)
out = reshape_like_Job(x)

# out.shape (2, 2, 4)
oneflow.experimental.reverse(input: oneflow._oneflow_internal.BlobDesc, axis: Union[int, Sequence[int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reverses the elements on the assigned axis.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • axis (Union[int, Sequence[int]]) – The reverse axis.

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

Raises
  • ValueError – The name must be a string.

  • ValueError – The axis must be a int or a list/tuple of int.

  • ValueError – The axis is out of range.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def reverse_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32)) -> tp.Numpy:
    reverse_blob = flow.reverse(x,
                                axis=0)
    return reverse_blob


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

# out [[7. 8. 9.]
#      [4. 5. 6.]
#      [1. 2. 3.]]
oneflow.experimental.same_padding(x: oneflow._oneflow_internal.BlobDesc, padding: Sequence[int], data_format: str, kernel_size: Sequence[int], strides: Sequence[int], dilation_rate: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator do the padding in “SAME” mode, It can computes the pad width according to the kernel_size and strides to keep the size of feature map unchanged after convolution or other operations.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input blob.

  • padding (Sequence[int]) – The padding mode. It should be “SAME_UPPER” or “SAME_LOWER”

  • data_format ([type]) – The data format of input Blob. If the string starts with “NC”, it means the data format is channel first, else the data format is channel last.

  • kernel_size (Sequence[int]) – The kernel size of operations. Its type should be tuple or list.

  • strides (Sequence[int]) – The strides of operations. Its type should be tuple or list.

  • dilation_rate (Sequence[int]) – The dilation rate of operations. Its type should be tuple or list.

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

Returns

The Blob after padding.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def same_pad_Job(x: tp.Numpy.Placeholder((1, 1, 3, 3))
) -> tp.Numpy:
    return flow.same_padding(x,
                            padding="SAME_UPPER",
                            data_format="NCHW",
                            kernel_size=(3, 3),
                            strides=(1, 1),
                            dilation_rate=(1, 1))


x = np.ones(shape=(1, 1, 3, 3)).astype(np.float32)
out = same_pad_Job(x)

# out [[[[0. 0. 0. 0. 0.]
#        [0. 1. 1. 1. 0.]
#        [0. 1. 1. 1. 0.]
#        [0. 1. 1. 1. 0.]
#        [0. 0. 0. 0. 0.]]]]
oneflow.experimental.save(obj, save_dir)
oneflow.experimental.scatter_nd(indices: oneflow._oneflow_internal.BlobDesc, updates: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], name: Optional[str] = None)

This operator inserts the elements in updates according to the indices and create a new Blob.

Parameters
  • indices (oneflow._oneflow_internal.BlobDesc) – The indice of updates. Its type should be flow.int.

  • updates (oneflow._oneflow_internal.BlobDesc) – The update Blob.

  • shape (Sequence[int]) – The constant tensor shape, the constant tensor elements are all zero.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

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


@flow.global_function()
def scatter_nd_Job(indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                update: tp.Numpy.Placeholder(shape=(3, ), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.scatter_nd(indices=indice,
                                updates=update,
                                shape=[8])
    return scatter_blob


indice_array = np.array([[1], [6], [4]]).astype(np.int32)
update_array = np.array([10.2, 5.1, 12.7]).astype(np.float32)
out = scatter_nd_Job(indice_array, update_array)

# [ 0.  10.2  0.   0.  12.7  0.   5.1  0. ]

Example 2:

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


@flow.global_function()
def scatter_nd_Job(indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                update: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.scatter_nd(indices=indice,
                                updates=update,
                                shape=[5, 3])
    return scatter_blob


indice_array = np.array([[0], [4], [2]]).astype(np.int32)
update_array = np.array([[1, 1, 1],
                        [2, 2, 2],
                        [3, 3, 3]]).astype(np.float32)
out = scatter_nd_Job(indice_array, update_array)

# out [[1. 1. 1.]
#      [0. 0. 0.]
#      [3. 3. 3.]
#      [0. 0. 0.]
#      [2. 2. 2.]]
oneflow.experimental.set_interface_blob_value(op_name, ndarray)
oneflow.experimental.sigmoid(x)

Applies the element-wise function:

\[\text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}\]
Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Output: \((N, *)\), same shape as the input

For example:

import oneflow.experimental as flow
import numpy as np

x = flow.Tensor(
    np.array(
        [
            [0.81733328, 0.43621480, 0.10351428],
            [-1.15555191, -0.67776406, 0.27372134],
        ]
    )
)
y = x.sigmoid()
# [[0.69366997, 0.60735673, 0.52585548],
# [0.23947647, 0.33676055, 0.56800622]]
oneflow.experimental.sin(tensor)

Returns a new tensor with the sine of the elements of input. .. math:

\text{out}_{i} = \sin(\text{input}_{i})
Parameters

input (Tensor) –

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np arr = np.array([-0.5461, 0.1347, -2.7266, -0.2746]) input = flow.Tensor(arr, dtype=flow.float32) output = flow.sin(input) # [-0.51935846 0.13429303 -0.40318328 -0.27116194]

oneflow.experimental.slice(x: oneflow._oneflow_internal.BlobDesc, begin: Sequence[int], size: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Extracts a slice from a tensor.

Parameters
  • x – A Blob.

  • begin – A list or a tuple, indicate each dimension slice begin, whose length must be equal to x’s number of dimensions, the first element of begin must be set to None. (Because the internal op of OneFlow does not support 0-dimension slice at present.)

  • size – A list or a tuple, indicate each dimension slice size, whose length must be equal to x’s number of dimensions, the first element of beign must be set to None.

  • name – A name for the operation (optional).

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def slice_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32)
) -> tp.Numpy:
    slice_blob = flow.slice(x,
                            begin=[None, 0],
                            size=[None, 2])
    return slice_blob

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

# out [[1. 2.]
#      [4. 5.]
#      [7. 8.]]
oneflow.experimental.slice_update(x: oneflow._oneflow_internal.BlobDesc, update: oneflow._oneflow_internal.BlobDesc, slice_tup_list: Sequence[Tuple[int, int, int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Update a slice of tensor x. Like x[start:stop:step] = update.

Parameters
  • x – A Blob, whose slice will be updated.

  • update – A Blob, indicate the update content.

  • slice_tup_list – A list of slice tuple, indicate each dimension slice (start, stop, step).

  • name – A name for the operation (optional).

For example:

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


@flow.global_function()
def slice_update_job(x: tp.Numpy.Placeholder(shape=(5, )),
                    update: tp.Numpy.Placeholder(shape=(3, )))->tp.Numpy:
    out = flow.slice_update(x=x,
                            update=update,
                            slice_tup_list=[[1, 4, 1]])

    return out

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

# out [1. 2. 3. 4. 1.]
oneflow.experimental.slice_v2(x: oneflow._oneflow_internal.BlobDesc, slice_tup_list: Sequence[Tuple[int, int, int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Extracts a slice from a tensor. The slice_tup_list assigns the slice indices in each dimension, the format is (start, stop, step). The operator will slice the Blob according to the slice_top_list.

Parameters
  • x – A Blob.

  • slice_tup_list – A list of slice tuple, indicate each dimension slice (start, stop, step).

  • name – A name for the operation (optional).

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

Note: Because the internal op of OneFlow does not support 0-dimension slice at present, we should set the zero element in slice_tup_list as None.

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp
@flow.global_function()
def slicev2_Job(x: tp.Numpy.Placeholder(shape=(3, 6, 9), dtype=flow.float32)
) -> tp.Numpy:
    slicev2_blob = flow.slice_v2(x,
                                slice_tup_list=[[None, None, None],
                                                [0, 5, 2], # slice in dimension 1, extract [0, 2, 4]
                                                [0, 6, 3]]) # slice in dimension 2, extract [0, 3]
    return slicev2_blob
x = np.random.randn(3, 6, 9).astype(np.float32)
out = slicev2_Job(x)

# out.shape (3, 3, 2)
oneflow.experimental.smooth_l1_loss(prediction: oneflow._oneflow_internal.BlobDesc, label: oneflow._oneflow_internal.BlobDesc, beta: float = 1.0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator computes the smooth l1 loss.

The equation is:

\[ \begin{align}\begin{aligned}& out = \frac{(\beta*x)^2}{2}, \left|x\right|<\frac{1}{{\beta}^2}\\& out = \left|x\right|-\frac{0.5}{{\beta}^2}, otherwise\end{aligned}\end{align} \]
Parameters
  • prediction (oneflow._oneflow_internal.BlobDesc) – The prediction Blob

  • label (oneflow._oneflow_internal.BlobDesc) – The label Blob

  • beta (float, optional) – The \(\beta\) in the equation. Defaults to 1.0.

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

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def smooth_l1_loss_Job(prediction: tp.Numpy.Placeholder((5, )),
                    label: tp.Numpy.Placeholder((5, ))
) -> tp.Numpy:
    return flow.smooth_l1_loss(prediction=prediction,
                            label=label)


prediction = np.array([0.1, 0.4, 0.3, 0.5, 0.9]).astype(np.float32)
label = np.array([0.3, 0.9, 2.5, 0.4, 0.3]).astype(np.float32)
out = smooth_l1_loss_Job(prediction, label)

# out [0.02       0.12499999 1.7        0.005      0.17999998]
oneflow.experimental.softmax(tensor, dim=None)

Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.

Softmax is defined as:

\[\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]

When the input Tensor is a sparse tensor then the unspecifed values are treated as -inf.

Shape:
  • Input: \((*)\) where * means, any number of additional dimensions

  • Output: \((*)\), same shape as the input

Returns

a Tensor of the same dimension and shape as the input with values in the range [0, 1]

Parameters

dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

For example:

import oneflow as flow
import numpy as np

m = flow.nn.Softmax(dim = 2)
x = flow.Tensor(
    np.array(
        [[[[-0.46716809,  0.40112534,  0.61984003],
        [-1.31244969, -0.42528763,  1.47953856]]],

        [[[ 1.02978742, -0.49383053,  1.88214159],
        [ 1.35351622, -1.46251285, -1.40751374]]]]
    )
)
y = m(x)
# [[[[0.6995764  0.6955959  0.29740235]
# [0.3004236  0.30440408 0.7025977 ]]]

# [[[0.4197673  0.7248568  0.96407217]
# [0.58023274 0.27514324 0.03592779]]]]
oneflow.experimental.sqrt(input)

Returns a new tensor with the square-root of the elements of input.

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
Parameters

input

import oneflow.experimental as flow
import numpy as np

arr = np.random.randn(3, 2, 5, 7)
input = flow.Tensor(arr)
output = flow.sqrt(input)
# output equal to np.sqrt(arr)
oneflow.experimental.square(input)

Returns a new tensor with the square of the elements of input.

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
Parameters

input

import oneflow.experimental as flow
import numpy as np

arr = np.random.randn(3, 2, 5, 7)
input = flow.Tensor(arr)
output = flow.square(input)
# output equal to np.square(arr)
oneflow.experimental.square_sum(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.squeeze(input, axis: Optional[Sequence[int]] = None)

This operator removes the specified dimention which size is 1 of the input Tensor. If the axis is not specified, this operator will remove all the dimention which size is 1 of the input Tensor.

The amount of element in return value is the same as Tensor input.

Parameters
  • input (oneflow.Tensor) – The input Tensor.

  • axis (Optional[Sequence[int]], optional) – The axis. Defaults to None.

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

Example:

import oneflow.experimental as flow
import numpy as np

input = flow.Tensor(np.array([[[[1, 1, 1]]]]).astype(np.int32))
out = flow.squeeze(input, axis=[1, 2]).numpy().shape

# out.shape (1, 3)
oneflow.experimental.ssp_variable_proxy(var: oneflow._oneflow_internal.BlobDesc, buffer_size: int = 1, name=None) → Tuple[oneflow._oneflow_internal.BlobDesc, oneflow._oneflow_internal.BlobDesc]

return ref_blob, value_blob

oneflow.experimental.stack(inputs: Sequence[oneflow._oneflow_internal.BlobDesc], axis: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator stacks the multiple Blobs on the specified axis.

Parameters
  • inputs (Sequence[oneflow._oneflow_internal.BlobDesc]) – A list of input Blob.

  • axis (int) – The stack axis.

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

For example:

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


@flow.global_function()
def stack_job(x: tp.Numpy.Placeholder(shape=(2, 4, 6)),
            y: tp.Numpy.Placeholder(shape=(2, 4, 6)))->tp.Numpy:
    out = flow.stack([x, y], axis=2)
    return out

x = np.ones(shape=(2, 4, 6), dtype=np.float32)
y = np.ones(shape=(2, 4, 6), dtype=np.float32)

out = stack_job(x, y)

# output.shape (2, 4, 2, 6)
Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

oneflow.experimental.std(tensor, dim, unbiased=True, keepdim=False)

Returns the standard-deviation of each row of the input tensor in the dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Parameters
  • input (Tensor) –

  • (int or tuple of python (dim) – ints) – the dimension or dimensions to reduce.

  • unbiased (bool) –

  • keepdim (bool) –

For example:

import oneflow.experimental as flow
import numpy as np

arr = np.random.randn(2, 3, 4, 5)
input = flow.Tensor(arr)
output = flow.std(input, dim=2)

# equal to numpy np.std(arr, axis=2)
oneflow.experimental.sub(x, y)

Computes the subtraction of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:

out = x - y

For example:

import oneflow.experimental as flow

# element-wise subtract
x = flow.Tensor(np.random.randn(2,3))
y = flow.Tensor(np.random.randn(2,3))
out = flow.sub(x,y).numpy()
print(out.shape) # (2,3)

# scalar subtract
x = 5
y = flow.Tensor(np.random.randn(2,3))
out = flow.sub(x,y).numpy()
print(out.shape) # (2,3)

# broadcast subtract
x = flow.Tensor(np.random.randn(1,1))
y = flow.Tensor(np.random.randn(2,3))
out = flow.sub(x,y).numpy()
print(out.shape) # (2,3)
oneflow.experimental.sum(input, dim=None, keepdims=False)

Computes the sum of row of elements in a tensor in the given axis, if the axis is None, sum of all elements will be caculated. For example:

import oneflow.experimental as flow
input = flow.Tensor(np.random.randn(4, 5, 6), dtype=flow.float32)
of_out = flow.sum(input, dim=(2,1))
oneflow.experimental.sync_default_session() → None

Synchronize the default session. Block until every synchronous OneFlow function and its callback finishes running.

oneflow.experimental.sync_dynamic_resize(inputs: oneflow._oneflow_internal.BlobDesc, size: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
Parameters
  • inputs (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • size (oneflow._oneflow_internal.BlobDesc) – The size of new Blob.

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

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def sync_dynamic_resize_Job(x: tp.Numpy.Placeholder(shape=(4, 3), dtype=flow.float32),
                            size: tp.Numpy.Placeholder(shape=(1, ), dtype=flow.int32),
) -> tp.ListNumpy:
    resize_Blob = flow.sync_dynamic_resize(inputs=x,
                                        size=size)
    return resize_Blob

x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [10, 11, 12]]).astype(np.float32)
size = np.array([2]).astype(np.int32)
out = sync_dynamic_resize_Job(x, size)

# out [array([[1., 2., 3.],
#             [4., 5., 6.]], dtype=float32)]
oneflow.experimental.tanh(x)

This operator computes the hyperbolic tangent value of Tensor.

The equation is:

\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]
Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

import oneflow as flow
import numpy as np

x = np.array([-1, 0, 1]).astype(np.float32)
input = flow.Tensor(x)
tanh = flow.nn.Tanh()
out = tanh(input).numpy()

# out [-0.7615942  0.         0.7615942]
oneflow.experimental.tensor_buffer_to_list_of_tensors(x: oneflow._oneflow_internal.BlobDesc, out_shape: Sequence[int], out_dtype: oneflow._oneflow_internal.dtype, dynamic_out: Optional[bool] = False, name: Optional[str] = None) → List[oneflow._oneflow_internal.BlobDesc]

This operator converts the Blob of TensorBuffer to list of Tensors. Every element in x will be converted to a Tensor and output will be flatten to a list.

Parameters
  • x (BlobDesc) – Input Blob, data type must be tensor buffer.

  • out_shape (Sequence[int]) – max shape for a tensor buffer in x

  • out_dtype (flow.dtype,) – output data type

  • dynamic_out (Optional[bool]) – if output is dynamic blob. Default to False.

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

Returns

result blobs

Return type

List[BlobDesc]

For example: .. code-block:: python

# the same with gen_tensor_buffer op

oneflow.experimental.tensor_buffer_to_tensor(x: oneflow._oneflow_internal.BlobDesc, dtype: oneflow._oneflow_internal.dtype, instance_shape: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator converts the Blob’s type from TensorBuffer to Tensor. Some operator’s output data type is TensorBuffer, you can use this operator to convert back to Tensor.

Refer to Concept Explanation for more about TensorBuffer.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – Input Blob.

  • dtype (flow.dtype) – The data dtype.

  • instance_shape (Sequence[int]) – The shape of each TensorBuffer instance.

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

Returns

A Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_buffer_to_tensor_Job(x: tp.Numpy.Placeholder(shape=(4, 16, 64, 64), dtype=flow.float32),
) -> tp.Numpy:
    x = flow.tensor_to_tensor_buffer(x,
                                    instance_dims=2)
    return flow.tensor_buffer_to_tensor(x,
                                        instance_shape=(64, 64),
                                        dtype=flow.float)

x = np.random.randn(4, 16, 64, 64).astype(np.float32)
out = tensor_buffer_to_tensor_Job(x)

# out.shape (4, 16, 64, 64)
oneflow.experimental.tensor_scatter_nd_add(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, updates: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator adds elements from ‘updates’ to Blob ‘params’ based on the indices.

Parameters
  • params (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • indices (oneflow._oneflow_internal.BlobDesc) – The indice of updates. Its type should be flow.int32.

  • updates (oneflow._oneflow_internal.BlobDesc) – The update Blob.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_scatter_nd_add_Job(x: tp.Numpy.Placeholder(shape=(5, 3), dtype=flow.float32),
                            indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                            update: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.tensor_scatter_nd_add(params=x,
                                            indices=indice,
                                            updates=update)
    return scatter_blob

x = np.array([[1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3]]).astype(np.float32)
indice_array = np.array([[0], [4], [2]]).astype(np.int32)
update_array = np.array([[1, 1, 1],
                        [2, 2, 2],
                        [3, 3, 3]]).astype(np.float32)
out = tensor_scatter_nd_add_Job(x, indice_array, update_array)

# out [[2. 3. 4.]
#      [1. 2. 3.]
#      [4. 5. 6.]
#      [1. 2. 3.]
#      [3. 4. 5.]]
oneflow.experimental.tensor_scatter_nd_update(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, updates: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator inserts the elements in updates according to the indices into the Blob params.

Parameters
  • params (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • indices (oneflow._oneflow_internal.BlobDesc) – The indice of updates. Its type should be flow.int32.

  • updates (oneflow._oneflow_internal.BlobDesc) – The update Blob.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_scatter_nd_Job(x: tp.Numpy.Placeholder(shape=(5, 3), dtype=flow.float32),
                        indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                        update: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.tensor_scatter_nd_update(params=x,
                                                indices=indice,
                                                updates=update)
    return scatter_blob

x = np.array([[1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3]]).astype(np.float32)
indice_array = np.array([[0], [4], [2]]).astype(np.int32)
update_array = np.array([[1, 1, 1],
                        [2, 2, 2],
                        [3, 3, 3]]).astype(np.float32)
out = tensor_scatter_nd_Job(x, indice_array, update_array)

# out [[1. 1. 1.]
#      [1. 2. 3.]
#      [3. 3. 3.]
#      [1. 2. 3.]
#      [2. 2. 2.]]
oneflow.experimental.tensor_to_tensor_buffer(x: oneflow._oneflow_internal.BlobDesc, instance_dims: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator converts the Blob’s type from Tensor to TensorBuffer.

Refer to Concept Explanation for more about TensorBuffer.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – Input Blob.

  • instance_dims (int) – The dimensions of dynamic tensor instance.

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

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_buffer_to_tensor_Job(x: tp.Numpy.Placeholder(shape=(4, 16, 64, 64), dtype=flow.float32),
) -> tp.Numpy:
    x = flow.tensor_to_tensor_buffer(x,
                                    instance_dims=2)
    return flow.tensor_buffer_to_tensor(x,
                                        instance_shape=(64, 64),
                                        dtype=flow.float)

x = np.random.randn(4, 16, 64, 64).astype(np.float32)
out = tensor_buffer_to_tensor_Job(x)

# out.shape (4, 16, 64, 64)
oneflow.experimental.transpose(tensor, perm: Sequence[int] = None)

This operator transposes the specified axis of input Tensor. :param tensor: The input tensor. :type tensor: oneflow.Tensor :param perm: The list of dimension permutation. Defaults to None. :type perm: Sequence[int], optional

Returns

A transposed tensor.

Return type

oneflow.Tensor

For example: .. code-block:: python

import oneflow.experimental as flow import numpy as np

input = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) out = flow.transpose(input, perm=(0, 2, 3, 1))

# out.shape (2, 5, 3, 6)

oneflow.experimental.truncated_normal(stddev: float = 1.0) → oneflow.core.job.initializer_conf_pb2.InitializerConf
oneflow.experimental.truncated_normal_initializer(mean: float = 0.0, stddev: float = 1.0) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a truncated normal distribution.

Parameters
  • mean (float, optional) – A scalar (float). Defaults to 0.0.

  • stddev (float, optional) – A scalar (float). Defaults to 1.0.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def truncated_normal_Job() -> None:
    init = flow.truncated_normal_initializer(mean=1, stddev=1)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
truncated_normal_Job()

# out [1.8303236  0.09787154 0.83049864]

Example 2:

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


@flow.global_function()
def conv2d_truncated_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.truncated_normal_initializer(mean=0, stddev=1)

    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_truncated_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.unique_with_counts(x: oneflow.python.framework.input_blob_def.ArgBlobDef, out_idx: oneflow._oneflow_internal.dtype = oneflow.int32, name: Optional[str] = None) → Tuple[oneflow._oneflow_internal.BlobDesc]
oneflow.experimental.unpack(input: oneflow._oneflow_internal.BlobDesc, unpack_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.experimental.unsorted_batch_segment_sum(data: oneflow._oneflow_internal.BlobDesc, segment_ids: oneflow._oneflow_internal.BlobDesc, num_segments: int, name: Optional[str] = None) → oneflow._oneflow_internal.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._oneflow_internal.BlobDesc) – Input Blob

  • segment_ids (oneflow._oneflow_internal.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._oneflow_internal.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.experimental.unsorted_segment_sum(data: oneflow._oneflow_internal.BlobDesc, segment_ids: oneflow._oneflow_internal.BlobDesc, num_segments: int, axis: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Computes the sum along segments of a Blob.

Parameters
  • data (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • segment_ids (oneflow._oneflow_internal.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._oneflow_internal.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.experimental.unsorted_segment_sum_like(data: oneflow._oneflow_internal.BlobDesc, segment_ids: oneflow._oneflow_internal.BlobDesc, like: oneflow._oneflow_internal.BlobDesc, axis: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

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

Parameters
  • data (oneflow._oneflow_internal.BlobDesc) – Input Blob

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

  • like (oneflow._oneflow_internal.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._oneflow_internal.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.experimental.unsqueeze(input, dim)

Returns a new tensor with a dimension of size one inserted at the specified position.

The returned tensor shares the same underlying data with this tensor.

A dim value within the range [-input.ndimension() - 1, input.ndimension() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.ndimension() + 1.

Parameters
  • input (Tensor) –

  • dim (int) – the index at which to insert the singleton dimension

For example:

import numpy as np
import oneflow as flow

x = flow.Tensor(np.random.rand(2, 3, 4))
y = x.unsqueeze(2)
oneflow.experimental.user_op_builder(op_name)

Build a wrapper of user op.

For instance::
def myargmax(

input: oneflow._oneflow_internal.BlobDesc) -> oneflow._oneflow_internal.BlobDesc: return ( flow.user_op_builder(“myargmax”) .Op(“argmax”) .Input(“in”, [input]) .Output(“out”) .Build() .InferAndTryRun() .RemoteBlobList()[0] )

Parameters

op_name (str) – name of new user op

Returns

UserOpConfBuilder object used to build a wrapper of user op.

Return type

UserOpConfBuilder

oneflow.experimental.user_op_module_builder(op_type_name)
oneflow.experimental.variance_scaling_initializer(scale: float = 1.0, mode: str = 'fan_in', distribution: str = 'truncated_normal', data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a truncated normal distribution or a random normal distribution or a random uniform distribution with a scale adapting to it.

When the distribution is “truncated_normal”

The equation is:

\[W\sim N(0, \sqrt{\frac{{scale}}{{n}}})\]

If mode is “fan_in”, the “n” is the number of input units in the weight Blob.

If mode is “fan_out”, the “n” is the number of output units in the weight Blob.

if mode is “fan_avg”, the “n” is the average of the number of input and output units in the weight Blob

Parameters
  • scale (float, optional) – Scaling factor (positive float). Defaults to 1.0.

  • mode (str, optional) – One of “fan_in”, “fan_out”, “fan_avg”. Defaults to “fan_in”.

  • distribution (str, optional) – Random distribution to use. One of “truncated_normal”,. Defaults to “truncated_normal”.

  • data_format (str, optional) – A string be one of “N…C” or “NC…”. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def variance_scale_Job() -> None:
    init = flow.variance_scaling_initializer(scale=2.0, mode="fan_avg")
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
variance_scale_Job()

# out [[-0.13931477  0.12266728 -0.9434968 ]
#      [-0.49665168  0.10231158 -0.19194333]
#      [-0.7902896  -1.7034698  -0.38695997]]

Example 2:

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


@flow.global_function()
def conv2d_variance_scaling_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.variance_scaling_initializer(mode="fan_out")
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_variance_scaling_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.watch(blob_watched: oneflow._oneflow_internal.BlobDesc, handler_or_prompt: Union[Callable, str, None] = None) → None

Register callback for a blob. The callback function will be called after the computation produce the blob finishes. We can use it to watch the values of Blob.

Parameters
  • blob_watched – a Blob

  • handler_or_prompt – a function has an argument of a Blob

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def watch_Job() -> None:
    init = flow.constant_initializer(2.5)
    variable = flow.get_variable(
        "variable-weight",
        shape=(5, ),
        initializer=init,
        trainable=True
    )
    flow.watch(variable, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
watch_Job()

# out [2.5 2.5 2.5 2.5 2.5]

Example 2:

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

def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def watch_Job(x: tp.Numpy.Placeholder((1, 3, 2, 2))
) -> None:
    initializer = flow.truncated_normal(0.1)
    conv2d = flow.layers.conv2d(
        x,
        filters=3,
        kernel_size=1,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )

    flow.watch(conv2d, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
x = np.ones(shape=(1, 3, 2, 2)).astype(np.float32)
watch_Job(x)

# out [[[[ 0.03757111  0.03757111]
#        [ 0.03757111  0.03757111]]

#       [[-0.36131713 -0.36131713]
#        [-0.36131713 -0.36131713]]

#       [[-0.12266113 -0.12266113]
#        [-0.12266113 -0.12266113]]]]
oneflow.experimental.watch_diff(blob_watched: oneflow._oneflow_internal.BlobDesc, handler_or_prompt: Union[Callable, str, None] = None) → None

Register callback for gradient of a blob. The callback will be called after the computation produce the gradient blob finishes.

Parameters
  • blob_watched – a Blob

  • handler_or_prompt – a function has an argument of a Blob

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


BATCH_SIZE = 20

def watch_diff_handler(blob: tp.Numpy):
    print("watch_diff_handler:", blob, blob.shape, blob.dtype)

@flow.global_function(type="train")
def train_job(
    images: tp.Numpy.Placeholder((BATCH_SIZE, 1, 28, 28), dtype=flow.float),
    labels: tp.Numpy.Placeholder((BATCH_SIZE,), dtype=flow.int32),
) -> tp.Numpy:
    initializer = flow.truncated_normal(0.1)
    with flow.scope.placement("gpu", "0:0"):
        reshape = flow.reshape(images, [images.shape[0], -1])
        hidden = flow.layers.dense(
            reshape,
            512,
            activation=flow.nn.relu,
            kernel_initializer=initializer,
            name="hidden",
        )
        logits = flow.layers.dense(
            hidden, 10, kernel_initializer=initializer, name="output"
        )
        loss = flow.nn.sparse_softmax_cross_entropy_with_logits(labels, logits, name="softmax_loss")

    lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0.1])
    flow.optimizer.SGD(lr_scheduler, momentum=0).minimize(loss)
    flow.watch_diff(logits, watch_diff_handler)
    return loss


if __name__ == "__main__":
    checkpoint = flow.train.CheckPoint()
    checkpoint.init()
    (train_images, train_labels), (test_images, test_labels) = flow.data.load_mnist(
            BATCH_SIZE
    )
    for i, (images, labels) in enumerate(zip(train_images, train_labels)):
        loss = train_job(images, labels)


# watch_diff_handler: [[-1.88834548e-01  2.71021971e-03  2.28271242e-02  7.17673637e-03
#                       4.10183379e-03  8.93106461e-02  2.23669074e-02  3.86103359e-03
#                       3.12465224e-02  5.23346756e-03] .....

Example 2:

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


BATCH_SIZE = 20

def watch_diff_handler(blob: tp.Numpy):
    print("watch_diff_handler:", blob)


@flow.global_function(type="train")
def watch_matmul_diff_job(
    images: tp.Numpy.Placeholder((3, 3), dtype=flow.float),
) -> None:
    with flow.scope.placement("cpu", "0:0"):
        weight_initializer = flow.constant_initializer(2)
        weight_shape = (3, BATCH_SIZE)
        weight = flow.get_variable(
            "matmultest-weight",
            shape=weight_shape,
            initializer=weight_initializer)
        output = flow.linalg.matmul(images, weight)

    lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0.1])
    flow.optimizer.SGD(lr_scheduler, momentum=0.9).minimize(output)
    flow.watch_diff(weight, watch_diff_handler)


if __name__ == "__main__":
    check_point = flow.train.CheckPoint()
    check_point.init()

    x = np.array([[1, 1, 1],
                [1, 1, 1],
                [1, 1, 1]]).astype(np.float32)
    watch_matmul_diff_job(x)

# watch_diff_handler: [[3. 3. 3.]
#                      [3. 3. 3.]
#                      [3. 3. 3.]]

Example 3:

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


def watch_diff_handler(blob: tp.Numpy):
    print("watch_diff_handler:", blob, blob.shape, blob.dtype)


@flow.global_function(type="train")
def watch_conv_diff_job(
    images: tp.Numpy.Placeholder((1, 1, 4, 4), dtype=flow.float),
) -> None:
    with flow.scope.placement("gpu", "0:0"):
        weight_shape = (1, 1, 3, 3)
        weight_initializer = flow.truncated_normal(0.1)
        weight = flow.get_variable(
            name="conv-weight",
            shape=weight_shape,
            initializer=weight_initializer
        )
        output = flow.nn.conv2d(images, weight, strides=1, padding="VALID")

    lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0.1])
    flow.optimizer.SGD(lr_scheduler, momentum=0.9).minimize(output)
    flow.watch_diff(weight, watch_diff_handler)


if __name__ == "__main__":
    check_point = flow.train.CheckPoint()
    check_point.init()

    x = np.array([[[[ 1.,  2.,  3.,  4.],
                    [ 5.,  6.,  7.,  8.],
                    [ 9., 10., 11., 12.],
                    [13., 14., 15., 16.]]]]).astype(np.float32)

    watch_conv_diff_job(x)

# watch_diff_handler: [[[[14. 18. 22.]
#                        [30. 34. 38.]
#                        [46. 50. 54.]]]]
oneflow.experimental.where(condition, x, y)

Return a tensor of elements selected from either x or y, depending on condition. If the element in condition is larger than 0,

it will take the x element, else it will take the y element

The tensors condition, x, y must be broadcastable. it will take the x element, else it will take the y element. :param condition: When 1 (nonzero), yield x, otherwise yield y :type condition: IntTensor :param x: value (if :attr:x is a scalar) or values selected at indices

where condition is True

Parameters

y (Tensor or Scalar) – value (if :attr:x is a scalar) or values selected at indices where condition is False

Returns

A tensor of shape equal to the broadcasted shape of condition, x, y

Return type

Tensor

For example: .. code-block:: python

import flow.experimental as flow

x = flow.Tensor(

np.array([[-0.4620, 0.3139], [0.3898, -0.7197], [0.0478, -0.1657]]), dtype=flow.float32,

) y = flow.Tensor(np.ones(shape=(3, 2)), dtype=flow.float32) condition = flow.Tensor(np.array([[0, 1], [1, 0], [1, 0]]), dtype=flow.int32) of_out = condition.where(x, y) # of_out # [[1. 0.3139] # [0.3898 1. ] # [0.0478 1. ]]

oneflow.experimental.xavier_normal_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier normal distribution.

It also can be called as oneflow.glorot_normal_initializer.

The equation is:

\[W\sim N(0, \sqrt{\frac{{2}}{{n_j+n_{j+1}}}})\]

\(N\) means normal distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_normal_Job() -> None:
    init = flow.xavier_normal_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_normal_Job()

# out [[ 0.5908121  -0.10804518 -0.6148571 ]
#      [ 1.4007381  -0.08172473  0.36579943]
#      [-0.6461796  -0.15923311  0.33653972]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_normal_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.xavier_uniform_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier uniform distribution.

It also can be called as oneflow.glorot_uniform_initializer.

The equation is:

\[W\sim U(-\sqrt{\frac{{6}}{{n_j+n_{j+1}}}},\sqrt{\frac{{6}}{{n_j+n_{j+1}}}})\]

\(U\) means uniform distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_uniform_Job() -> None:
    init = flow.xavier_uniform_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_uniform_Job()

# out [[-0.14424723 -0.9532095  -0.08723891]
#      [-0.8011227  -0.29729813 -0.26769108]
#      [ 0.9208976  -0.5971756  -0.15077025]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_uniform_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_uniform_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_uniform_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.zero_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using zeros.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

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

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3), const_value)
) -> tp.Numpy:
    return flow.constant_pad2d(x, padding=[2, 2, 1, 1], const_value)


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
const_value = 1.5
out = pad_Job(x, const_value)

# out [[[[ 0.  0.  0.  0.  0.  0.  0.]
#    [ 0.  0.  0.  1.  2.  0.  0.]
#    [ 0.  0.  3.  4.  5.  0.  0.]
#    [ 0.  0.  6.  7.  8.  0.  0.]
#    [ 0.  0.  0.  0.  0.  0.  0.]]

#   [[ 0.  0.  0.  0.  0.  0.  0.]
#    [ 0.  0.  9.  10.  11.  0.  0.]
#    [ 0.  0.  12.  13.  14.  0.  0.]
#    [ 0.  0.  15.  16.  17.  0.  0.]
#    [ 0.  0.  0.  0.  0.  0.  0.]]]]
oneflow.experimental.zeros(size, dtype=None)

Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.

Parameters
  • size (an integer or tuple of integer values) – defining the shape of the output tensor.

  • be a variable number of arguments or a collection like a list or tuple. (Can) –

For example:

import oneflow.experimental as flow

y = flow.zeros(5)
# [0. 0. 0. 0. 0. ]
oneflow.experimental.zeros_initializer(dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blobs initialized to 0

Parameters

dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Returns

constant_initializer

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def zeros_Job() -> None:
    init = flow.zeros_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
zeros_Job()

# out [0. 0. 0.]

Example 2:

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


@flow.global_function()
def conv2d_zero_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.zeros_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_zero_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.experimental.zeros_like(other)

Returns a tensor filled with the scalar value 0, with the same size as input. flow.zeros_like(input) is equivalent to flow.zeros(input.shape, dtype=input.dtype)

Parameters

other (Tensor) – The size of input will determine size of the output tensor.

For example:

import oneflow.experimental as flow
import numpy as np

x = flow.Tensor(np.random.rand([5]))
y = flow.zeros_like(x)
# [0. 0. 0. 0. 0. ]