Пример #1
0
    # In[Second-order dynamical system custom defined]
    G = SisoLinearDynamicalOperator(n_b, n_a)
    H_inv = SisoLinearDynamicalOperator(2, 2, n_k=1)

    with torch.no_grad():
        G.b_coeff[0, 0, 0] = 0.01
        G.b_coeff[0, 0, 1] = 0.0

        G.a_coeff[0, 0, 0] = -0.9
        G.b_coeff[0, 0, 1] = 0.01

    # In[Setup optimizer]
    optimizer = torch.optim.Adam([
        {
            'params': G.parameters(),
            'lr': lr
        },
        {
            'params': H_inv.parameters(),
            'lr': lr
        },
    ],
                                 lr=lr)

    # In[Train]
    LOSS = []
    start_time = time.time()
    for itr in range(0, num_iter):

        optimizer.zero_grad()
Пример #2
0
    n_u = u.shape[2]
    n_y = y.shape[2]

    # In[To tensors]
    u_torch = torch.tensor(u, dtype=torch.float32)
    y_torch = torch.tensor(y, dtype=torch.float32)

    # In[Deterministic model]
    G = SisoLinearDynamicalOperator(n_b, n_a, n_k=n_k)
    F = SisoStaticNonLinearity(n_hidden=10)

    # In[Log-likelihood]


    optimizer = torch.optim.Adam([
        {'params': G.parameters(),    'lr': lr},
        {'params': F.parameters(), 'lr': lr},
    ], lr=lr)

    # In[Train]
    LOSS = []
    start_time = time.time()
    for itr in range(0, num_iter):

        optimizer.zero_grad()

        # Simulate
        y_lin = G(u_torch)
        y_nl = F(y_lin)
        y_hat = y_nl
Пример #3
0
    y_fit_torch = torch.tensor(y_fit[None, ...],
                               dtype=torch.float,
                               requires_grad=False)
    y_hidden_torch = torch.tensor(y_fit[None, ...],
                                  dtype=torch.float,
                                  requires_grad=True)
    # optimize on the output to manage the feedback connection
    # In[First dynamical system custom defined]
    G1 = SisoLinearDynamicalOperator(n_b, n_a, n_k)
    # Static non-linearity
    F_nl = SisoStaticNonLinearity()

    # Setup optimizer
    optimizer = torch.optim.Adam([
        {
            'params': G1.parameters(),
            'lr': lr
        },
        {
            'params': F_nl.parameters(),
            'lr': lr
        },
        {
            'params': [y_hidden_torch],
            'lr': 1e-3
        },
    ],
                                 lr=lr)

    # In[Structure]
    #   u ---> ----> G ------> y_lin
Пример #4
0
    # In[Prepare model]
    G1 = SisoLinearDynamicalOperator(n_b, n_a, n_k=1)
    F_nl = SisoStaticNonLinearity(n_hidden=10, activation='tanh')
    G2 = SisoLinearDynamicalOperator(n_b, n_a)

    def model(u_in):
        y1_lin = G1(u_fit_torch)
        y1_nl = F_nl(y1_lin)
        y_hat = G2(y1_nl)
        return y_hat, y1_nl, y1_lin

    # In[Setup optimizer]
    optimizer_ADAM = torch.optim.Adam([
        {
            'params': G1.parameters(),
            'lr': lr_ADAM
        },
        {
            'params': G2.parameters(),
            'lr': lr_ADAM
        },
        {
            'params': F_nl.parameters(),
            'lr': lr_ADAM
        },
    ],
                                      lr=lr_ADAM)

    optimizer_LBFGS = torch.optim.LBFGS(list(G1.parameters()) +
                                        list(G2.parameters()) +
Пример #5
0
    n_u = u.shape[2]
    n_y = y.shape[2]

    # In[To tensors]
    u_torch = torch.tensor(u, dtype=torch.float32)
    y_torch = torch.tensor(y, dtype=torch.float32)

    # In[Deterministic model]
    G = SisoLinearDynamicalOperator(n_b, n_a, n_k=n_k)
    F = SisoStaticNonLinearity(n_hidden=10)

    # In[Log-likelihood]

    optimizer = torch.optim.Adam([
        {
            'params': G.parameters(),
            'lr': lr
        },
        {
            'params': F.parameters(),
            'lr': lr
        },
    ],
                                 lr=lr)

    # In[Train]
    LOSS = []
    start_time = time.time()
    for itr in range(0, num_iter):

        optimizer.zero_grad()
Пример #6
0
    std_noise_V = add_noise * 0.1
    #y_nonoise = np.copy(1 + x[:, [0]] + x[:, [0]]**2)
    y_nonoise = np.copy(x[:, [0, 1]])  #np.copy(1 + x[:, [0]] ** 3)
    y_noise = y_nonoise + np.random.randn(*y_nonoise.shape) * std_noise_V

    # Prepare data
    u_torch = torch.tensor(u[None, :, :],
                           dtype=torch.float,
                           requires_grad=False)
    y_meas_torch = torch.tensor(y_noise[None, :, :], dtype=torch.float)
    y_true_torch = torch.tensor(y_nonoise[None, :, :], dtype=torch.float)
    G = SisoLinearDynamicalOperator(n_b, n_a, n_k=1)
    nn_static = SisoStaticNonLinearity()

    # Setup optimizer
    params_lin = G.parameters()
    optimizer = torch.optim.Adam([{
        'params': params_lin,
        'lr': lr
    }, {
        'params': nn_static.parameters(),
        'lr': lr
    }],
                                 lr=lr)

    # In[Train]
    LOSS = []
    start_time = time.time()
    for itr in range(0, num_iter):

        optimizer.zero_grad()
Пример #7
0
    G1 = SisoLinearDynamicalOperator(n_b, n_a, n_k=1)
    F_nl = SisoStaticNonLinearity(n_hidden=10, activation='tanh')
    G2 = SisoLinearDynamicalOperator(n_b, n_a)

    H_inv = SisoLinearDynamicalOperator(2, 2, n_k=1)

    def model(u_in):
        y1_lin = G1(u_fit_torch)
        y1_nl = F_nl(y1_lin)
        y_hat = G2(y1_nl)
        return y_hat, y1_nl, y1_lin

    # In[Setup optimizer]
    optimizer_ADAM = torch.optim.Adam([
        {
            'params': G1.parameters(),
            'lr': lr_ADAM
        },
        {
            'params': G2.parameters(),
            'lr': lr_ADAM
        },
        {
            'params': F_nl.parameters(),
            'lr': lr_ADAM
        },
        {
            'params': H_inv.parameters(),
            'lr': lr_ADAM
        },
    ],
    y_fit_torch = torch.tensor(y_fit[None, :, :], dtype=torch.float)

    # In[Prepare model]
    G1 = SisoLinearDynamicalOperator(n_b, n_a, n_k=1)
    F_nl = SisoStaticNonLinearity(n_hidden=10, activation='tanh')
    G2 = SisoLinearDynamicalOperator(n_b, n_a)

    def model(u_in):
        y1_lin = G1(u_fit_torch)
        y1_nl = F_nl(y1_lin)
        y_hat = G2(y1_nl)
        return y_hat, y1_nl, y1_lin

    # In[Setup optimizer]
    optimizer_ADAM = torch.optim.Adam([
        {'params': G1.parameters(), 'lr': lr_ADAM},
        {'params': G2.parameters(), 'lr': lr_ADAM},
        {'params': F_nl.parameters(), 'lr': lr_ADAM},
    ], lr=lr_ADAM)

    optimizer_LBFGS = torch.optim.LBFGS(list(G1.parameters()) + list(G2.parameters()) + list(F_nl.parameters()), lr=lr_BFGS)


    def closure():
        optimizer_LBFGS.zero_grad()

        # Simulate
        y_hat, y1_nl, y1_lin = model(u_fit_torch)

        # Compute fit loss
        err_fit = y_fit_torch[:, n_skip:, :] - y_hat[:, n_skip:, :]