Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mini batches #42

Merged
merged 7 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions examples/example_Allen_Cahn_batches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import torch
import numpy as np
import sys
import os

os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..')))

from tedeous.data import Domain, Conditions, Equation
from tedeous.model import Model
from tedeous.callbacks import cache, early_stopping, plot
from tedeous.optimizers.optimizer import Optimizer
from tedeous.device import solver_device
from tedeous.models import Fourier_embedding

solver_device('gpu')

# if the casual_loss is used the time parameter must be
# at the first place in the grid

domain = Domain()

domain.variable('t', [0, 1], 51, dtype='float32')
domain.variable('x', [-1, 1], 51, dtype='float32')

boundaries = Conditions()

# Initial conditions at t=0
x = domain.variable_dict['x']

value = x**2*torch.cos(np.pi*x)

boundaries.dirichlet({'x': [-1, 1], 't': 0}, value=value)


# Initial conditions at t=1
boundaries.periodic([{'x': -1, 't': [0, 1]}, {'x': 1, 't': [0, 1]}])

bop3= {
'du/dx':
{
'coeff': 1,
'du/dx': [1],
'pow': 1,
'var': 0
}
}

boundaries.periodic([{'x': -1, 't': [0, 1]}, {'x': 1, 't': [0, 1]}], operator=bop3)

equation = Equation()

AC = {
'1*du/dt**1':
{
'coeff': 1,
'du/dt': [0],
'pow': 1,
'var': 0
},
'-0.0001*d2u/dx2**1':
{
'coeff': -0.0001,
'd2u/dx2': [1,1],
'pow': 1,
'var': 0
},
'+5u**3':
{
'coeff': 5,
'u': [None],
'pow': 3,
'var': 0
},
'-5u**1':
{
'coeff': -5,
'u': [None],
'pow': 1,
'var': 0
}
}

equation.add(AC)

FFL = Fourier_embedding(L=[None, 2], M=[None, 10])

out = FFL.out_features

net = torch.nn.Sequential(
FFL,
torch.nn.Linear(out, 128),
torch.nn.Tanh(),
torch.nn.Linear(128,128),
torch.nn.Tanh(),
torch.nn.Linear(128,128),
torch.nn.Tanh(),
torch.nn.Linear(128,1)
)

model = Model(net, domain, equation, boundaries, batch_size=32)

model.compile('autograd', lambda_operator=1, lambda_bound=100, tol=10)

img_dir = os.path.join(os.path.dirname( __file__ ), 'AC_eq_img')

cb_cache = cache.Cache(cache_verbose=False, model_randomize_parameter=1e-5)

cb_es = early_stopping.EarlyStopping(eps=1e-7,
loss_window=100,
no_improvement_patience=1000,
patience=5,
abs_loss=1e-5,
info_string_every=1000,
randomize_parameter=1e-5)

cb_plots = plot.Plots(save_every=1000, print_every=None, img_dir=img_dir)

optimizer = Optimizer('Adam', {'lr': 1e-3}, gamma=0.9, decay_every=1000)

model.train(optimizer, 1e5, save_model=True, callbacks=[cb_cache, cb_es, cb_plots])
12 changes: 6 additions & 6 deletions examples/example_Lotka_Volterra.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from tedeous.device import solver_device, check_device, device_type


solver_device('сpu')
solver_device('gpu')

alpha = 20.
beta = 20.
Expand Down Expand Up @@ -113,7 +113,7 @@
torch.nn.Linear(100, 2)
)

model = Model(net, domain, equation, boundaries)
model = Model(net, domain, equation, boundaries, batch_size=32)

model.compile("NN", lambda_operator=1, lambda_bound=100, h=h)

Expand All @@ -133,7 +133,7 @@

optimizer = Optimizer('Adam', {'lr': 1e-4})

model.train(optimizer, 5e6, save_model=True, callbacks=[cb_es, cb_cache, cb_plots])
model.train(optimizer, 5e6, save_model=True, callbacks=[cb_es, cb_cache,cb_plots])

end = time.time()

Expand Down Expand Up @@ -161,8 +161,8 @@ def deriv(X, t, alpha, beta, delta, gamma):
plt.title("odeint and NN methods comparing")
plt.plot(t, x, '+', label = 'preys_odeint')
plt.plot(t, y, '*', label = "predators_odeint")
plt.plot(grid, net(grid)[:,0].detach().numpy().reshape(-1), label='preys_NN')
plt.plot(grid, net(grid)[:,1].detach().numpy().reshape(-1), label='predators_NN')
plt.plot(grid.cpu(), net(grid.cpu())[:,0].detach().numpy().reshape(-1), label='preys_NN')
plt.plot(grid.cpu(), net(grid.cpu())[:,1].detach().numpy().reshape(-1), label='predators_NN')
plt.xlabel('Time t, [days]')
plt.ylabel('Population')
plt.legend(loc='upper right')
Expand All @@ -171,7 +171,7 @@ def deriv(X, t, alpha, beta, delta, gamma):
plt.figure()
plt.grid()
plt.title('Phase plane: prey vs predators')
plt.plot(net(grid)[:,0].detach().numpy().reshape(-1), net(grid)[:,1].detach().numpy().reshape(-1), '-*', label='NN')
plt.plot(net(grid.cpu())[:,0].detach().numpy().reshape(-1), net(grid.cpu())[:,1].detach().numpy().reshape(-1), '-*', label='NN')
plt.plot(x,y, label='odeint')
plt.xlabel('preys')
plt.ylabel('predators')
Expand Down
107 changes: 79 additions & 28 deletions examples/example_Lotka_Volterra_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,70 @@
x0 = 4.
y0 = 2.
t0 = 0.
tmax = 1.
tmax = 1


from copy import deepcopy


# Define the model
class MultiOutputModel(torch.nn.Module):
def __init__(self):
super(MultiOutputModel, self).__init__()

self.width_out=[2]

# Shared layers (base network)
self.shared_fc1 = torch.nn.Linear(1, 100) # Input size of 1 (for t)
self.shared_fc2 = torch.nn.Linear(100, 100)
self.shared_fc3 = torch.nn.Linear(100, 100)
self.shared_fc4 = torch.nn.Linear(100, 100)
# Output head for Process 1
self.process1_fc = torch.nn.Linear(100, 1)

# Output head for Process 2
self.process2_fc = torch.nn.Linear(100, 1)

def forward(self, t):
# Shared layers forward pass
x = torch.tanh(self.shared_fc1(t))
x = torch.tanh(self.shared_fc2(x))
x = torch.tanh(self.shared_fc3(x))
x = torch.tanh(self.shared_fc4(x))
# Process 1 output head
process1_out = self.process1_fc(x)

# Process 2 output head
process2_out = self.process2_fc(x)

out=torch.cat((process1_out, process2_out), dim=1)

return out

# Initialize the model
#model =


def Lotka_experiment(grid_res, CACHE):

exp_dict_list = []
solver_device('gpu')

solver_device('cpu')
#net = torch.nn.Sequential(
# torch.nn.Linear(1, 32),
# torch.nn.Tanh(),
# torch.nn.Linear(32, 32),
# torch.nn.Tanh(),
# torch.nn.Linear(32, 2)
#)

domain = Domain()
domain.variable('t', [0, tmax], grid_res)
net=MultiOutputModel()

h = 0.0001



domain = Domain()
domain.variable('t', [0, 1], grid_res)

boundaries = Conditions()
#initial conditions
Expand Down Expand Up @@ -104,35 +157,30 @@ def Lotka_experiment(grid_res, CACHE):
equation.add(eq1)
equation.add(eq2)

net = torch.nn.Sequential(
torch.nn.Linear(1, 100),
torch.nn.Tanh(),
torch.nn.Linear(100, 100),
torch.nn.Tanh(),
torch.nn.Linear(100, 100),
torch.nn.Tanh(),
torch.nn.Linear(100, 2)
)

model = Model(net, domain, equation, boundaries)

model.compile("NN", lambda_operator=1, lambda_bound=100, h=h)
model = Model(net, domain, equation, boundaries, batch_size=64)

model.compile("autograd", lambda_operator=1, lambda_bound=100)

img_dir=os.path.join(os.path.dirname( __file__ ), 'img_Lotka_Volterra_paper')

start = time.time()

cb_es = early_stopping.EarlyStopping(eps=1e-6,
loss_window=100,
no_improvement_patience=500,
patience=3,
randomize_parameter=1e-5)
loss_window=1000,
no_improvement_patience=500,
patience=3,
info_string_every=100,
randomize_parameter=1e-5)

cb_plots = plot.Plots(save_every=1000, print_every=None, img_dir=img_dir)

#cb_cache = cache.Cache(cache_verbose=True, model_randomize_parameter=1e-5)

optimizer = Optimizer('Adam', {'lr': 1e-4})

model.train(optimizer, 5e6, save_model=True, callbacks=[cb_es, cb_plots])
model.train(optimizer, 2e5, save_model=True, callbacks=[cb_es, cb_plots])

end = time.time()

Expand All @@ -151,7 +199,7 @@ def deriv(X, t, alpha, beta, delta, gamma):
doty = y * (-delta + gamma * x)
return np.array([dotx, doty])

t = np.linspace(0., tmax, grid_res+1)
t = np.linspace(0, 1, grid_res+1)

X0 = [x0, y0]
res = integrate.odeint(deriv, X0, t, args = (alpha, beta, delta, gamma))
Expand All @@ -169,30 +217,33 @@ def deriv(X, t, alpha, beta, delta, gamma):
print('Time taken {}= {}'.format(grid_res, end - start))
print('RMSE {}= {}'.format(grid_res, error_rmse))

t = domain.variable_dict['t']
#t = domain.variable_dict['t']
grid = domain.build('NN')

t = np.linspace(0, 1, grid_res+1)

plt.figure()
plt.grid()
plt.title("odeint and NN methods comparing")
plt.plot(t, u_exact[:,0].detach().numpy().reshape(-1), '+', label = 'preys_odeint')
plt.plot(t, u_exact[:,1].detach().numpy().reshape(-1), '*', label = "predators_odeint")
plt.plot(grid, net(grid)[:,0].detach().numpy().reshape(-1), label='preys_NN')
plt.plot(grid, net(grid)[:,1].detach().numpy().reshape(-1), label='predators_NN')
plt.plot(grid.cpu(), net(grid.cpu())[:,0].detach().numpy().reshape(-1), label='preys_NN')
plt.plot(grid.cpu(), net(grid.cpu())[:,1].detach().numpy().reshape(-1), label='predators_NN')
plt.xlabel('Time t, [days]')
plt.ylabel('Population')
plt.legend(loc='upper right')
plt.show()
plt.savefig(os.path.join(img_dir,'compare_{}.png'.format(grid_res)))


return exp_dict_list

nruns=10
nruns=1

exp_dict_list=[]

CACHE=False

for grid_res in range(60,101,10):
for grid_res in range(60,1001,100):
for _ in range(nruns):
exp_dict_list.append(Lotka_experiment(grid_res,CACHE))

Expand Down
2 changes: 1 addition & 1 deletion examples/example_wave_adaptive_lambdas.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def func(grid):
abs_loss=0.1,
info_string_every=500)

img_dir=os.path.join(os.path.dirname( __file__ ), 'wave_eq_img')
img_dir=os.path.join(os.path.dirname( __file__ ), 'wave_eq_img_nobatch')

cb_plots = plot.Plots(save_every=500, print_every=None, img_dir=img_dir)

Expand Down
Loading