Create a linear layer whose weights are initialized with a chosen method. The usage of the layer is the same as torch.nn.Linear. Args: in_features: Size of the last dimension of the input tensor. out_features: Size of
(
self,
in_features: int,
out_features: int,
bias: bool = True,
w_init_gain: str = "linear",
init_method: str = "xavier_normal",
lrelu_nslope: float = 0.01,
kaiming_fan_mode: str = "fan_in",
bias_init_val: float = 0.0,
lr_multiplier: float = 1.0,
fixed_bias: float = None,
)
| 23 | _FLOAT_MODULE = nn.Linear |
| 24 | |
| 25 | def __init__( |
| 26 | self, |
| 27 | in_features: int, |
| 28 | out_features: int, |
| 29 | bias: bool = True, |
| 30 | w_init_gain: str = "linear", |
| 31 | init_method: str = "xavier_normal", |
| 32 | lrelu_nslope: float = 0.01, |
| 33 | kaiming_fan_mode: str = "fan_in", |
| 34 | bias_init_val: float = 0.0, |
| 35 | lr_multiplier: float = 1.0, |
| 36 | fixed_bias: float = None, |
| 37 | ): |
| 38 | """ |
| 39 | Create a linear layer whose weights are initialized with a chosen method. |
| 40 | The usage of the layer is the same as torch.nn.Linear. |
| 41 | |
| 42 | Args: |
| 43 | in_features: |
| 44 | Size of the last dimension of the input tensor. |
| 45 | out_features: |
| 46 | Size of the last dimension of the output tensor. |
| 47 | bias: |
| 48 | Whether to add a learnable bias term, one for each out_features. |
| 49 | w_init_gain: |
| 50 | The nonlinearity that is designed to be added after the layer. |
| 51 | Check :py:func:`cdslib.nn.init_weight` for supported nonlinearities. |
| 52 | Note that the layer does not add nonlinearity. |
| 53 | init_method: |
| 54 | The initialization method. Check :py:func:`cdslib.nn.init_weight`. |
| 55 | lrelu_nslope: |
| 56 | The negative slope of leaky-relu if leaky-relu is used. Check :py:func:`cdslib.nn.init_weight`. |
| 57 | kaiming_fan_mode: |
| 58 | The fan mode if kaiming_* init method is used. Check :py:func:`cdslib.nn.init_weight`. |
| 59 | bias_init_val: |
| 60 | Initialization value of the bias. |
| 61 | lr_multiplier: |
| 62 | A scalar to be multiplied with the learning rate. |
| 63 | For example, if lr_multiplier = 0.1, both the weight and the bias will update at 1/10 of the rate. |
| 64 | fixed_bias (float): |
| 65 | A fixed bias value to add to the output. If `None`, nothing is added. |
| 66 | """ |
| 67 | nn.Linear.__init__(self, in_features, out_features, bias) |
| 68 | nn_utils.init_weight( |
| 69 | self.weight, |
| 70 | w_init_gain=w_init_gain, # leaky_relu, relu, tanh, sigmoid, ... |
| 71 | init_method=init_method, |
| 72 | lrelu_nslope=lrelu_nslope, |
| 73 | kaiming_fan_mode=kaiming_fan_mode, |
| 74 | ) |
| 75 | # init bias |
| 76 | if self.bias is not None: |
| 77 | nn.init.constant_(self.bias, bias_init_val) |
| 78 | |
| 79 | # learning rate multiplier (make the layer update slower or faster) |
| 80 | self.lr_multiplier = lr_multiplier |
| 81 | |
| 82 | # add constant at the end |