Esempio n. 1
0
import os

import torch

import approximator
from approximator.classes.approximation import Approximation
from approximator.examples.richards_remo.func_lib import theta_by_h, res_layer_2_theta_t0, res_layer_2_theta_t1
from approximator.examples.richards_remo.richards import problem, approximation_net

approximation = Approximation(problem=problem, net=approximation_net)


def load_trained_model(trained_model):
    dir_path = os.path.dirname(os.path.realpath(__file__))
    approximation.load(
        os.path.join(dir_path, f"trained_models/{trained_model}/net.pt"))


def integrate_matric_potential(trained_model: int):
    raise NotImplementedError(
        "It is probably useless to integrate? Mean should make more sense, so see below."
    )
    load_trained_model(trained_model)
    t_space = torch.linspace(0, 4 / 60, dtype=approximator.DTYPE)
    z_space = torch.linspace(0, 0.254, dtype=approximator.DTYPE)
    tz_pairs_space = torch.stack((t_space, z_space), dim=-1)
    net_vals = torch.squeeze(approximation.net(tz_pairs_space), dim=1)
    trapz = torch.trapz(net_vals, z_space)
    return trapz

Esempio n. 2
0
import os

import torch

import approximator
from approximator.classes.approximation import Approximation
from approximator.examples.richards.func_lib import theta, K
from approximator.examples.richards.richards import problem, approximation_net, plot_richards_res
from approximator.utils.visualization import plot_approximation, plot_approximation_residuals

approximation = Approximation(problem=problem, net=approximation_net)

dir_path = os.path.dirname(os.path.realpath(__file__))
# approximation.load(os.path.join(dir_path, "trained_models/patience-1/net.pt"))
# approximation.load(os.path.join(dir_path, "trained_models/patience-2-400-steps/net.pt"))
approximation.load(
    os.path.join(
        dir_path,
        "results-thesis/example-run/2021-03-19-21-23-53-510723/net.pt"))

mode = "trapz"
n = 1000

# results n = 1000
# mass_balance 100 steps trapz: tensor(1.66777013e+00, grad_fn=<DivBackward0>)
# mass_balance 400 steps trapz: tensor(1.55000981e+00, grad_fn=<DivBackward0>)

# visualize the approximation
# plot_approximation(approximation)
# plot_approximation_residuals(approximation)
Esempio n. 3
0
def objective(trial):
    # generate model, train it
    domain = Domain(
        x_min=0,  # x is time, t in h
        x_max=.2,
        y_min=0,  # y is spacial, z in m
        y_max=.4)
    problem = Problem(domain, [
        Constraint(identifier="initial condition",
                   condition=lambda x, y: x == 0,
                   prepone=True,
                   residual=lambda x, y, prediction: (prediction -
                                                      (-.615))**2),
        Constraint(identifier="lower boundary",
                   condition=lambda x, y: y == domain.y_min,
                   prepone=True,
                   residual=lambda x, y, prediction: (prediction -
                                                      (-.615))**2),
        Constraint(identifier="upper boundary",
                   condition=lambda x, y: y == domain.y_max,
                   prepone=True,
                   residual=lambda x, y, prediction:
                   (prediction -
                    (-.207 - .408 * torch.exp(-(x / 0.001)**2)))**2),
        Constraint(identifier="pde",
                   condition=lambda x, y: not (x == 0 or y == domain.y_min or y
                                               == domain.y_max),
                   prepone=False,
                   residual=lambda input, prediction:
                   (get_res(input, prediction))**2)
    ])

    suggested_n_hidden_layers = trial.suggest_int("n_hidden_layers", 1, 20)
    suggested_n_neurons_per_layer = trial.suggest_int("n_neurons_per_layer", 1,
                                                      40)

    approximation_net = ApproximationNet(
        n_hidden_layers=suggested_n_hidden_layers,
        n_neurons_per_layer=suggested_n_neurons_per_layer)

    approximation = Approximation(problem=problem, net=approximation_net)

    trial_dir = dir_path.joinpath(f"./trials/{trial.datetime_start}")
    trial_dir.mkdir(exist_ok=True)

    time_start = int(time.time() * 1000)
    approximation.train(learning_rate=1e-3,
                        epochs=int(1e7),
                        pretraining_patience=int(5e4),
                        training_patience=int(5e4),
                        checkpoint_dir_path="/ramdisk",
                        model_path=trial_dir.joinpath(f"./net.pt"),
                        losses_path=trial_dir.joinpath(f"./losses.csv"),
                        discretization=StepsDiscretization(
                            x_steps=100,
                            y_steps=100,
                            x_additional=[domain.x_min],
                            y_additional=[domain.y_min, domain.y_max],
                        ),
                        verbose_output=False)
    time_end = int(time.time() * 1000)

    accuracy = (approximation.pretraining_best_loss
                if not (approximation.pretraining_best_loss is None) else
                float('nan'))

    training_summary = np.array([
        np.concatenate([
            [time_start], [time_end],
            [
                approximation.pretraining_best_loss
                if not (approximation.pretraining_best_loss is None) else
                float('nan')
            ],
            [
                approximation.pretraining_best_loss_epoch if
                not (approximation.pretraining_best_loss_epoch is None) else -1
            ],
            [
                approximation.training_best_loss
                if not (approximation.training_best_loss is None) else
                float('nan')
            ],
            [
                approximation.training_best_loss_epoch
                if not (approximation.training_best_loss_epoch is None) else -1
            ], [accuracy], [t.item() for t in approximation.latest_residuals]
        ]).ravel()
    ])
    np.savetxt(
        trial_dir.joinpath(f"./training.csv"),
        training_summary,
        delimiter=",",
        fmt=(",".join(
            np.concatenate([
                ["%.i", "%.i"],
                ["%.18e", "%.i"],
                ["%.18e", "%.i"],
                ["%.18e"],
                ["%.18e" for _ in approximation.latest_residuals],
            ]).ravel())),
        header=(",".join(
            np.concatenate(
                [["time start", "time end"],
                 ["pretraining best loss", "pretraining best loss epoch"],
                 ["training best loss", "training best loss epoch"],
                 ["accuracy"],
                 [
                     "residual " + str(index + 1)
                     for index, _ in enumerate(approximation.latest_residuals)
                 ]]))))

    return accuracy