コード例 #1
0
ファイル: ex1.py プロジェクト: zhuqunxi/My_numpy_NODE
def train(x_1, t):

    N_epoch = 200
    lr = 0.01
    x_0 = np.array([0.0, 0.0]).reshape(-1, 1)
    loss = []
    plt.ion()
    cnt = 0
    for epoch in range(N_epoch):
        x_1_prd = myode(f, x_0, t, is_full_state=False)  #前向ode
        a_1 = (x_1_prd - x_1)  #  dL/dx1
        a_0 = myode(g, a_1, t[::-1], is_full_state=False)  #反向ode
        x_0 = x_0 - lr * a_0
        ls = np.mean(a_1 * a_1)
        loss.append(ls)
        print('loss:', ls)
        if epoch % (N_epoch // 10) == 0:
            plt.cla()
            trajectory_sample(x_0, t)
            plt.plot(x_1[0], x_1[1], 'pm')
            plt.plot(x_0[0], x_0[1], '*k')
            plt.xlim(-1.5, 1.5)
            plt.ylim(-1.5, 1.5)
            plt.legend(['Trajectory', 'Target state', 'Initial state'], loc=4, fontsize=fontsize-4)
            plt.xlabel('$x_1$', fontsize=fontsize)
            plt.ylabel('$x_2$', fontsize=fontsize)
            plt.savefig(main_image_path + "png_{}.png".format(cnt))
            cnt += 1
            plt.pause(0.1)
    get_gif(cnt, gif_name='ode_init.gif')
    plt.ioff()
    plt.show()
    return x_0
コード例 #2
0
def train(x_1, t):

    N_epoch = 350
    lr = 0.01
    x_0 = np.array([0.0, 0.0]).reshape(-1, 1)
    A = np.random.uniform(-1, 1, size=(2, 2))
    A_init = A.copy()
    print('initial A:\n', A)

    loss = []
    x_0_list = [x_0.flatten()]
    A_list = [A.flatten()]
    plt.figure(figsize=(12, 5))
    plt.ion()
    cnt = 0
    for epoch in range(N_epoch):
        x_1_prd = myode(f, x_0, t, A, is_full_state=False)  #前向ode
        a_1 = (x_1_prd - x_1)  #  dL/dx1
        x_0_back, a_0, A_grad = myode(h, [x_1_prd, a_1, np.zeros_like(A)], t[::-1], A, is_full_state=False)  #反向ode

        x_0 = x_0 - lr * a_0
        A = A - lr * A_grad

        x_0_list.append(x_0.flatten())
        A_list.append(A.flatten())
        ls = np.mean(a_1 * a_1)
        loss.append(ls)
        # print('loss:', ls)
        if epoch % (N_epoch // 10) == 0:
            plt.cla()
            plt.subplot(1, 2, 1)
            trajectory_sample(x_0, t, A)
            plt.plot(x_1[0], x_1[1], 'pm')
            plt.plot(x_0[0], x_0[1], '*k')
            plt.xlim(-1.5, 1.5)
            plt.ylim(-1.5, 1.5)
            plt.legend(['Trajectory', 'Target state', 'Initial state'], loc=4, fontsize=fontsize-4)
            plt.xlabel('$x_1$', fontsize=fontsize)
            plt.ylabel('$x_2$', fontsize=fontsize)

            plt.subplot(1, 2, 2)
            # plt.cla()
            tmp = np.array(A_list)
            for i in range(len(A.flatten())):
                plt.plot(tmp[:, i])
            plt.legend(['$A_{11}$', '$A_{12}$', '$A_{21}$', '$A_{22}$'], loc=4, fontsize=fontsize-4)
            plt.xlabel('Training steps', fontsize=fontsize)
            plt.savefig(main_image_path + "png_{}.png".format(cnt))
            cnt += 1
            plt.pause(0.1)

    get_gif(cnt, gif_name='ode_init_model.gif')
    print('initial A:\n', A_init)
    print('final A:\n', A)
    print('x(0):\n', x_0)
    plt.ioff()
    plt.show()
    return x_0
コード例 #3
0
def train(_y, _t, method='euler_step'):

    x_0 = np.array([2.0, 0.0]).reshape(1, -1)
    A = np.random.uniform(-1, 1, size=(2, 2))
    A_init = A.copy()
    print('initial A:\n', A)

    loss = []
    x_0_list = [x_0.flatten()]
    W1_list, W2_list = [], []

    lr = LR

    plt.figure(figsize=(18, 6))
    plt.ion()
    m_t_1, v_t_1 = np.zeros_like(ode_func.W1), np.zeros_like(ode_func.W1)
    m_t_2, v_t_2 = np.zeros_like(ode_func.W2), np.zeros_like(ode_func.W2)
    m_t_3, v_t_3 = np.zeros_like(ode_func.b1), np.zeros_like(ode_func.b1)
    cnt = 0
    for epoch in range(N_epoch):
        batch_x_0, batch_t = get_batch()

        batch_x_1 = myode(f,
                          batch_x_0,
                          batch_t,
                          method=method,
                          is_full_state=False)  #前向ode
        a_1, ls = dLdx1(batch_x_1)  #  dL/dx1

        print('#' * 100)
        print('batch_x_0 shape:', batch_x_0.shape)
        print('batch_x_1 shape:', batch_x_1.shape)
        print('a_1 shape:', a_1.shape)

        x_0_back, a_0, W1_grad, W2_grad, b1_grad = myode(
            h, [
                batch_x_1, a_1,
                np.zeros_like(ode_func.W1),
                np.zeros_like(ode_func.W2),
                np.zeros_like(ode_func.b1)
            ],
            batch_t[::-1],
            method=method,
            is_full_state=False)  #反向ode

        # x_0 = x_0 - lr * a_0
        # ode_func.W1 -= lr * W1_grad
        # ode_func.W2 -= lr * W2_grad

        ode_func.W1, m_t_1, v_t_1 = my_adam(theta=ode_func.W1,
                                            g_t=W1_grad,
                                            m_t=m_t_1,
                                            v_t=v_t_1,
                                            t=epoch + 1)
        ode_func.W2, m_t_2, v_t_2 = my_adam(theta=ode_func.W2,
                                            g_t=W2_grad,
                                            m_t=m_t_2,
                                            v_t=v_t_2,
                                            t=epoch + 1)
        ode_func.b1, m_t_3, v_t_3 = my_adam(theta=ode_func.b1,
                                            g_t=b1_grad,
                                            m_t=m_t_3,
                                            v_t=v_t_3,
                                            t=epoch + 1)

        W1_list.append(ode_func.W1.flatten())
        W2_list.append(ode_func.W2.flatten())

        x_0_list.append(x_0.flatten())

        loss.append(ls)
        print('loss:', ls)
        if epoch % (N_epoch // 10) == 0:
            lr *= 0.9
            # lr *= 0.7

            plt.clf()
            plt.subplot(1, 3, 1)
            plt.xlabel('epoch', fontsize=fontsize)
            plt.ylabel('loss', fontsize=fontsize)
            plt.plot(loss)
            plt.subplot(1, 3, 2)
            plt.plot(batch_x_0[:, 0], batch_x_0[:, 1], 'k*')
            plt.xlabel('$x_1$', fontsize=fontsize)
            plt.ylabel('$x_2$', fontsize=fontsize)
            plt.subplot(1, 3, 3)
            plt.plot(batch_x_1[:, 0], batch_x_1[:, 1], 'ro')
            plt.xlabel('$x_1$', fontsize=fontsize)
            plt.ylabel('$x_2$', fontsize=fontsize)

            # trajectory_sample(f,  x_0, _t, method=method)
            # # plt.plot(x_1[0], x_1[1], 'pm')
            #
            # _A = np.array([[-0.1, 2.0], [-2.0, -0.1]])
            # trajectory_sample(f_true, x_0, _t, method=method, xz='r--')
            #
            # plt.plot(x_0[:, 0], x_0[:, 1], '*k')
            #
            # scale = 2.5
            # plt.xlim(-scale, scale)
            # plt.ylim(-scale, scale)
            #
            # plt.legend(['Trajectory', 'True Trajectory', 'Initial state'], loc=4)

            # plt.subplot(2, 2, 2)
            # tmp1 = np.array(W1_list)
            # for i in range(len(ode_func.W1.flatten())):
            #     plt.plot(tmp1[:, i])
            # plt.ylabel('$W_1$')
            #
            # plt.subplot(2, 2, 4)
            # tmp1 = np.array(W2_list)
            # for i in range(len(ode_func.W2.flatten())):
            #     plt.plot(tmp1[:, i])
            # # plt.legend(['$A_{11}$', '$A_{12}$', '$A_{21}$', '$A_{22}$'], loc=4)
            # plt.xlabel('Training steps')
            # plt.ylabel('$W_2$')

            plt.savefig(main_image_path + "png_{}.png".format(cnt))
            cnt += 1
            plt.pause(0.1)
    get_gif(cnt, gif_name=gif_name)
    # print('initial A:\n', A_init)
    # print('final A:\n', A)
    # print('True A:\n', _A)
    # print('x(0):\n', x_0)
    print('lr final:', lr)
    plt.ioff()
    return x_0
コード例 #4
0
ファイル: ex4.py プロジェクト: zhuqunxi/My_numpy_NODE
def train(_y, _t):

    N_epoch = 1000
    lr = 0.1
    x_0 = np.array([2.0, 0.0]).reshape(1, -1)
    A = np.random.uniform(-1, 1, size=(2, 2))
    A_init = A.copy()
    print('initial A:\n', A)

    loss = []
    x_0_list = [x_0.flatten()]
    A_list = [A.flatten()]
    plt.figure(figsize=(13, 6))
    plt.ion()
    cnt = 0
    for epoch in range(N_epoch):
        batch_x0, batch_t, batch_x = get_batch(_y, _t)

        batch_x_prd = myode(f, batch_x0, batch_t, A,
                            is_full_state=True)  #前向ode
        # print('batch_x0 shape:', batch_x0.shape)
        # print('batch_x shape:', batch_x.shape)
        # print('batch_x_prd shape:', batch_x_prd.shape)
        a_1 = (batch_x_prd - batch_x)  #  dL/dx1
        x_0_back, a_0, A_grad = myode(
            h, [batch_x_prd[::-1], a_1[::-1],
                np.zeros_like(A)],
            batch_t[::-1],
            A,
            is_full_state=False)  #反向ode
        # x_0 = x_0 - lr * a_0
        A = A - lr * A_grad

        x_0_list.append(x_0.flatten())
        A_list.append(A.flatten())
        ls = np.mean(a_1 * a_1)
        loss.append(ls)
        print('loss:', ls)
        if epoch % (N_epoch // 10) == 0:
            plt.clf()
            plt.subplot(1, 2, 1)
            trajectory_sample(x_0, _t, A, method='euler_step')
            # plt.plot(x_1[0], x_1[1], 'pm')

            _A = np.array([[-0.1, 2.0], [-2.0, -0.1]])
            trajectory_sample(x_0, _t, _A, method='euler_step', xz='r--')

            plt.plot(x_0[:, 0], x_0[:, 1], '*k')

            scale = 2.5
            plt.xlim(-scale, scale)
            plt.ylim(-scale, scale)

            plt.legend(['Trajectory', 'True Trajectory', 'Initial state'],
                       loc=4,
                       fontsize=fontsize - 4)
            plt.xlabel('$x_1$', fontsize=fontsize)
            plt.ylabel('$x_2$', fontsize=fontsize)

            plt.subplot(1, 2, 2)
            # plt.cla()
            tmp = np.array(A_list)
            for i in range(len(A.flatten())):
                plt.plot(tmp[:, i])
            plt.legend(['$A_{11}$', '$A_{12}$', '$A_{21}$', '$A_{22}$'],
                       loc=4,
                       fontsize=fontsize - 4)
            plt.xlabel('Training steps', fontsize=fontsize)
            plt.ylabel('$W_2$', fontsize=fontsize)

            plt.savefig(main_image_path + "png_{}.png".format(cnt))
            cnt += 1
            plt.pause(0.1)

    get_gif(cnt, gif_name='ode_model.gif')
    print('initial A:\n', A_init)
    print('final A:\n', A)
    print('True A:\n', _A)
    print('x(0):\n', x_0)
    plt.ioff()
    plt.show()
    return x_0