from approximator.classes.net import ApproximationNet from approximator.classes.problem import Problem, Domain from approximator.examples.heat.func_lib import get_res from approximator.utils.visualization import plot_approximation, plot_approximation_residuals, \ plot_approximation_deviation, plot_domain problem = Problem( Domain( x_min=0, # x is time x_max=1, y_min=-1, # y is spacial y_max=+1), [ Constraint(identifier="initial condition", condition=lambda x, y: x == 0, prepone=True, residual=lambda x, y, prediction: (prediction - (torch.sin(math.pi * y / 2 + math.pi / 2)))**2), Constraint(identifier="upper boundary", condition=lambda x, y: y == -1, prepone=True, residual=lambda x, y, prediction: (prediction - 0)**2), Constraint(identifier="lower boundary", condition=lambda x, y: y == +1, prepone=True, residual=lambda x, y, prediction: (prediction - 0)**2), Constraint(identifier="pde", condition=lambda x, y: not (x == 0 or y == -1 or y == +1), prepone=False, residual=lambda input, prediction: (get_res(input, prediction))**2)
pass def get_res_initial(x, y, prediction): epsilon = 0.01 initial_h_curved = initial_h - \ torch.exp(-((y - domain.y_min) / epsilon) ** 2) * (initial_h - lower_boundary_h) - \ torch.exp(-((y - domain.y_max) / epsilon) ** 2) * (initial_h - upper_boundary_h) return prediction - initial_h_curved problem = Problem( domain, [ Constraint(identifier="initial condition", condition=lambda x, y: x == domain.x_min, residual=lambda x, y, prediction: (get_res_initial(x, y, prediction)) ** 2, prepone=True), Constraint(identifier="upper boundary", condition=lambda x, y: y == domain.y_max, residual=lambda input, prediction: (prediction - upper_boundary_h) ** 2, prepone=True), Constraint(identifier="lower boundary", condition=lambda x, y: y == domain.y_min, residual=lambda x, y, prediction: (prediction - lower_boundary_h) ** 2, prepone=True), Constraint(identifier="pde", condition=lambda x, y: not (x == domain.x_min or y == domain.y_min or y == domain.y_max), residual=lambda input, prediction: (get_res(input, prediction)) ** 2) ] ) # hyperstudy for richards => 8 hidden, with 21 neurons approximation_net = ApproximationNet(n_hidden_layers=8, n_neurons_per_layer=21) # approximation_net = ApproximationNet(n_hidden_layers=12, n_neurons_per_layer=25) approximation = Approximation(
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
from approximator.classes.approximation import Approximation from approximator.classes.discretization import Discretization, StepSizeDiscretization from approximator.classes.constraint import Constraint from approximator.classes.net import ApproximationNet from approximator.classes.problem import Problem, Domain from approximator.utils.visualization import plot_approximation def in_circle(x, y): r = 0.2 a, b = 0.5, 0.5 return ((x - a)**2 + (y - b)**2)**(1 / 2) <= r problem = Problem(Domain(x_min=0, x_max=1, y_min=0, y_max=1), [ Constraint("in circle constraint", lambda x, y: in_circle(x, y), lambda x, y, prediction: (prediction - 1)**2), Constraint("out of circle constraint", lambda x, y: not in_circle(x, y), lambda x, y, prediction: (prediction - 0)**2) ]) approximation_net = ApproximationNet(n_hidden_layers=5, n_neurons_per_layer=10) approximation = Approximation(problem=problem, net=approximation_net) def plot_circle(): approximation.train(discretization=StepSizeDiscretization(x_step=.01, y_step=.01), learning_rate=.001, epochs=int(2e3)) print('quick check result:' + str(approximation.use(.5, .5)))
import numpy as np import torch import approximator from approximator.classes.approximation import Approximation from approximator.classes.constraint import Constraint from approximator.classes.discretization import StepsDiscretization from approximator.classes.net import ApproximationNet from approximator.classes.problem import Problem, Domain from approximator.utils.visualization import plot_approximation problem = Problem(Domain(x_min=0, x_max=1, y_min=0, y_max=1), [ Constraint("Flat boundary", lambda x, y: x == 0 or x == 1 or y == 0, lambda x, y, prediction: (prediction - 0)**2, prepone=True), Constraint("Sin boundary", lambda x, y: y == 1, lambda x, y, prediction: (prediction - torch.sin(np.pi * x))**2, prepone=True), Constraint("PDE", lambda x, y: not (x == 0 or x == 1 or y == 0 or y == 1), lambda input, prediction: laplace(input, prediction)**2, prepone=False) ]) approximation_net = ApproximationNet(n_hidden_layers=5, n_neurons_per_layer=10) approximation = Approximation(problem=problem, net=approximation_net)
from approximator.classes.approximation import Approximation from approximator.classes.discretization import Discretization, StepSizeDiscretization from approximator.classes.constraint import Constraint from approximator.classes.net import ApproximationNet from approximator.classes.problem import Domain, Problem from approximator.utils.visualization import plot_approximation problem = Problem( Domain( x_min=0, x_max=1, y_min=0, y_max=1 ), [ Constraint("sin", lambda x, y: True, lambda x, y, prediction: (prediction - torch.sin(x) * torch.cos(y)) ** 2) ] ) approximation_net = ApproximationNet(n_hidden_layers=5, n_neurons_per_layer=10) approximation = Approximation( problem=problem, net=approximation_net ) def plot_sin(): approximation.train( discretization=StepSizeDiscretization( x_step=.01,
from approximator.classes.net import ApproximationNet from approximator.classes.problem import Problem, Domain from approximator.examples.richards.func_lib import get_res from approximator.utils.visualization import plot_x_y_z 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.01) ** 2))) ** 2), Constraint(identifier="upper boundary", condition=lambda x, y: y == domain.y_max, prepone=True, residual=lambda x, y, prediction: (prediction -
import torch import numpy as np import approximator from approximator.classes.approximation import Approximation from approximator.classes.discretization import Discretization, StepsDiscretization from approximator.classes.constraint import Constraint from approximator.classes.net import ApproximationNet from approximator.classes.problem import Problem, Domain from approximator.utils.visualization import plot_approximation problem = Problem(Domain(x_min=0, x_max=1, y_min=0, y_max=1), [ Constraint( "Boundaries", lambda x, y: 1 >= x >= 0 == y, lambda x, y, prediction: (prediction - (x**2 + torch.exp(-x**2)))**2), Constraint("PDE", lambda x, y: not (1 >= x >= 0 == y), lambda input, prediction: conservation(input, prediction)**2) ]) approximation_net = ApproximationNet(n_hidden_layers=5, n_neurons_per_layer=10) approximation = Approximation(problem=problem, net=approximation_net) def conservation(input, prediction): input_x = input[:, 0:1] input_y = input[:, 1:2] ones = torch.unsqueeze( torch.ones(len(input), dtype=approximator.DTYPE,