MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / MLP

Class MLP

detrsmpl/models/heads/expose_head.py:90–146  ·  view source on GitHub ↗

MLP Args: input_dim (int): Input dim of MLP. output_dim (int): Output dim of MLP. layers (List): Layer dims. activ_type (str): Activation layer type. dropout (float): Dropout. gain (float): Xavier init gain value.

Source from the content-addressed store, hash-verified

88
89
90class MLP(nn.Module):
91 """MLP
92 Args:
93 input_dim (int): Input dim of MLP.
94 output_dim (int): Output dim of MLP.
95 layers (List): Layer dims.
96 activ_type (str): Activation layer type.
97 dropout (float): Dropout.
98 gain (float): Xavier init gain value.
99 """
100 def __init__(
101 self,
102 input_dim: int,
103 output_dim: int,
104 layers: List[int] = [],
105 activ_type: str = 'relu',
106 dropout: float = 0.5,
107 gain: float = 0.01,
108 ):
109 super(MLP, self).__init__()
110 curr_input_dim = input_dim
111 self.num_layers = len(layers)
112
113 self.blocks = nn.ModuleList()
114 for layer_idx, layer_dim in enumerate(layers):
115 if activ_type == 'none':
116 active = None
117 else:
118 active = build_activation_layer(
119 cfg=dict(type=activ_type, inplace=True))
120 linear = nn.Linear(curr_input_dim, layer_dim, bias=True)
121 curr_input_dim = layer_dim
122
123 layer = []
124 layer.append(linear)
125
126 if active is not None:
127 layer.append(active)
128
129 if dropout > 0.0:
130 layer.append(nn.Dropout(dropout))
131
132 block = nn.Sequential(*layer)
133 self.add_module('layer_{:03d}'.format(layer_idx), block)
134 self.blocks.append(block)
135
136 self.output_layer = nn.Linear(curr_input_dim, output_dim)
137 initialize(self.output_layer,
138 init_cfg=dict(type='Xavier',
139 gain=gain,
140 distribution='uniform'))
141
142 def forward(self, module_input):
143 curr_input = module_input
144 for block in self.blocks:
145 curr_input = block(curr_input)
146 return self.output_layer(curr_input)
147

Callers 1

load_regressorMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected