@@ -314,8 +314,8 @@ def MLP(
314
314
norm_type : str = None ,
315
315
use_dropout : bool = False ,
316
316
dropout_probability : float = 0.5 ,
317
- output_activation : nn . Module = None ,
318
- output_norm_type : str = None ,
317
+ output_activation : bool = True ,
318
+ output_norm : bool = True ,
319
319
last_linear_layer_init_zero : bool = False
320
320
):
321
321
r"""
@@ -328,15 +328,18 @@ def MLP(
328
328
- hidden_channels (:obj:`int`): Number of channels in the hidden tensor.
329
329
- out_channels (:obj:`int`): Number of channels in the output tensor.
330
330
- layer_num (:obj:`int`): Number of layers.
331
- - layer_fn (:obj:`Callable`): layer function.
332
- - activation (:obj:`nn.Module`): the optional activation function.
333
- - norm_type (:obj:`str`): type of the normalization.
334
- - use_dropout (:obj:`bool`): whether to use dropout in the fully-connected block.
335
- - dropout_probability (:obj:`float`): probability of an element to be zeroed in the dropout. Default: 0.5.
336
- - output_activation (:obj:`nn.Module`): the optional activation function in the last layer.
337
- - output_norm_type (:obj:`str`): type of the normalization in the last layer.
338
- - last_linear_layer_init_zero (:obj:`bool`): zero initialization for the last linear layer (including w and b),
339
- which can provide stable zero outputs in the beginning.
331
+ - layer_fn (:obj:`Callable`): Layer function.
332
+ - activation (:obj:`nn.Module`): The optional activation function.
333
+ - norm_type (:obj:`str`): The type of the normalization.
334
+ - use_dropout (:obj:`bool`): Whether to use dropout in the fully-connected block.
335
+ - dropout_probability (:obj:`float`): The probability of an element to be zeroed in the dropout. Default: 0.5.
336
+ - output_activation (:obj:`bool`): Whether to use activation in the output layer. If True,
337
+ we use the same activation as front layers. Default: True.
338
+ - output_norm (:obj:`bool`): Whether to use normalization in the output layer. If True,
339
+ we use the same normalization as front layers. Default: True.
340
+ - last_linear_layer_init_zero (:obj:`bool`): Whether to use zero initializations for the last linear layer
341
+ (including w and b), which can provide stable zero outputs in the beginning,
342
+ usually used in the policy network in RL settings.
340
343
Returns:
341
344
- block (:obj:`nn.Sequential`): a sequential list containing the torch layers of the fully-connected block.
342
345
@@ -361,30 +364,31 @@ def MLP(
361
364
if use_dropout :
362
365
block .append (nn .Dropout (dropout_probability ))
363
366
364
- # the last layer
367
+ # The last layer
365
368
in_channels = channels [- 2 ]
366
369
out_channels = channels [- 1 ]
367
- if output_activation is None and output_norm_type is None :
368
- # the last layer use the same norm and activation as front layers
369
- block .append (layer_fn (in_channels , out_channels ))
370
+ block .append (layer_fn (in_channels , out_channels ))
371
+ """
372
+ In the final layer of a neural network, whether to use normalization and activation are typically determined
373
+ based on user specifications. These specifications depend on the problem at hand and the desired properties of
374
+ the model's output.
375
+ """
376
+ if output_norm is True :
377
+ # The last layer uses the same norm as front layers.
370
378
if norm_type is not None :
371
379
block .append (build_normalization (norm_type , dim = 1 )(out_channels ))
380
+ if output_activation is True :
381
+ # The last layer uses the same activation as front layers.
372
382
if activation is not None :
373
383
block .append (activation )
374
- if use_dropout :
375
- block .append (nn .Dropout (dropout_probability ))
376
- else :
377
- # the last layer use the specific norm and activation
378
- block .append (layer_fn (in_channels , out_channels ))
379
- if output_norm_type is not None :
380
- block .append (build_normalization (output_norm_type , dim = 1 )(out_channels ))
381
- if output_activation is not None :
382
- block .append (output_activation )
383
- if use_dropout :
384
- block .append (nn .Dropout (dropout_probability ))
385
- if last_linear_layer_init_zero :
386
- block [- 2 ].weight .data .fill_ (0 )
387
- block [- 2 ].bias .data .fill_ (0 )
384
+
385
+ if last_linear_layer_init_zero :
386
+ # Locate the last linear layer and initialize its weights and biases to 0.
387
+ for _ , layer in enumerate (reversed (block )):
388
+ if isinstance (layer , nn .Linear ):
389
+ nn .init .zeros_ (layer .weight )
390
+ nn .init .zeros_ (layer .bias )
391
+ break
388
392
389
393
return sequential_pack (block )
390
394
0 commit comments