Exemplo n.º 1
0
def test_scimodel(variable_x, variable_y, functional_fx, functional_gx):
    xs = [variable_x, variable_y]
    ys = [functional_fx, functional_gx]
    assert isinstance(sn.SciModel(xs, ys), sn.SciModel)
    assert isinstance(sn.SciModel(xs, ys, "mse", "adam"), sn.SciModel)
    assert isinstance(sn.SciModel(variable_x, ys), sn.SciModel)
    assert isinstance(sn.SciModel(xs, functional_fx), sn.SciModel)
Exemplo n.º 2
0
def test_scimodel_keras_optimizers(variable_x, variable_y, functional_fx,
                                   functional_gx):
    xs = [variable_x, variable_y]
    ys = [functional_fx, functional_gx]
    assert isinstance(sn.SciModel(xs, ys, "mse", tf_optimizers.Adam()),
                      sn.SciModel)
    assert isinstance(sn.SciModel(xs, ys, "mse", tf_optimizers.RMSprop()),
                      sn.SciModel)
Exemplo n.º 3
0
def test_scimodel_optimizers(variable_x, variable_y, functional_fx,
                             functional_gx):
    xs = [variable_x, variable_y]
    ys = [functional_fx, functional_gx]
    assert isinstance(sn.SciModel(xs, ys, "mse", "adam"), sn.SciModel)
    assert isinstance(sn.SciModel(xs, ys, "mse", "rmsprop"), sn.SciModel)
    assert isinstance(sn.SciModel(xs, ys, "mse", "scipy-l-bfgs-b"),
                      sn.SciModel)
    assert isinstance(sn.SciModel(xs, ys, "mse", "scipy-newtoncg"),
                      sn.SciModel)
Exemplo n.º 4
0
def model_fx_gx(variable_x, functional_fx, functional_gx):
    return sn.SciModel(variable_x, [functional_fx, functional_gx])
Exemplo n.º 5
0
def model_fx(variable_x, functional_fx):
    return sn.SciModel(variable_x, functional_fx)
Exemplo n.º 6
0
def test_eval_scimodel(test_data_xy, expected_hxy, variable_x, variable_y,
                       functional_hxy):
    m = sn.SciModel([variable_x, variable_y], functional_hxy)
    hxy_test = functional_hxy.eval(m, test_data_xy)
    assert np.linalg.norm(hxy_test -
                          expected_hxy) < 1e-6 * np.linalg.norm(expected_hxy)
Exemplo n.º 7
0
def test_scimodel_lossfunc_exceptions(variable_x, variable_y, functional_fx,
                                      functional_gx):
    xs = [variable_x, variable_y]
    ys = [functional_fx, functional_gx]
    with pytest.raises(ValueError):
        assert isinstance(sn.SciModel(xs, ys, "to_fail", "adam"), sn.SciModel)
Exemplo n.º 8
0
def test_scimodel_exceptions(variable_x, variable_y, functional_fx,
                             functional_gx):
    with pytest.raises(ValueError):
        sn.SciModel(variable_x)
    with pytest.raises(ValueError):
        sn.SciModel(functional_fx)
Exemplo n.º 9
0
C1 = (1 - sign(t - TOL)) * (u - sin(np.pi * x) * sin(np.pi * y))
C2 = (1 - sign(t - TOL)) * (diff(u, t))

C3 = (1 - sign(x - TOL)) * u
C4 = (1 - sign(y - TOL)) * u
C5 = (1 + sign(x - (1 - TOL))) * u
C6 = (1 + sign(y - (1 - TOL))) * u

C7 = (1 - sign(x - TOL)) * (diff(u, x, order=2))
C8 = (1 - sign(y - TOL)) * (diff(u, y, order=2))
C9 = (1 + sign(x - (1 - TOL))) * (diff(u, x, order=2))
C10 = (1 + sign(y - (1 - TOL))) * (diff(u, y, order=2))

m = sn.SciModel(
    [x, y, t],
    [sn.PDE(L1), C1, C2, C3, C4, C5, C6, C7, C8, C9, C10],
    # load_weights_from = 'plate-weights.hdf5'
)

inputs = [data['x'], data['y'], data['t']]
targets = [(data['dom_ids'], 'zeros')] \
        + 2*[(data['t0_ids'], 'zeros')] \
        + 8*[(data['bc_ids'], 'zeros')]

h = m.train(
    inputs,
    targets,
    batch_size=BATCH,
    learning_rate=0.001,
    reduce_lr_after=20,
    adaptive_weights={'freq': True},
Exemplo n.º 10
0
u = sn.Functional('u', [x, y, t], 4 * [20], 'sin')
c = 1.0

L1 = c * (diff(u, x, order=2) + diff(u, y, order=2)) - diff(u, t, order=2)

TOL = 0.001
C1 = (1 - sign(t - TOL)) * (u - sin(np.pi * x) * sin(np.pi * y))
C2 = (1 - sign(t - TOL)) * (diff(u, t))
C3 = (1 - sign(x - TOL)) * u
C4 = (1 - sign(y - TOL)) * u
C5 = (1 + sign(x - (1 - TOL))) * u
C6 = (1 + sign(y - (1 - TOL))) * u

m = sn.SciModel(
    [x, y, t],
    [sn.PDE(L1), C1, C2, C3, C4, C5, C6],
    # load_weights_from='membrane-weights.hdf5'
)

inputs = [data['x'], data['y'], data['t']]
targets = [(data['dom_ids'], 'zeros')] \
        + 2*[(data['t0_ids'], 'zeros')] \
        + 4*[(data['bc_ids'], 'zeros')]

h = m.train(inputs,
            targets,
            batch_size=BATCH,
            learning_rate=0.001,
            reduce_lr_after=50,
            adaptive_weights={'freq': True},
            epochs=EPOCHS)
Exemplo n.º 11
0
import numpy as np
import matplotlib.pyplot as plt
import sciann as sn

omega = 2 * np.pi / 2.0
omega_bar = 2 * np.pi / 1.5
A = 1.0
beta = 1.0

tunits = 2
NDATA = 1000

t_data = np.linspace(0, 4 * np.pi, NDATA).reshape(-1, 1)
dt = np.diff(t_data.flatten()).mean()
t_data = t_data + np.linspace(0, dt * (tunits - 1), tunits).reshape(1, -1)

y_data = A * (np.sin(omega * t_data) - beta * np.sin(omega_bar * t_data))

# Add noise
# y_noise = 0.15*np.std(y_data)*np.random.randn(NDATA)

t = sn.functionals.RNNVariable(tunits, 't')
y = sn.functionals.RNNFunctional('y',
                                 t, [1],
                                 'sin',
                                 recurrent_activation='sin')

mRNN = sn.SciModel(t, y)

mRNN.train(t_data, y_data, learning_rate=0.001, epochs=10000, batch_size=100)
Exemplo n.º 12
0
Lambd11 = np.pi * np.sqrt(2)
u_data = np.sin(np.pi * x_data) * np.sin(np.pi * y_data) * np.cos(
    Lambd11 * t_data)

x = sn.Variable('x', dtype='float64')
y = sn.Variable('y', dtype='float64')
t = sn.Variable('t', dtype='float64')
u = sn.Functional('u', [x, y, t], 4 * [20], 'sin')

c = sn.Parameter(np.random.rand(), inputs=[x, y, t], name='c')

L1 = c * (diff(u, x, order=2) + diff(u, y, order=2)) - diff(u, t, order=2)

m = sn.SciModel(
    [x, y, t],
    [sn.PDE(L1), sn.Data(u)],
    # load_weights_from='membrane_inv-weights.hdf5'
)

inputs = [x_data, y_data, t_data]
targets = ['zeros', u_data]

h = m.train(inputs,
            targets,
            batch_size=BATCH,
            learning_rate=0.001,
            reduce_lr_after=50,
            adaptive_weights={'freq': True},
            epochs=EPOCHS,
            log_parameters={
                'parameters': c,