MCPcopy Create free account
hub / github.com/apple/ml-pointersect / LinearLayer

Class LinearLayer

cdslib/core/nn/modules/linear.py:18–102  ·  view source on GitHub ↗

A helper class that wraps around the typical nn.Linear to help initialize it.

Source from the content-addressed store, hash-verified

16
17
18class LinearLayer(nn.Linear):
19 """
20 A helper class that wraps around the typical nn.Linear to help initialize it.
21 """
22
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

Callers 2

__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected