Пример #1
0
def train(model, data, target, optimizer, max_iterations, loss_func_args):
    start_time = time.time()
    number_of_terms = [coeff_vec.shape[0] for coeff_vec in model(data)[3]]
    board = Tensorboard(number_of_terms)

    # Training
    print(
        '| Iteration | Progress | Time remaining |     Cost |      MSE |      Reg |       LL |'
    )
    for iteration in torch.arange(0, max_iterations + 1):
        # Calculating prediction and library and scaling
        prediction, time_deriv_list, sparse_theta_list, coeff_vector_list, theta = model(
            data)
        coeff_vector_scaled_list = scaling(coeff_vector_list,
                                           sparse_theta_list, time_deriv_list)

        # Calculating loss
        loss_mse = mse_loss(prediction, target)
        loss_reg = reg_loss(time_deriv_list, sparse_theta_list,
                            coeff_vector_list)
        loss = torch.sum(loss_mse) + torch.sum(loss_reg)

        # Writing
        if iteration % 100 == 0:
            # Write progress to command line
            progress(iteration, start_time, max_iterations, loss.item(),
                     torch.sum(loss_mse).item(),
                     torch.sum(loss_reg).item(),
                     torch.sum(loss_reg).item())

            lstsq_solution = torch.inverse(
                theta.T @ theta) @ theta.T @ time_deriv_list[0]

            # Calculate error for theta
            theta_true = loss_func_args['library']
            dt_true = loss_func_args['time_deriv']
            mae_library = torch.mean(torch.abs(theta - theta_true), dim=0)
            mae_dt = torch.mean(torch.abs(dt_true - time_deriv_list[0]), dim=0)

            # Write to tensorboard
            board.write(iteration,
                        loss,
                        loss_mse,
                        loss_reg,
                        loss_reg,
                        coeff_vector_list,
                        coeff_vector_scaled_list,
                        lstsq_solution=lstsq_solution.squeeze(),
                        mae_library=mae_library,
                        mae_time_deriv=mae_dt)

        # Optimizer step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    board.close()
def train(model, data, target, optimizer, max_iterations, loss_func_args):
    '''Trains the deepmod model with MSE, regression and l1 cost function. Updates model in-place.'''
    start_time = time.time()
    number_of_terms = [coeff_vec.shape[0] for coeff_vec in model(data)[3]]
    board = Tensorboard(number_of_terms)

    # Training
    print(
        '| Iteration | Progress | Time remaining |     Cost |      MSE |      Reg |       LL |'
    )
    for iteration in torch.arange(0, max_iterations + 1):
        # Calculating prediction and library and scaling
        prediction, time_deriv_list, sparse_theta_list, coeff_vector_list, theta = model(
            data)
        coeff_vector_scaled_list = scaling(coeff_vector_list,
                                           sparse_theta_list, time_deriv_list)

        # Calculating loss
        loss_mse = mse_loss(prediction, target)
        loss_reg = reg_loss(time_deriv_list, sparse_theta_list,
                            coeff_vector_list)
        loss_l1 = l1_loss(coeff_vector_scaled_list, loss_func_args['l1'])

        loss = torch.sum(1 + 2 * torch.log(2 * pi * loss_mse) +
                         loss_reg / loss_mse)

        # Writing
        if iteration % 100 == 0:
            # Write progress to command line
            progress(iteration, start_time, max_iterations, loss.item(),
                     torch.sum(loss_mse).item(),
                     torch.sum(loss_reg).item(),
                     torch.sum(loss_reg).item())

            # Calculate error for theta
            theta_true = loss_func_args['library']
            dt_true = loss_func_args['time_deriv']
            mae_library = torch.mean(torch.abs(theta - theta_true), dim=0)
            mae_dt = torch.mean(torch.abs(dt_true - time_deriv_list[0]), dim=0)

            # Write to tensorboard
            board.write(iteration,
                        loss,
                        loss_mse,
                        loss_reg,
                        loss_l1,
                        coeff_vector_list,
                        coeff_vector_scaled_list,
                        mae_library=mae_library,
                        mae_time_deriv=mae_dt)

        # Optimizer step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    board.close()
Пример #3
0
def train(model,
          data,
          target,
          optimizer,
          max_iterations,
          loss_func_args,
          log_dir=None):
    start_time = time.time()
    number_of_terms = [coeff_vec.shape[0] for coeff_vec in model(data)[3]]
    board = Tensorboard(number_of_terms, log_dir)

    # Training
    print(
        '| Iteration | Progress | Time remaining |     Cost |      MSE |      Reg |       LL |'
    )
    for iteration in torch.arange(0, max_iterations + 1):
        # Calculating prediction and library and scaling
        prediction, time_deriv_list, sparse_theta_list, coeff_vector_list, theta = model(
            data)
        coeff_vector_scaled_list = scaling(coeff_vector_list,
                                           sparse_theta_list, time_deriv_list)

        # Calculating loss
        loss_mse = mse_loss(prediction, target)
        loss_reg = reg_loss(time_deriv_list, sparse_theta_list,
                            coeff_vector_list)
        loss_ll = torch.log(2 * pi * loss_mse)
        loss_ll_fit = torch.log(2 * pi * loss_mse) + loss_reg / loss_mse
        loss = torch.sum(loss_ll) + torch.sum(loss_ll_fit)

        # Writing
        if iteration % 100 == 0:
            # Write progress to command line
            progress(iteration, start_time, max_iterations, loss.item(),
                     torch.sum(loss_mse).item(),
                     torch.sum(loss_reg).item(),
                     torch.sum(loss_ll).item())

            # Calculate error for theta

            # Write to tensorboard
            board.write(iteration, loss, loss_mse, loss_reg, loss_ll,
                        coeff_vector_list, coeff_vector_scaled_list)

        # Optimizer step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    board.close()
Пример #4
0
def train_dynamic_logprob_scaled_double(model, data, target, optimizer, max_iterations, loss_func_args={'sparsity_update_period': 200, 'start_sparsity_update': 5000}):
    '''Trains the deepmod model with MSE, regression and l1 cost function. Updates model in-place.'''
    start_time = time.time()
    number_of_terms = [coeff_vec.shape[0] for coeff_vec in model(data)[3]]
    board = Tensorboard(number_of_terms)
    
    #sigma = loss_func_args['noise'] # noise parameter
    # Training
    print('| Iteration | Progress | Time remaining |     Cost |      MSE |      Reg |       LL |')
    for iteration in torch.arange(0, max_iterations + 1):
        # Calculating prediction and library and scaling
        prediction, time_deriv_list, sparse_theta_list, coeff_vector_list, theta = model(data)
        coeff_vector_scaled_list = scaling(coeff_vector_list, sparse_theta_list, time_deriv_list) 
        
        # Calculating loss
        loss_mse = mse_loss(prediction, target)
        loss_reg = reg_loss(time_deriv_list, sparse_theta_list, coeff_vector_list)
        loss_ll_fit = torch.log(2 * pi * loss_reg)  #optimal sigma
        loss_ll = torch.log(2 * pi * loss_mse) #optimal sigma
            
        loss = torch.sum(loss_ll_fit) + torch.sum(loss_ll)
        
        # Writing
        if iteration % 100 == 0:
            progress(iteration, start_time, max_iterations, loss.item(), torch.sum(loss_mse).item(), torch.sum(loss_reg).item(), torch.sum(loss_ll).item())
            # Before writing to tensorboard, we need to fill the missing values with 0
            coeff_vectors_padded = [torch.zeros(mask.size()).masked_scatter_(mask, coeff_vector.squeeze()) for mask, coeff_vector in zip(model.constraints.sparsity_mask, coeff_vector_list)]
            scaled_coeff_vectors_padded = [torch.zeros(mask.size()).masked_scatter_(mask, coeff_vector.squeeze()) for mask, coeff_vector in zip(model.constraints.sparsity_mask, coeff_vector_scaled_list)]
            
            board.write(iteration, loss, loss_mse, loss_reg, loss_ll, coeff_vectors_padded, scaled_coeff_vectors_padded)

        # Optimizer step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        # Updating sparsity pattern
        if (iteration >= loss_func_args['start_sparsity_update']) and (iteration % loss_func_args['sparsity_update_period'] == 0):
            with torch.no_grad():
                model.constraints.sparsity_mask = model.calculate_sparsity_mask(theta, time_deriv_list)
                
    board.close()