Ejemplo n.º 1
0
    def setUp(self):
        """..."""

        self.tol = 1e-10
        self.d = 10
        self.m = 5
        self.n = 20
        self.data = np.random.rand(self.d, self.m)
        self.data_2 = np.random.rand(self.d, self.n)
        self.phi_1 = [[tdt.constant_function(), tdt.identity(i), tdt.monomial(i,2)] for i in range(self.d)]
        self.psi_1 = [lambda t: 1, lambda t: t, lambda t: t**2]
        self.phi_2 = [[tdt.constant_function()] + [tdt.sin(i,1) for i in range(self.d)], [tdt.constant_function()] + [tdt.cos(i,1) for i in range(self.d)] ]
        self.psi_2 = [lambda t: np.sin(t), lambda t: np.cos(t)]
Ejemplo n.º 2
0
    def test_basis_functions(self):
        """test basis functions"""

        constant = tdt.constant_function()
        indicator = tdt.indicator_function(0,0,0.5)
        identity = tdt.identity(0)
        monomial = tdt.monomial(0,2)
        sin = tdt.sin(0,1)
        cos = tdt.cos(0,1)
        gauss = tdt.gauss_function(0,1,1)
        periodic_gauss = tdt.periodic_gauss_function(0,1,1)
        
        self.assertEqual(np.sum(np.abs(constant(self.data)-np.ones(self.m))), 0)
        self.assertEqual(np.sum(np.abs(indicator(self.data)-np.logical_and(self.data[0, :]>=0, self.data[0, :]<0.5))), 0)
        self.assertEqual(np.sum(np.abs(identity(self.data)-self.data[0, :])), 0)
        self.assertEqual(np.sum(np.abs(monomial(self.data)-self.data[0, :]**2)), 0)
        self.assertEqual(np.sum(np.abs(sin(self.data)-np.sin(self.data[0,:]))), 0)
        self.assertEqual(np.sum(np.abs(cos(self.data)-np.cos(self.data[0,:]))), 0)
        self.assertEqual(np.sum(np.abs(gauss(self.data)-np.exp(-0.5 * (self.data[0,:] - 1) ** 2))), 0)
        self.assertEqual(np.sum(np.abs(periodic_gauss(self.data)-np.exp(-0.5 * np.sin(0.5 * (self.data[0,:] - 1)) ** 2))), 0)
Ejemplo n.º 3
0
def classification_arr(data_path, m_start, m_final, m_step, rank):
    """Alternating ridge regression for classification.

    Parameters
    ----------
    data_path: string
        path of data to load
    m_start: int
        minimum number of images
    m_final: int
        maximum number of images
    m_step: int
        step size for number of images
    rank: int
        TT rank of coefficient tensor
    Returns
    -------
    classification_rates: list of floats
        amount of correctly identified images
    cpu_times: list of floats
        run times of training phases
    """

    # load data
    data = np.load(data_path)
    x_train = data['tr_img']
    y_train = data['tr_lbl']
    x_test = data['te_img']
    y_test = data['te_lbl']

    # order of the transformed data tensor
    order = x_train.shape[0]

    # define basis functions
    alpha = 19 / 100 * np.pi
    basis_list = []
    for i in range(order):
        basis_list.append([tdt.cos(i, alpha), tdt.sin(i, alpha)])

    # initial guess
    ranks = [1] + [rank for _ in range(order - 1)] + [1]
    cores = [
        0.001 * np.ones([ranks[i], 2, 1, ranks[i + 1]]) for i in range(order)
    ]
    initial_guess = TT(cores).ortho()

    # define lists
    classification_rates = []
    cpu_times = []

    # output
    print('Images' + 8 * ' ' + 'Classification rate' + 6 * ' ' + 'CPU time')
    print(47 * '-')

    # loop over image numbers
    for m in range(m_start, m_final, m_step):

        # training phase (apply ARR)
        with utl.timer() as timer:
            xi = reg.arr(x_train[:, :m],
                         y_train[:, :m],
                         basis_list,
                         initial_guess,
                         repeats=5,
                         rcond=10**(-2),
                         progress=False)
        cpu_time = timer.elapsed

        # test phase (contract xi with transformed data tensor)
        d = y_test.shape[0]
        solution = []
        for k in range(d):
            solution_vector = np.ones([1, 1])
            for l in range(order):
                n = len(basis_list[l])
                theta = np.array([basis_list[l][k](x_test) for k in range(n)])
                solution_vector = np.einsum('ij,kj->ijk', solution_vector,
                                            theta)
                solution_vector = np.tensordot(xi[k].cores[l],
                                               solution_vector,
                                               axes=([0, 1], [0, 2]))[0, :, :]
            solution.append(solution_vector)
        solution = np.vstack(solution)

        # compute classification rate
        n = y_test.shape[1]
        sol = np.zeros(y_test.shape)
        sol[np.argmax(solution, axis=0), np.arange(0, n)] = 1
        classification_rate = 100 - 50 * np.sum(np.abs(sol - y_test)) / n

        # print results
        str_m = str(m)
        len_m = len(str_m)
        str_c = str("%.2f" % classification_rate + '%')
        len_c = len(str_c)
        str_t = str("%.2f" % cpu_time + 's')
        len_t = len(str_t)
        print(str_m + (20 - len_m) * ' ' + str_c + (27 - len_c - len_t) * ' ' +
              str_t)
        classification_rates.append(classification_rate)
        cpu_times.append(cpu_time)

    print(' ')

    return classification_rates, cpu_times
Ejemplo n.º 4
0
def classification_mandy(data_path, m_start, m_final, m_step):
    """Kernel-based MANDy for classification.

    Parameters
    ----------
    data_path: string
        path of data to load
    m_start: int
        minimum number of images
    m_final: int
        maximum number of images
    m_step: int
        step size for number of images

    Returns
    -------
    classification_rates: list of floats
        amount of correctly identified images
    cpu_times: list of floats
        run times of training phases
    """

    # load data
    data = np.load(data_path)
    x_train = data['tr_img']
    y_train = data['tr_lbl']
    x_test = data['te_img']
    y_test = data['te_lbl']

    # order of the transformed data tensor
    order = x_train.shape[0]

    # define basis functions
    alpha = 19 / 100 * np.pi
    basis_list = []
    for i in range(order):
        basis_list.append([tdt.cos(i, alpha), tdt.sin(i, alpha)])

    # define lists
    classification_rates = []
    cpu_times = []

    # output
    print('Images' + 8 * ' ' + 'Classification rate' + 6 * ' ' + 'CPU time')
    print(47 * '-')

    # loop over image numbers
    for m in range(m_start, m_final, m_step):

        # training phase (apply kernel-based MANDy)
        with utl.timer() as timer:
            z = reg.mandy_kb(x_train[:, :m], y_train[:, :m], basis_list)
        cpu_time = timer.elapsed

        # test phase (multiply z with gram matrix)
        gram = tdt.gram(x_train[:, :m], x_test, basis_list)
        solution = z.dot(gram)

        # compute classification rate
        n = y_test.shape[1]
        sol = np.zeros(y_test.shape)
        sol[np.argmax(solution, axis=0), np.arange(0, n)] = 1
        classification_rate = 100 - 50 * np.sum(np.abs(sol - y_test)) / n

        # print results
        str_m = str(m)
        len_m = len(str_m)
        str_c = str("%.2f" % classification_rate + '%')
        len_c = len(str_c)
        str_t = str("%.2f" % cpu_time + 's')
        len_t = len(str_t)
        print(str_m + (20 - len_m) * ' ' + str_c + (27 - len_c - len_t) * ' ' +
              str_t)
        classification_rates.append(classification_rate)
        cpu_times.append(cpu_time)

    print(' ')

    return classification_rates, cpu_times
Ejemplo n.º 5
0
    def setUp(self):
        """Consider the Fermi-Pasta-Ulam problem and Kuramoto model for testing the routines in sle.py"""

        # set tolerance
        self.tol = 1e-5

        # number of oscillators
        self.fpu_d = 4
        self.kuramoto_d = 10

        # parameters for the Fermi-Pasta_ulam problem
        self.fpu_m = 2000
        self.fpu_psi = [
            lambda t: 1, lambda t: t, lambda t: t**2, lambda t: t**3
        ]

        # parameters for the Kuramoto model
        self.kuramoto_x_0 = 2 * np.pi * np.random.rand(self.kuramoto_d) - np.pi
        self.kuramoto_w = np.linspace(-5, 5, self.kuramoto_d)
        self.kuramoto_t = 100
        self.kuramoto_m = 1000
        self.kuramoto_psi = [lambda t: np.sin(t), lambda t: np.cos(t)]
        self.kuramoto_basis = [[tdt.constant_function()] +
                               [tdt.sin(i, 1) for i in range(self.kuramoto_d)],
                               [tdt.constant_function()] +
                               [tdt.cos(i, 1) for i in range(self.kuramoto_d)]]
        self.kuramoto_initial = tt.ones([11, 11], [1, 1], 11)

        # exact coefficient tensors
        self.fpu_xi_exact = mdl.fpu_coefficients(self.fpu_d)
        self.kuramoto_xi_exact = mdl.kuramoto_coefficients(
            self.kuramoto_d, self.kuramoto_w)

        # generate test data for FPU
        self.fpu_x = 0.2 * np.random.rand(self.fpu_d, self.fpu_m) - 0.1
        self.fpu_y = np.zeros((self.fpu_d, self.fpu_m))
        for j in range(self.fpu_m):
            self.fpu_y[0,
                       j] = self.fpu_x[1, j] - 2 * self.fpu_x[0, j] + 0.7 * (
                           (self.fpu_x[1, j] - self.fpu_x[0, j])**3 -
                           self.fpu_x[0, j]**3)
            for i in range(1, self.fpu_d - 1):
                self.fpu_y[i, j] = self.fpu_x[
                    i + 1,
                    j] - 2 * self.fpu_x[i, j] + self.fpu_x[i - 1, j] + 0.7 * (
                        (self.fpu_x[i + 1, j] - self.fpu_x[i, j])**3 -
                        (self.fpu_x[i, j] - self.fpu_x[i - 1, j])**3)
                self.fpu_y[-1, j] = -2 * self.fpu_x[-1, j] + self.fpu_x[
                    -2, j] + 0.7 * (-self.fpu_x[-1, j]**3 -
                                    (self.fpu_x[-1, j] - self.fpu_x[-2, j])**3)

        # generate test data for Kuramoto
        number_of_oscillators = len(self.kuramoto_x_0)

        def kuramoto_ode(_, theta):
            [theta_i, theta_j] = np.meshgrid(theta, theta)
            return self.kuramoto_w + 2 / number_of_oscillators * np.sin(
                theta_j - theta_i).sum(0) + 0.2 * np.sin(theta)

        sol = spint.solve_ivp(kuramoto_ode, [0, self.kuramoto_t],
                              self.kuramoto_x_0,
                              method='BDF',
                              t_eval=np.linspace(0, self.kuramoto_t,
                                                 self.kuramoto_m))
        self.kuramoto_x = sol.y
        self.kuramoto_y = np.zeros([number_of_oscillators, self.kuramoto_m])
        for i in range(self.kuramoto_m):
            self.kuramoto_y[:, i] = kuramoto_ode(0, self.kuramoto_x[:, i])