From 433cb958a4f32c64f78be79976702d0c48da9878 Mon Sep 17 00:00:00 2001 From: Nils Wisiol Date: Wed, 12 Jan 2022 15:26:03 +0100 Subject: [PATCH] LR attack: allow setting validation set size --- pypuf/attack/lr2021.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pypuf/attack/lr2021.py b/pypuf/attack/lr2021.py index c6261a87..7c57b8b2 100644 --- a/pypuf/attack/lr2021.py +++ b/pypuf/attack/lr2021.py @@ -33,7 +33,7 @@ def on_epoch_end(self, epoch: int, logs: dict = None) -> None: self.model.stop_training = True def __init__(self, crps: ChallengeResponseSet, seed: int, k: int, bs: int, lr: float, epochs: int, - stop_validation_accuracy: float = .95) -> None: + stop_validation_accuracy: float = .95, validation_set_size: float = .01) -> None: """ Initialize an improved Logistic Regression attack using the given parameters. @@ -56,6 +56,8 @@ def __init__(self, crps: ChallengeResponseSet, seed: int, k: int, bs: int, lr: f :param stop_validation_accuracy: Training is stopped when this validation accuracy is reached. Set to 1 to deactivate. :type stop_validation_accuracy: ``float`` + :param validation_set_size: Proportion of CRPs to be used for validation, if <= 1, or else absolute number of + CRPs used to validation. """ super().__init__(crps) self.crps = crps @@ -67,6 +69,7 @@ def __init__(self, crps: ChallengeResponseSet, seed: int, k: int, bs: int, lr: f self.stop_validation_accuracy = stop_validation_accuracy self._history = None self._keras_model = None + self.validation_split = validation_set_size if validation_set_size <= 1 else validation_set_size / len(crps) @property def history(self) -> Optional[dict]: @@ -147,7 +150,7 @@ def fit(self, verbose: bool = True) -> Simulation: features, labels, batch_size=self.bs, epochs=self.epochs, - validation_split=.01, + validation_split=self.validation_split, callbacks=[self.AccuracyStop(self.stop_validation_accuracy)], verbose=verbose, ).history @@ -182,11 +185,6 @@ class BeliLR(LRAttack2021): def beli_output(output_delays: List[tf.Tensor]) -> List[tf.Tensor]: raise NotImplementedError - def __init__(self, crps: ChallengeResponseSet, seed: int, k: int, bs: int, lr: float, epochs: int, - stop_validation_accuracy: float = .95, validation_set_size: int = 1000) -> None: - super().__init__(crps, seed, k, bs, lr, epochs, stop_validation_accuracy) - self.validation_set_size = validation_set_size - def beli_model(self, input_tensor: tf.Tensor) -> List[tf.Tensor]: internal_delays = tf.keras.layers.Dense( units=1, @@ -231,7 +229,7 @@ def fit(self, verbose: bool = True) -> Simulation: self.crps.responses, batch_size=self.bs, epochs=self.epochs, - validation_split=self.validation_set_size / N, # validation set of 1000 CRPs + validation_split=self.validation_split, callbacks=[self.AccuracyStop(self.stop_validation_accuracy)], verbose=verbose, ).history