Beispiel #1
0
def crf_train_step(x, spacings, target, architecture: CRFWrapper, criterion, optimizer, lr, with_crf: bool):
    architecture.train()

    x, target = sequence_to_var(x, target, device=architecture)

    output = architecture(x, spacings) if with_crf else architecture.network(x)
    loss = criterion(output, target)

    optimizer_step(optimizer, loss, lr=lr)
    return to_np(loss)
Beispiel #2
0
def train_autoencoder(model,
                      optimizer,
                      criterion,
                      batch_iter,
                      n_epochs,
                      train_dataset,
                      path,
                      batch_size=200,
                      length=500):
    '''
    :param model: PyTorch Neural Network model
    :param optimizer: Torch optimization strategy: SGD, Adam, AdaDelta, ...
    :param criterion: Loss function
    :param batch_iter: Batch iterator function
    :param n_epochs:  Number of epochs to train model
    :param train_dataset:  Dataset object to train model
    :param path: Experiment output path
    :param batch_size: Size of batches
    :param length: Length of crop to load train data
    :return: None
    '''
    # initial setup
    path = Path(path)
    logger = NamedTBLogger(path / 'logs', ['loss'])
    model.eval()

    for step in tqdm(range(n_epochs)):

        model.train()
        losses = []
        for inputs in batch_iter(train_dataset, batch_size, length):

            *inputs, target, = sequence_to_var(*tuple(inputs[:2]),
                                               cuda=is_on_cuda(model))

            logits = model(*inputs)

            if isinstance(criterion, nn.BCELoss):
                target = target.float()

            total = criterion(logits, target)

            optimizer.zero_grad()
            total.backward()
            optimizer.step()

            losses.append(sequence_to_np(total))

        logger.train(losses, step)
        print(f'Loss: {losses}')

    save_model_state(model, path / 'model.pth')
def train_step(*inputs,
               architecture,
               criterion,
               optimizer,
               n_targets=1,
               loss_key=None,
               alpha_l2sp=None,
               reference_architecture=None,
               **optimizer_params):
    architecture.train()
    if n_targets >= 0:
        n_inputs = len(inputs) - n_targets
    else:
        n_inputs = -n_targets

    assert 0 <= n_inputs <= len(inputs)
    inputs = sequence_to_var(*inputs, device=architecture)
    inputs, targets = inputs[:n_inputs], inputs[n_inputs:]

    if alpha_l2sp is not None:
        if reference_architecture is None:
            raise ValueError(
                '`reference_architecture` should be provided for L2-SP regularization.'
            )

        w_diff = torch.tensor(0., requires_grad=True, dtype=torch.float32)
        w_diff.to(get_device(architecture))
        for p1, p2 in zip(architecture.parameters(),
                          reference_architecture.parameters()):
            w_diff = w_diff + torch.sum((p1 - p2)**2)

        loss = criterion(architecture(*inputs), *targets) + alpha_l2sp * w_diff
    else:
        loss = criterion(architecture(*inputs), *targets)

    if loss_key is not None:
        optimizer_step(optimizer, loss[loss_key], **optimizer_params)
        return dmap(to_np, loss)

    optimizer_step(optimizer, loss, **optimizer_params)
    return to_np(loss)
def train_step(inputs, targets, model, optimizer):
    model.train()

    # move the tensors to the same device as `model`
    inputs, targets = sequence_to_var(inputs, targets, device=model)
    prediction = model(inputs)

    # the model has 2 `heads`
    assert prediction.shape[1] == 2, prediction.shape
    curves, limits = prediction[:, 0], prediction[:, 1]

    limits_mask = ~torch.isnan(targets)
    loss = functional.binary_cross_entropy_with_logits(
        limits, limits_mask.to(dtype=limits.dtype))

    if limits_mask.any():
        # penalize only the predictions where the target is defined
        loss = loss + functional.mse_loss(curves[limits_mask],
                                          targets[limits_mask])

    optimizer_step(optimizer, loss)
    return to_np(loss)  # convert back to numpy
Beispiel #5
0
def train(model,
          optimizer,
          criterion,
          batch_iter,
          n_epochs,
          train_dataset,
          val_data,
          val_labels,
          path,
          batch_size=200,
          length=500):
    '''
    :param model: PyTorch Neural Network model
    :param optimizer: Torch optimization strategy: SGD, Adam, AdaDelta, ...
    :param criterion: Loss function
    :param batch_iter: Batch iterator function
    :param n_epochs:  Number of epochs to train model
    :param train_dataset:  Dataset object to train model
    :param val_data: Loaded numpy data to evaluate model
    :param val_labels: Loaded numpy target to evaluate model
    :param path: Experiment output path
    :param batch_size: Size of batches
    :param length: Length of crop to load train data
    :return: None
    '''
    # initial setup
    path = Path(path)
    logger = NamedTBLogger(path / 'logs', ['loss'])
    model.eval()

    best_score = None

    for step in tqdm(range(n_epochs)):

        model.train()
        losses = []
        for inputs in batch_iter(train_dataset, batch_size, length):
            *inputs, target = sequence_to_var(*tuple(inputs),
                                              cuda=is_on_cuda(model))

            logits = model(*inputs)

            if isinstance(criterion, nn.BCELoss):
                target = target.float()

            total = criterion(logits, target)

            optimizer.zero_grad()
            total.backward()
            optimizer.step()

            losses.append(sequence_to_np(total))

        logger.train(losses, step)

        # validation
        model.eval()

        # metrics
        score = evaluate(model, val_data, val_labels)
        dump_json(score, path / 'val_accuracy.json')
        print(f'Val score {score}')
        logger.metrics({'accuracy': score}, step)

        # best model
        if best_score is None or best_score < score:
            best_score = score
            save_model_state(model, path / 'best_model.pth')

    save_model_state(model, path / 'model.pth')