Function to change the mode of the dropout layers (bool: train_mode -> train or evaluation) Args: model: Pytorch model dropout_layer_indexes: Indexes of the dropout layers which should be activated Choose indices from : list(torch_model.mod
(model, dropout_layer_indexes: list, train_mode: bool)
| 403 | |
| 404 | |
| 405 | def set_dropout_mode(model, dropout_layer_indexes: list, train_mode: bool): |
| 406 | """ |
| 407 | Function to change the mode of the dropout layers (bool: train_mode -> train or evaluation) |
| 408 | |
| 409 | Args: |
| 410 | model: Pytorch model |
| 411 | dropout_layer_indexes: Indexes of the dropout layers which should be activated |
| 412 | Choose indices from : list(torch_model.modules()) |
| 413 | train_mode: boolean, true <=> train_mode, false <=> evaluation_mode |
| 414 | """ |
| 415 | |
| 416 | modules = list(model.modules()) # list of all modules in the network. |
| 417 | |
| 418 | if len(dropout_layer_indexes) != 0: |
| 419 | for index in dropout_layer_indexes: |
| 420 | layer = modules[index] |
| 421 | if layer.__class__.__name__.startswith('Dropout'): |
| 422 | if True == train_mode: |
| 423 | layer.train() |
| 424 | elif False == train_mode: |
| 425 | layer.eval() |
| 426 | else: |
| 427 | raise KeyError( |
| 428 | "The passed index: {} is not a Dropout layer".format(index)) |
| 429 | |
| 430 | else: |
| 431 | for module in modules: |
| 432 | if module.__class__.__name__.startswith('Dropout'): |
| 433 | if True == train_mode: |
| 434 | module.train() |
| 435 | elif False == train_mode: |
| 436 | module.eval() |
no outgoing calls
no test coverage detected
searching dependent graphs…