예제 #1
0
def load_predict_save(hyperp, run_options, file_paths): 
    #=== Load Testing Data ===# 
    obs_indices, parameter_test, state_obs_test, data_input_shape, parameter_dimension\
    = load_thermal_fin_test_data(file_paths, run_options.num_data_test, run_options.parameter_dimensions) 

    #=== Shuffling Data and Forming Batches ===#
    parameter_and_state_obs_test = tf.data.Dataset.from_tensor_slices((parameter_test, state_obs_test)).shuffle(8192, seed=run_options.random_seed).batch(hyperp.batch_size)

    #=== Data and Latent Dimensions of Autoencoder ===#    
    if run_options.use_standard_autoencoder == 1:
        data_dimension = parameter_dimension
        if hyperp.data_type == 'full':
            latent_dimension = run_options.full_domain_dimensions
        if hyperp.data_type == 'bnd':
            latent_dimension = len(obs_indices)
    
    if run_options.use_reverse_autoencoder == 1:
        if hyperp.data_type == 'full':
            data_dimension = run_options.full_domain_dimensions
        if hyperp.data_type == 'bnd':
            data_dimension = len(obs_indices)
        latent_dimension = parameter_dimension
        
    #=== Load Trained Neural Network ===#
    NN = AutoencoderFwdInv(hyperp, data_dimension, latent_dimension)
    NN.load_weights(file_paths.NN_savefile_name)  
    
    #=== Predict and Save ===#
    predict_and_save(hyperp, run_options, file_paths, NN, parameter_and_state_obs_test, obs_indices)
예제 #2
0
def trainer(hyperp, run_options, file_paths):
    #=== GPU Settings ===# Must put this first! Because TF2 will automatically work on a GPU and it may clash with used ones if the visible device list is not yet specified
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    if run_options.use_distributed_training == 0:
        os.environ["CUDA_VISIBLE_DEVICES"] = run_options.which_gpu
        GLOBAL_BATCH_SIZE = hyperp.batch_size
    if run_options.use_distributed_training == 1:
        os.environ["CUDA_VISIBLE_DEVICES"] = run_options.dist_which_gpus
        gpus = tf.config.experimental.list_physical_devices('GPU')
        GLOBAL_BATCH_SIZE = hyperp.batch_size * len(
            gpus
        )  # To avoid the core dump issue, have to do this instead of hyperp.batch_size * dist_strategy.num_replicas_in_sync

    #=== Load Data ===#
    obs_indices, parameter_train, state_obs_train,\
    parameter_test, state_obs_test,\
    data_input_shape, parameter_dimension\
    = load_thermal_fin_data(file_paths, run_options.num_data_train, run_options.num_data_test, run_options.parameter_dimensions)

    #=== Construct Validation Set and Batches ===#
    parameter_and_state_obs_train, parameter_and_state_obs_val, parameter_and_state_obs_test,\
    run_options.num_data_train, num_data_val, run_options.num_data_test,\
    num_batches_train, num_batches_val, num_batches_test\
    = form_train_val_test_batches(parameter_train, state_obs_train, parameter_test, state_obs_test, GLOBAL_BATCH_SIZE, run_options.random_seed)

    #=== Data and Latent Dimensions of Autoencoder ===#
    data_dimension = parameter_dimension
    if hyperp.data_type == 'full':
        latent_dimension = run_options.full_domain_dimensions
    if hyperp.data_type == 'bnd':
        latent_dimension = len(obs_indices)

    #=== Non-distributed Training ===#
    if run_options.use_distributed_training == 0:
        #=== Neural Network ===#
        NN = AutoencoderFwdInv(hyperp, data_dimension, latent_dimension)

        #=== Training ===#
        storage_array_loss_train, storage_array_loss_train_autoencoder, storage_array_loss_train_forward_problem, storage_array_loss_train_model_augmented,\
        storage_array_loss_val, storage_array_loss_val_autoencoder, storage_array_loss_val_forward_problem, storage_array_loss_val_model_augmented,\
        storage_array_loss_test, storage_array_loss_test_autoencoder, storage_array_loss_test_forward_problem, storage_array_loss_test_model_augmented,\
        storage_array_relative_error_parameter_autoencoder, storage_array_relative_error_state_obs, storage_array_relative_error_parameter_inverse_problem\
        = optimize(hyperp, run_options, file_paths, NN, obs_indices, loss_autoencoder, loss_encoder, loss_model_augmented, relative_error,\
                   parameter_and_state_obs_train, parameter_and_state_obs_val, parameter_and_state_obs_test,\
                   parameter_dimension, num_batches_train)

    #=== Distributed Training ===#
    if run_options.use_distributed_training == 1:
        dist_strategy = tf.distribute.MirroredStrategy()
        with dist_strategy.scope():
            #=== Neural Network ===#
            NN = AutoencoderFwdInv(hyperp, data_dimension, latent_dimension)

        #=== Training ===#
        storage_array_loss_train, storage_array_loss_train_autoencoder, storage_array_loss_train_forward_problem, storage_array_loss_train_model_augmented,\
        storage_array_loss_val, storage_array_loss_val_autoencoder, storage_array_loss_val_forward_problem, storage_array_loss_val_model_augmented,\
        storage_array_loss_test, storage_array_loss_test_autoencoder, storage_array_loss_test_forward_problem, storage_array_loss_test_model_augmented,\
        storage_array_relative_error_parameter_autoencoder, storage_array_relative_error_state_obs, storage_array_relative_error_parameter_inverse_problem\
        = optimize_distributed(dist_strategy, GLOBAL_BATCH_SIZE,
                               hyperp, run_options, file_paths, NN, obs_indices, loss_autoencoder, loss_model_augmented, relative_error,\
                               parameter_and_state_obs_train, parameter_and_state_obs_val, parameter_and_state_obs_test,\
                               parameter_dimension, num_batches_train)

    #=== Saving Metrics ===#
    metrics_dict = {}
    metrics_dict['loss_train'] = storage_array_loss_train
    metrics_dict[
        'loss_train_autoencoder'] = storage_array_loss_train_autoencoder
    metrics_dict[
        'loss_train_forward_problem'] = storage_array_loss_train_forward_problem
    metrics_dict[
        'loss_train_model_augmented'] = storage_array_loss_train_model_augmented
    metrics_dict['loss_val'] = storage_array_loss_val
    metrics_dict['loss_val_autoencoder'] = storage_array_loss_val_autoencoder
    metrics_dict[
        'loss_val_forward_problem'] = storage_array_loss_val_forward_problem
    metrics_dict[
        'loss_val_model_augmented'] = storage_array_loss_val_model_augmented
    metrics_dict[
        'relative_error_parameter_autoencoder'] = storage_array_relative_error_parameter_autoencoder
    metrics_dict[
        'relative_error_state_obs'] = storage_array_relative_error_state_obs
    metrics_dict[
        'relative_error_parameter_inverse_problem'] = storage_array_relative_error_parameter_inverse_problem
    df_metrics = pd.DataFrame(metrics_dict)
    df_metrics.to_csv(file_paths.NN_savefile_name + "_metrics" + '.csv',
                      index=False)
    def objective_functional(**hyperp_of_interest_objective_args_tuple):
        #=== Assign Hyperparameters of Interest ===#
        for key, val in hyperp_of_interest_objective_args_tuple.items():
            setattr(hyperp, key, val)
        hyperp.truncation_layer = int(np.ceil(hyperp.num_hidden_layers / 2))

        #=== Update File Paths with New Hyperparameters ===#
        file_paths = FilePaths(hyperp, run_options)

        #=== Construct Validation Set and Batches ===#
        if run_options.use_distributed_training == 0:
            GLOBAL_BATCH_SIZE = hyperp.batch_size
        if run_options.use_distributed_training == 1:
            GLOBAL_BATCH_SIZE = hyperp.batch_size * len(
                gpus
            )  # To avoid the core dump issue, have to do this instead of hyperp.batch_size * dist_strategy.num_replicas_in_sync
        parameter_and_state_obs_train, parameter_and_state_obs_val, parameter_and_state_obs_test,\
        run_options.num_data_train, num_data_val, run_options.num_data_test,\
        num_batches_train, num_batches_val, num_batches_test\
        = form_train_val_test_batches(parameter_train, state_obs_train, parameter_test, state_obs_test, GLOBAL_BATCH_SIZE, run_options.random_seed)

        #=== Non-distributed Training ===#
        if run_options.use_distributed_training == 0:
            #=== Neural Network ===#
            NN = AutoencoderFwdInv(hyperp, parameter_dimension,
                                   run_options.full_domain_dimensions,
                                   obs_indices)

            #=== Training ===#
            storage_array_loss_train, storage_array_loss_train_autoencoder, storage_array_loss_train_forward_problem,\
            storage_array_loss_val, storage_array_loss_val_autoencoder, storage_array_loss_val_forward_problem,\
            storage_array_loss_test, storage_array_loss_test_autoencoder, storage_array_loss_test_forward_problem,\
            storage_array_relative_error_parameter_autoencoder, storage_array_relative_error_parameter_inverse_problem, storage_array_relative_error_state_obs\
            = optimize(hyperp, run_options, file_paths, NN, loss_autoencoder, loss_forward_problem, relative_error,\
                       parameter_and_state_obs_train, parameter_and_state_obs_val, parameter_and_state_obs_test,\
                       parameter_dimension, num_batches_train)

        #=== Distributed Training ===#
        if run_options.use_distributed_training == 1:
            dist_strategy = tf.distribute.MirroredStrategy()
            GLOBAL_BATCH_SIZE = hyperp.batch_size * dist_strategy.num_replicas_in_sync
            with dist_strategy.scope():
                #=== Neural Network ===#
                NN = AutoencoderFwdInv(hyperp, parameter_dimension,
                                       run_options.full_domain_dimensions,
                                       obs_indices)

                #=== Training ===#
                storage_array_loss_train, storage_array_loss_train_autoencoder, storage_array_loss_train_forward_problem,\
                storage_array_loss_val, storage_array_loss_val_autoencoder, storage_array_loss_val_forward_problem,\
                storage_array_loss_test, storage_array_loss_test_autoencoder, storage_array_loss_test_forward_problem,\
                storage_array_relative_error_parameter_autoencoder, storage_array_relative_error_parameter_inverse_problem, storage_array_relative_error_state_obs\
                = optimize_distributed(dist_strategy, GLOBAL_BATCH_SIZE, hyperp, run_options, file_paths, NN, loss_autoencoder, loss_forward_problem, relative_error,\
                                       parameter_and_state_obs_train, parameter_and_state_obs_val, parameter_and_state_obs_test,\
                                       parameter_dimension, num_batches_train)

        #=== Saving Metrics ===#
        metrics_dict = {}
        metrics_dict['loss_train'] = storage_array_loss_train
        metrics_dict[
            'loss_train_autoencoder'] = storage_array_loss_train_autoencoder
        metrics_dict[
            'loss_train_forward_problem'] = storage_array_loss_train_forward_problem
        metrics_dict['loss_val'] = storage_array_loss_val
        metrics_dict[
            'loss_val_autoencoder'] = storage_array_loss_val_autoencoder
        metrics_dict[
            'loss_val_forward_problem'] = storage_array_loss_val_forward_problem
        metrics_dict[
            'relative_error_parameter_autoencoder'] = storage_array_relative_error_parameter_autoencoder
        metrics_dict[
            'relative_error_parameter_inverse_problem'] = storage_array_relative_error_parameter_inverse_problem
        metrics_dict[
            'relative_error_state_obs'] = storage_array_relative_error_state_obs
        df_metrics = pd.DataFrame(metrics_dict)
        df_metrics.to_csv(file_paths.NN_savefile_name + "_metrics" + '.csv',
                          index=False)

        return storage_array_loss_val[-1]
def predict_and_save(hyperp, run_options, file_paths):
    #=== Load testing data ===#
    obs_indices, parameter_and_state_obs_test, data_input_shape, parameter_dimension\
    = load_thermal_fin_test_data(file_paths, run_options.num_data_test, run_options.parameter_dimensions,\
                                 hyperp.batch_size, run_options.random_seed)

    ####################################
    #   Import Trained Neural Network  #
    ####################################
    #=== Neural Network ===#
    NN = AutoencoderFwdInv(hyperp, data_input_shape[0],
                           run_options.full_domain_dimensions, obs_indices)
    NN.load_weights(file_paths.NN_savefile_name)

    #######################
    #   Form Predictions  #
    #######################
    #=== From Parameter Instance ===#
    # =============================================================================
    #     df_parameter_test = pd.read_csv(file_paths.savefile_name_parameter_test + '.csv')
    #     parameter_test = df_parameter_test.to_numpy()
    #     df_state_test = pd.read_csv(file_paths.savefile_name_state_test + '.csv')
    #     state_test = df_state_test.to_numpy()
    #
    #     state_pred = NN.encoder(parameter_test.T)
    #     if hyperp.data_type == 'bnd':
    #         state_test_bnd = state_test[obs_indices].flatten()
    #         state_test_bnd = state_test_bnd.reshape(state_test_bnd.shape[0], 1)
    #         parameter_pred = NN.decoder(state_test_bnd.T)
    #     else:
    #         parameter_pred = NN.decoder(state_test.T)
    #
    #     parameter_test = parameter_test.flatten()
    #     state_test = state_test.flatten()
    #     parameter_pred = parameter_pred.numpy().flatten()
    #     state_pred = state_pred.numpy().flatten()
    # =============================================================================

    #=== From Test Batch ===#
    parameter_and_state_obs_test_draw = parameter_and_state_obs_test.take(1)
    for batch_num, (
            parameter_test,
            state_obs_test) in parameter_and_state_obs_test_draw.enumerate():
        parameter_pred_batch = NN.decoder(state_obs_test)
        state_pred_batch = NN.encoder(parameter_test)

    parameter_test = parameter_test[4, :].numpy()
    parameter_pred = parameter_pred_batch[4, :].numpy()
    state_test = state_obs_test[4, :].numpy()
    state_pred = state_pred_batch[4, :].numpy()

    #=== Generating Boundary Data from Full Data ===#
    #state_test = state_test[obs_indices].flatten()

    #####################################
    #   Save Test Case and Predictions  #
    #####################################
    df_parameter_test = pd.DataFrame({'parameter_test': parameter_test})
    df_parameter_test.to_csv(file_paths.savefile_name_parameter_test + '.csv',
                             index=False)
    df_parameter_pred = pd.DataFrame({'parameter_pred': parameter_pred})
    df_parameter_pred.to_csv(file_paths.savefile_name_parameter_pred + '.csv',
                             index=False)
    df_state_test = pd.DataFrame({'state_test': state_test})
    df_state_test.to_csv(file_paths.savefile_name_state_test + '.csv',
                         index=False)
    df_state_pred = pd.DataFrame({'state_pred': state_pred})
    df_state_pred.to_csv(file_paths.savefile_name_state_pred + '.csv',
                         index=False)

    print('\nPredictions Saved to ' + file_paths.NN_savefile_directory)