def add_model_args(parser): group = parser.add_argument_group('Model configuration') # Model definitions can be found under fairseq/models/ # # The model architecture can be specified in several ways. # In increasing order of priority: # 1) model defaults (lowest priority) # 2) --arch argument # 3) --encoder/decoder-* arguments (highest priority) group.add_argument( '--arch', '-a', default='fconv', metavar='ARCH', required=True, choices=ARCH_MODEL_REGISTRY.keys(), help='model architecture: {} (default: fconv)'.format( ', '.join(ARCH_MODEL_REGISTRY.keys())), ) # Criterion definitions can be found under fairseq/criterions/ group.add_argument( '--criterion', default='cross_entropy', metavar='CRIT', choices=CRITERION_REGISTRY.keys(), help='training criterion: {} (default: cross_entropy)'.format( ', '.join(CRITERION_REGISTRY.keys())), ) return group
def add_ac_args(parser): from fairseq.tasks import TASK_REGISTRY from fairseq.criterions import CRITERION_REGISTRY parser.add_argument('--actor-restore-file', default='checkpoints/actor/model.pt', metavar='DIR', help='path to restore actor') parser.add_argument('--actor-task', default='translation', metavar='TASK', choices=TASK_REGISTRY.keys(), help='task for actor') parser.add_argument('--actor-criterion', default='ac-loss-actor', metavar='CRITERION', choices=CRITERION_REGISTRY.keys(), help='criterion for actor') parser.add_argument('--actor-save-update', '--asu', default=0, type=int, metavar='N', help='force stop training actor at specified update') parser.add_argument('--critic-restore-file', default='checkpoints/critic/model.pt', metavar='DIR', help='path to restore critic') parser.add_argument('--critic-task', default='translation', metavar='TASK', choices=TASK_REGISTRY.keys(), help='task for critic') parser.add_argument('--critic-criterion', default='ac-loss-critic', metavar='CRITERION', choices=CRITERION_REGISTRY.keys(), help='criterion for critic') parser.add_argument('--critic-save-update', '--csu', default=0, type=int, metavar='N', help='force stop training critic at specified update')
def add_model_args(parser): group = parser.add_argument_group('Model configuration') # Model definitions can be found under fairseq/models/ # # The model architecture can be specified in several ways. # In increasing order of priority: # 1) model defaults (lowest priority) # 2) --arch argument # 3) --encoder/decoder-* arguments (highest priority) group.add_argument( '--arch', '-a', default='fconv', metavar='ARCH', required=True, choices=ARCH_MODEL_REGISTRY.keys(), help='Model Architecture', ) # Criterion definitions can be found under fairseq/criterions/ group.add_argument( '--criterion', default='cross_entropy', metavar='CRIT', choices=CRITERION_REGISTRY.keys(), help='Training Criterion', ) return group
parser.add_argument('--max-target-positions', default=1024, type=int, metavar='N', help='max number of tokens in the target sequence') parser.add_argument('--pad-sequence', default=1, type=int, metavar='N', help='Pad sequences to a multiple of N') parser.add_argument( '--criterion', default='label_smoothed_cross_entropy', metavar='CRIT', choices=CRITERION_REGISTRY.keys(), help='training criterion: {} (default: label_smoothed_cross_entropy)'. format(', '.join(CRITERION_REGISTRY.keys())), ) parser.add_argument( '--label-smoothing', default=0.1, type=float, metavar='D', help='epsilon for label smoothing, 0 means no label smoothing') parser.add_argument( '--warmup-updates', default=4000, type=int, metavar='N',
def add_adversarial_args(parser, attack_only=False, train=False): """Adds arguments specific to adversarial example generation""" group = parser.add_argument_group("Adversarial examples arguments") if attack_only: group.add_argument( "--quiet", action="store_true", help="Don't print the adversarial sentences to stdout", ) group.add_argument( "--path", metavar="DIR/FILE", default=None, help="path(s) to model file(s), colon separated " "(only one model is supported right now)", ) if train: group.add_argument( "--adv-weight", default=0.5, metavar="EPS", type=float, help="Weight (between 0 and 1) of the adversarial samples during " "training", ) group.add_argument( "--warmup-epochs", default=1, metavar="N", type=int, help="Minimum number of epochs of training before adversarial " "augmentation kicks in", ) group.add_argument( "--accumulate-adv-gradient", action="store_true", default=False, help="Run one forward/backward pass for the adversarial input " "and one for the normal input before updating. This is slower " "but more memory efficient than wrapping the two in a single " "batch, effectively doubling the batch size.", ) # Trainer Arguments group.add_argument( "--modify-gradient", default="", metavar="ACTION", choices=["", "sign", "normalize", "normalize"], help="Modify the gradient by taking the sign or normalizing along the " "word vector dimension.", ) # Adversarial criterion group.add_argument( "--adv-criterion", default="cross_entropy", metavar="CRIT", choices=CRITERION_REGISTRY.keys(), help="Adversarial criterion: {} (default: cross_entropy). " "This is the objective that the adversary will try to minimize. It " "should be something that will make the model worse at its training " "criterions".format(", ".join(CRITERION_REGISTRY.keys())), ) group.add_argument( "--reverse-criterion", action="store_true", default=False, help="Force the adversary to *maximize* the criterion instead of " "minimizing it. This is convenient way of reusing training criterions " "for adversarial attacks.", ) # Number of iterations for the attack group.add_argument( "--n-attack-iterations", default=1, metavar="N", type=int, help="Number of iterations during the attack. One iteration consists " "in: 1. Forward pass on the current input " "2. Backward pass to get the gradient" "3. Generate a new input with the adversary", ) # Adversaries definitions can be found under research/adversarial/adversaries group.add_argument( "--adversary", default="random_swap", metavar="ADV", choices=ADVERSARY_REGISTRY.keys(), help="adversary type: {} (default: random_swap)".format(", ".join( ADVERSARY_REGISTRY.keys())), ) # Add arguments specific to all adversaries BaseAdversary.add_args(group) # Add constraints specific arguments AdversarialConstraints.add_args(parser) return group