(loss,
warmup_steps,
num_train_steps,
learning_rate,
train_program,
startup_prog,
weight_decay,
scheduler='linear_warmup_decay',
use_fp16=False,
use_dynamic_loss_scaling=False,
init_loss_scaling=1.0,
incr_every_n_steps=1000,
decr_every_n_nan_or_inf=2,
incr_ratio=2.0,
decr_ratio=0.8)
| 51 | |
| 52 | |
| 53 | def optimization(loss, |
| 54 | warmup_steps, |
| 55 | num_train_steps, |
| 56 | learning_rate, |
| 57 | train_program, |
| 58 | startup_prog, |
| 59 | weight_decay, |
| 60 | scheduler='linear_warmup_decay', |
| 61 | use_fp16=False, |
| 62 | use_dynamic_loss_scaling=False, |
| 63 | init_loss_scaling=1.0, |
| 64 | incr_every_n_steps=1000, |
| 65 | decr_every_n_nan_or_inf=2, |
| 66 | incr_ratio=2.0, |
| 67 | decr_ratio=0.8): |
| 68 | |
| 69 | scheduled_lr, loss_scaling = None, None |
| 70 | if scheduler == 'noam_decay': |
| 71 | if warmup_steps > 0: |
| 72 | scheduled_lr = fluid.layers.learning_rate_scheduler\ |
| 73 | .noam_decay(1/(warmup_steps *(learning_rate ** 2)), |
| 74 | warmup_steps) |
| 75 | else: |
| 76 | print( |
| 77 | "WARNING: noam decay of learning rate should have postive warmup " |
| 78 | "steps but given {}, using constant learning rate instead!" |
| 79 | .format(warmup_steps)) |
| 80 | scheduled_lr = fluid.layers.create_global_var( |
| 81 | name=fluid.unique_name.generate("learning_rate"), |
| 82 | shape=[1], |
| 83 | value=learning_rate, |
| 84 | dtype='float32', |
| 85 | persistable=True) |
| 86 | elif scheduler == 'linear_warmup_decay': |
| 87 | if warmup_steps > 0: |
| 88 | scheduled_lr = linear_warmup_decay(learning_rate, warmup_steps, |
| 89 | num_train_steps) |
| 90 | else: |
| 91 | print( |
| 92 | "WARNING: linear warmup decay of learning rate should have " |
| 93 | "postive warmup steps but given {}, use constant learning rate " |
| 94 | "instead!".format(warmup_steps)) |
| 95 | scheduled_lr = fluid.layers.create_global_var( |
| 96 | name=fluid.unique_name.generate("learning_rate"), |
| 97 | shape=[1], |
| 98 | value=learning_rate, |
| 99 | dtype='float32', |
| 100 | persistable=True) |
| 101 | else: |
| 102 | raise ValueError("Unkown learning rate scheduler, should be " |
| 103 | "'noam_decay' or 'linear_warmup_decay'") |
| 104 | |
| 105 | optimizer = fluid.optimizer.Adam(learning_rate=scheduled_lr) |
| 106 | fluid.clip.set_gradient_clip( |
| 107 | clip=fluid.clip.GradientClipByGlobalNorm(clip_norm=1.0)) |
| 108 | |
| 109 | def exclude_from_weight_decay(param): |
| 110 | name = param.name.rstrip(".master") |
no test coverage detected