oneflow.regularizers

Regularizers

oneflow.regularizers.l1(l: float = 0.01) → oneflow.core.job.regularizer_conf_pb2.RegularizerConf

This operator creates a L1 weight regularizer.

Parameters

l (float, optional) – The L1 regularization coefficient. Defaults to 0.01.

Returns

A regularizer that can be used in other layers or operators.

Return type

regularizer_conf_util.RegularizerConf

For example:

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


@flow.global_function()
def conv2d_l1_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.truncated_normal(0.1)
    regularizer = flow.regularizers.l1(l=0.001)
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        kernel_regularizer=regularizer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_l1_Job(x)
oneflow.regularizers.l1_l2(l1: float = 0.01, l2: float = 0.01) → oneflow.core.job.regularizer_conf_pb2.RegularizerConf

This operator creates a L1 and L2 weight regularizer.

Parameters
  • l1 (float, optional) – The L1 regularization coefficient. Defaults to 0.01.

  • l2 (float, optional) – The L2 regularization coefficient. Defaults to 0.01.

Returns

A regularizer that can be used in other layers or operators.

Return type

regularizer_conf_util.RegularizerConf

For example:

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


@flow.global_function()
def conv2d_l1_l2_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.truncated_normal(0.1)
    regularizer = flow.regularizers.l1_l2(l1=0.001, l2=0.001)
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        kernel_regularizer=regularizer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_l1_l2_Job(x)
oneflow.regularizers.l2(l: float = 0.01) → oneflow.core.job.regularizer_conf_pb2.RegularizerConf

This operator creates a L2 weight regularizer.

Parameters

l (float, optional) – The L2 regularization coefficient. Defaults to 0.01.

Returns

A regularizer that can be used in other layers or operators.

Return type

regularizer_conf_util.RegularizerConf

For example:

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


@flow.global_function()
def conv2d_l2_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.truncated_normal(0.1)
    regularizer = flow.regularizers.l2(l=0.001)
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        kernel_regularizer=regularizer,
        name="Conv2d"
    )
    return conv2d


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