L2#

class usencrypt.ai.regularizers.L2(l2=0.01)#

Applies an L2 regularization penalty, defined as l2 * numpy.sum(x * x).

Parameters

l2 (float) – The L2 regularization factor. Defaults to 0.01.

Call arguments
Returns

The L2 regularization of the input values.

Return type

numpy.ndarray of float or usencrypt.cipher.Float

Inheritance
Examples

The L2 regularization penalty can be passed into supported layers (e.g., usencrypt.ai.nn.layers.FCLayer) as either a weight_regularizer or bias_regularizer argument, as follows:

>>> import usencrypt as ue
>>> l2_reg = ue.ai.regularizers.L2()
>>> layer = ue.ai.nn.layers.FCLayer(3, 4, weight_regularizer=l2_reg)

Further, the regularization penalty can be called directly:

>>> import numpy as np
>>> import usencrypt as ue
>>> x = np.random.rand(1, 3)
>>> x
array([[0.46363754, 0.69529382, 0.21814414]])
>>> ue_x = ue.encrypt(x)
>>> ue_x
array([[(0xffff80eb7970) Float: 0xf49ae86bff5c716598da26a6d4e41081f87ebc8f67ad9a5c83baa9f8835f5a01,
        (0xffff80eb7a30) Float: 0xa0a4f574e595f1d59d1e91a9a27d18dfb405386acefe1ccbb174a16387492562,
        (0xffff80eb7940) Float: 0xcd1f75027943eb1d42d805abdaa260449ac8008be3433c5be82638f4d2e1d749]],
     dtype=object)
>>> l2 = ue.ai.regularizers.L2(l2=0.01)
>>> reg = l2(x)
>>> reg
0.007459801297022889
>>> ue_reg = l2(ue_x)
>>> ue_reg
(0xffff80eb7c40) Float: 0x228573b5e5c22be9d7349302c68c39abc3c82a9ae5499e12dbee21469f5530bd
>>> ue.decrypt(ue_reg)
0.007459799526259303