Skip to content

Commit

Permalink
Dropout regularization
Browse files Browse the repository at this point in the history
  • Loading branch information
kahst committed Oct 20, 2023
1 parent 725bafa commit 672b243
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ Here is a list of all command line arguments:
--val_split, Validation split ratio. Defaults to 0.2.
--learning_rate, Learning rate. Defaults to 0.01.
--hidden_units, Number of hidden units. Defaults to 0. If set to >0, a two-layer classifier is used.
--dropout, Dropout rate. Defaults to 0.
--mixup, Whether to use mixup for training.
--upsampling_ratio, Balance train data and upsample minority classes. Values between 0 and 1. Defaults to 0.
--upsampling_mode, Upsampling mode. Can be 'repeat', 'mean' or 'smote'. Defaults to 'repeat'.
Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
# If >0, a two-layer classifier will be trained
TRAIN_HIDDEN_UNITS: int = 0

# Dropout rate for training
TRAIN_DROPOUT: float = 0.0

# Whether to use mixup for training
TRAIN_WITH_MIXUP: bool = False

Expand Down
7 changes: 7 additions & 0 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ def buildLinearClassifier(num_labels, input_size, hidden_units=0):

# Hidden layer
if hidden_units > 0:
# Dropout layer?
if cfg.TRAIN_DROPOUT > 0:
model.add(keras.layers.Dropout(cfg.TRAIN_DROPOUT))
model.add(keras.layers.Dense(hidden_units, activation="relu"))

# Dropout layer?
if cfg.TRAIN_DROPOUT > 0:
model.add(keras.layers.Dropout(cfg.TRAIN_DROPOUT))

# Classification layer
model.add(keras.layers.Dense(num_labels))

Expand Down
2 changes: 2 additions & 0 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def trainModel(on_epoch_end=None):
default=0,
help="Number of hidden units. Defaults to 0. If set to >0, a two-layer classifier is used.",
)
parser.add_argument("--dropout", type=float, default=0.0, help="Dropout rate. Defaults to 0.")
parser.add_argument("--mixup", action=argparse.BooleanOptionalAction, help="Whether to use mixup for training.")
parser.add_argument("--upsampling_ratio", type=float, default=0.0, help="Balance train data and upsample minority classes. Values between 0 and 1. Defaults to 0.")
parser.add_argument("--upsampling_mode", default="repeat", help="Upsampling mode. Can be 'repeat', 'mean' or 'smote'. Defaults to 'repeat'.")
Expand All @@ -195,6 +196,7 @@ def trainModel(on_epoch_end=None):
cfg.TRAIN_VAL_SPLIT = args.val_split
cfg.TRAIN_LEARNING_RATE = args.learning_rate
cfg.TRAIN_HIDDEN_UNITS = args.hidden_units
cfg.TRAIN_DROPOUT = min(max(0, args.dropout), 0.9)
cfg.TRAIN_WITH_MIXUP = args.mixup
cfg.UPSAMPLING_RATIO = min(max(0, args.upsampling_ratio), 1)
cfg.UPSAMPLING_MODE = args.upsampling_mode
Expand Down

0 comments on commit 672b243

Please sign in to comment.