Example #1
0
def test_val_default_logging(val_state):
    val_state.logger = MockLogger()
    val_state.metrics = {"val_loss": 0.01}
    val_state.epoch = -1
    default_logging.handler(state=val_state)
    assert val_state.logger.message == 'val - epoch: -1, val_loss: 0.01'

    val_state.metrics = {"val_loss": 0.01, "val_accuracy": 0.42}
    val_state.epoch = 12
    default_logging.handler(state=val_state)
    assert val_state.logger.message == 'val - epoch: 12, val_loss: 0.01, val_accuracy: 0.42'
Example #2
0
def test_train_default_logging(train_state):
    train_state.logger = MockLogger()
    train_state.metrics = {"train_loss": 0.1}
    train_state.logger = MockLogger()
    default_logging.handler(state=train_state)
    assert train_state.logger.message == 'train - epoch: 0, lr: 0.01, train_loss: 0.1'

    train_state.metrics = {"train_loss": 0.01, "train_accuracy": 0.42}
    train_state.epoch = 42
    default_logging.handler(state=train_state)
    assert train_state.logger.message == 'train - epoch: 42, lr: 0.01, train_loss: 0.01, train_accuracy: 0.42'
Example #3
0
def test_logging_to_file_create_dir(tmpdir, train_state):
    logger = train_state.model.logger
    train_state.logger = logger
    path = str(tmpdir.join("path/to/another_logs/log.txt"))
    logging_to_file = LoggingToFile(path,
                                    append=False,
                                    create_dir=True,
                                    formatter='[%(levelname)s]: %(message)s')
    logging_to_file.start(train_state)
    default_logging.handler(state=train_state)
    expected_messages = ['[INFO]: train - epoch: 0, lr: 0.01\n']
    assert read_file(path) == expected_messages
    assert any(logging_to_file.file_handler is h for h in logger.handlers)
    logging_to_file.catch_exception(train_state)
    assert not any(logging_to_file.file_handler is h for h in logger.handlers)
Example #4
0
def test_logging_to_file(tmpdir, train_state):
    logger = train_state.model.logger
    train_state.logger = logger
    path = str(tmpdir.mkdir('logs').join("log.txt"))

    with open(path, 'w') as file:
        file.write('qwerty')

    logging_to_file = LoggingToFile(path,
                                    append=False,
                                    create_dir=False,
                                    formatter='[%(levelname)s]: %(message)s')

    logging_to_file.start(train_state)
    assert [logging_to_file.file_handler is h for h in logger.handlers]
    default_logging.handler(state=train_state)
    expected_messages = ['[INFO]: train - epoch: 0, lr: 0.01\n']
    assert read_file(path) == expected_messages

    train_state.epoch = 12
    train_state.metrics = {'train_loss': 0.123}
    default_logging.handler(state=train_state)
    expected_messages += [
        '[INFO]: train - epoch: 12, lr: 0.01, train_loss: 0.123\n'
    ]
    assert read_file(path) == expected_messages
    logging_to_file.complete(train_state)
    assert not any(logging_to_file.file_handler is h for h in logger.handlers)

    logging_to_file = LoggingToFile(path,
                                    append=True,
                                    create_dir=True,
                                    formatter='[%(levelname)s]: %(message)s')
    logging_to_file.start(train_state)
    train_state.epoch = 42
    train_state.metrics = {'train_loss': 0.246}
    default_logging.handler(state=train_state)
    expected_messages += [
        '[INFO]: train - epoch: 42, lr: 0.01, train_loss: 0.246\n'
    ]
    assert read_file(path) == expected_messages
    logging_to_file.complete(train_state)
    assert not any(logging_to_file.file_handler is h for h in logger.handlers)