Skip to content

Commit

Permalink
LR attack: allow setting validation set size
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-wisiol committed Jan 12, 2022
1 parent e04306a commit 433cb95
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions pypuf/attack/lr2021.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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]:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 433cb95

Please sign in to comment.