def test_grads_scalar_handler_wrong_setup(): with pytest.raises(TypeError, match="Argument model should be of type torch.nn.Module"): GradsScalarHandler(None) model = MagicMock(spec=torch.nn.Module) with pytest.raises(TypeError, match="Argument reduction should be callable"): GradsScalarHandler(model, reduction=123) wrapper = GradsScalarHandler(model) mock_logger = MagicMock() mock_engine = MagicMock() with pytest.raises(TypeError, match="Handler GradsScalarHandler works only with NeptuneLogger"): wrapper(mock_engine, mock_logger, Events.ITERATION_STARTED)
def test_grads_scalar_handler_frozen_layers(dummy_model_factory, norm_mock): model = dummy_model_factory(with_grads=True, with_frozen_layer=True) wrapper = GradsScalarHandler(model, reduction=norm_mock) mock_logger = MagicMock(spec=NeptuneLogger) mock_logger.log_metric = MagicMock() mock_engine = MagicMock() mock_engine.state = State() mock_engine.state.epoch = 5 norm_mock.reset_mock() wrapper(mock_engine, mock_logger, Events.EPOCH_STARTED) mock_logger.log_metric.assert_has_calls([ call("grads_norm/fc2/weight", y=ANY, x=5), call("grads_norm/fc2/bias", y=ANY, x=5) ], any_order=True) with pytest.raises(AssertionError): mock_logger.log_metric.assert_has_calls([ call("grads_norm/fc1/weight", y=ANY, x=5), call("grads_norm/fc1/bias", y=ANY, x=5) ], any_order=True) assert mock_logger.log_metric.call_count == 2 assert norm_mock.call_count == 2
def _test(tag=None): wrapper = GradsScalarHandler(model, reduction=norm_mock, tag=tag) mock_logger = MagicMock(spec=NeptuneLogger) mock_logger.log_metric = MagicMock() mock_engine = MagicMock() mock_engine.state = State() mock_engine.state.epoch = 5 norm_mock.reset_mock() wrapper(mock_engine, mock_logger, Events.EPOCH_STARTED) tag_prefix = f"{tag}/" if tag else "" mock_logger.log_metric.assert_has_calls( [ call(tag_prefix + "grads_norm/fc1/weight", y=ANY, x=5), call(tag_prefix + "grads_norm/fc1/bias", y=ANY, x=5), call(tag_prefix + "grads_norm/fc2/weight", y=ANY, x=5), call(tag_prefix + "grads_norm/fc2/bias", y=ANY, x=5), ], any_order=True, ) assert mock_logger.log_metric.call_count == 4 assert norm_mock.call_count == 4
def run(train_batch_size, val_batch_size, epochs, lr, momentum): train_loader, val_loader = get_data_loaders(train_batch_size, val_batch_size) model = Net() device = "cpu" if torch.cuda.is_available(): device = "cuda" model.to(device) # Move model before creating optimizer optimizer = SGD(model.parameters(), lr=lr, momentum=momentum) criterion = nn.CrossEntropyLoss() trainer = create_supervised_trainer(model, optimizer, criterion, device=device) trainer.logger = setup_logger("Trainer") metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)} train_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device) train_evaluator.logger = setup_logger("Train Evaluator") validation_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device) validation_evaluator.logger = setup_logger("Val Evaluator") @trainer.on(Events.EPOCH_COMPLETED) def compute_metrics(engine): train_evaluator.run(train_loader) validation_evaluator.run(val_loader) npt_logger = NeptuneLogger( api_token="ANONYMOUS", project_name="shared/pytorch-ignite-integration", name="ignite-mnist-example", params={ "train_batch_size": train_batch_size, "val_batch_size": val_batch_size, "epochs": epochs, "lr": lr, "momentum": momentum, }, ) npt_logger.attach_output_handler( trainer, event_name=Events.ITERATION_COMPLETED(every=100), tag="training", output_transform=lambda loss: {"batchloss": loss}, ) for tag, evaluator in [("training", train_evaluator), ("validation", validation_evaluator)]: npt_logger.attach_output_handler( evaluator, event_name=Events.EPOCH_COMPLETED, tag=tag, metric_names=["loss", "accuracy"], global_step_transform=global_step_from_engine(trainer), ) npt_logger.attach_opt_params_handler( trainer, event_name=Events.ITERATION_COMPLETED(every=100), optimizer=optimizer) npt_logger.attach(trainer, log_handler=WeightsScalarHandler(model), event_name=Events.ITERATION_COMPLETED(every=100)) npt_logger.attach(trainer, log_handler=GradsScalarHandler(model), event_name=Events.ITERATION_COMPLETED(every=100)) def score_function(engine): return engine.state.metrics["accuracy"] handler = Checkpoint( {"model": model}, NeptuneSaver(npt_logger), n_saved=2, filename_prefix="best", score_function=score_function, score_name="validation_accuracy", global_step_transform=global_step_from_engine(trainer), ) validation_evaluator.add_event_handler(Events.COMPLETED, handler) # kick everything off trainer.run(train_loader, max_epochs=epochs) npt_logger.close()