tensorflow / neural-structured-learning

Training neural models with structured signals.
https://www.tensorflow.org/neural_structured_learning
Apache License 2.0
980 stars 189 forks source link

Apply adversarial training after few epochs. #124

Closed jhss closed 1 year ago

jhss commented 1 year ago

I want to train the model without adversarial attack at the first two epochs. After that, I would like to train the model with adversarial learning.

In summary,

1 epoch: Training w/o adversarial 2 epoch: Training w/o adversarial 3 epoch: Training with adversarial 4 epoch: Training with adversarial ....

Is it possible to adjust the start epoch of the adversarial training? I couldn't find any related parameter in nsl.configs.make_adv_reg_config

csferng commented 1 year ago

Hi @jhss, thanks for the question.

You're right that make_adv_reg_config doesn't have such parameter for delaying the adversarial training. Instead, you may train a base model for a few epochs before wrapping it with nsl.keras.AdversarialRegularization. This achieves the desired behavior because an AdversarialRegularization-wrapped model and its base model share the model variables. The following code snippet illustrates this idea:

base_model.fit(training_data, epochs=num_epochs_without_adv_training)
adv_model = nsl.keras.AdversarialRegularization(base_model, ...)
adv_model.fit(training_data, epochs=num_epochs_with_adv_training))
csferng commented 1 year ago

Closing this issue due to inactivity in 30 days. Please feel free to reopen if you have more questions.