[BUGFIX] examples/glvq_spiral.py works again

This commit is contained in:
Jensun Ravichandran 2021-06-14 20:19:08 +02:00
parent 68034d56f6
commit 3afced8662
2 changed files with 10 additions and 10 deletions

View File

@ -25,7 +25,6 @@ if __name__ == "__main__":
distribution=(num_classes, prototypes_per_class),
transfer_function="swish_beta",
transfer_beta=10.0,
# lr=0.1,
proto_lr=0.1,
bb_lr=0.1,
input_dim=2,
@ -36,7 +35,7 @@ if __name__ == "__main__":
model = pt.models.GMLVQ(
hparams,
optimizer=torch.optim.Adam,
prototype_initializer=pt.components.SSI(train_ds, noise=1e-2),
prototypes_initializer=pt.initializers.SSCI(train_ds, noise=1e-2),
)
# Callbacks
@ -46,12 +45,12 @@ if __name__ == "__main__":
block=False,
)
pruning = pt.models.PruneLoserPrototypes(
threshold=0.02,
threshold=0.01,
idle_epochs=10,
prune_quota_per_epoch=5,
frequency=2,
frequency=5,
replace=True,
initializer=pt.components.SSI(train_ds, noise=1e-2),
prototypes_initializer=pt.initializers.SSCI(train_ds, noise=1e-1),
verbose=True,
)
es = pl.callbacks.EarlyStopping(
@ -67,7 +66,7 @@ if __name__ == "__main__":
args,
callbacks=[
vis,
# es,
# es, # FIXME
pruning,
],
terminate_on_nan=True,

View File

@ -16,7 +16,7 @@ class PruneLoserPrototypes(pl.Callback):
prune_quota_per_epoch=-1,
frequency=1,
replace=False,
initializer=None,
prototypes_initializer=None,
verbose=False):
self.threshold = threshold # minimum win ratio
self.idle_epochs = idle_epochs # epochs to wait before pruning
@ -24,7 +24,7 @@ class PruneLoserPrototypes(pl.Callback):
self.frequency = frequency
self.replace = replace
self.verbose = verbose
self.initializer = initializer
self.prototypes_initializer = prototypes_initializer
def on_epoch_end(self, trainer, pl_module):
if (trainer.current_epoch + 1) < self.idle_epochs:
@ -55,8 +55,9 @@ class PruneLoserPrototypes(pl.Callback):
if self.verbose:
print(f"Re-adding pruned prototypes...")
print(f"{distribution=}")
pl_module.add_prototypes(distribution=distribution,
initializer=self.initializer)
pl_module.add_prototypes(
distribution=distribution,
components_initializer=self.prototypes_initializer)
new_num_protos = pl_module.num_prototypes
if self.verbose:
print(f"`num_prototypes` changed from {cur_num_protos} "