| 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 |