'''
Tutorial on Tensorizer and TensorizedFunction.

'''

import numpy as np
import tensap

# %% Identification of a function f(x) on (0,1) with a function g(i1, ...,id , y)
# x is identified with (i_1, ..., i_d, y) through a Tensorizer
# First interpolate and then truncate

L = 10  # Resolution
B = 3  # Scaling factor

T = tensap.Tensorizer(B, L, 1)


def FUN(x):
    return np.sqrt(x)


FUN = tensap.UserDefinedFunction(FUN, 1)
FUN.evaluation_at_multiple_points = True

TENSORIZED_FUN = T.tensorize(FUN)
TENSORIZED_FUN.fun.evaluation_at_multiple_points = True

# Interpolation of the function in the tensor product feature space
DEGREE = 3
BASES = T.tensorized_function_functional_bases(DEGREE)
CHOICE = 1
if CHOICE == 1:

    def FUN(x):
        return np.sin(10 * np.pi *
                      (2 * x + 0.5)) / (4 * x + 1) + (2 * x - 0.5)**4
elif CHOICE == 2:

    def FUN(x):
        return (np.sin(4*np.pi*x) + 0.2*np.cos(16*np.pi*x))**(x < 0.5) + \
            (2*x-1)*(x >= 0.5)
else:
    raise ValueError('Bad function choice.')

T = tensap.Tensorizer(B, R, 1, X, Y)
TENSORIZED_FUN = T.tensorize(FUN)
TENSORIZED_FUN.fun.evaluation_at_multiple_points = True

# %% Approximation basis
DEGREE = 5
H = tensap.PolynomialFunctionalBasis(Y.orthonormal_polynomials(),
                                     range(DEGREE + 1))
BASES = T.tensorized_function_functional_bases(H)

# %% Training and test samples
NUM_TRAIN = 1000
X_TRAIN = X.random(NUM_TRAIN)
Y_TRAIN = FUN(X_TRAIN)
X_TRAIN = T.map(X_TRAIN)  # Identification of X_TRAIN with (i_1,...,i_d,y)
Пример #3
0
'''

import sys
import numpy as np
sys.path.insert(0, './../../../')
import tensap

# %% Identification of a function f(x) with a function g(i1, ...,id , y)
# x is identified with (i_1, ..., i_d, y) through a Tensorizer
D = 6  # Resolution
B = 3  # Scaling factor

X = tensap.UniformRandomVariable(0, 1)
Y = tensap.UniformRandomVariable(0, 1)

T = tensap.Tensorizer(B, D, 1, X, Y)


def FUN(x):
    return np.sqrt(x)


TENSORIZED_FUN = T.tensorize(FUN)
TENSORIZED_FUN.fun.evaluation_at_multiple_points = True

DEGREE = 4
P = tensap.PolynomialFunctionalBasis(Y.orthonormal_polynomials(),
                                     range(DEGREE + 1))
BASES = T.tensorized_function_functional_bases(P)

H = tensap.FullTensorProductFunctionalBasis(BASES)