Beispiel #1
0
    def __init__(self, bases):
        '''
        Constructor for the class FullTensorProductFunctionalBasis.

        Parameters
        ----------
        bases : list or tensap.FunctionalBases
            The bases associated with the object.

        Returns
        -------
        None.

        '''
        tensap.FunctionalBasis.__init__(self)

        if isinstance(bases, list):
            bases = tensap.FunctionalBases(bases)

        assert isinstance(bases, tensap.FunctionalBases), \
            'The first argument must be a FunctionalBases.'

        self.bases = bases
        self.measure = tensap.ProductMeasure([x.measure for x in bases.bases])
        self.is_orthonormal = np.all([x.is_orthonormal for x in bases.bases])
Beispiel #2
0
    def _prepare(self, fun):
        '''
        Prepare the principal component analysis.

        Parameters
        ----------
        fun : function
            A function of d variables.

        Raises
        ------
        ValueError
            If the attribute projection_type is wrong.

        Returns
        -------
        tensap.FunctionalTensorPrincipalComponentAnalysis
            A FunctionalTensorPrincipalComponentAnalysis object with updated
            attributes.
        function
            A function used by the methods of
            tensap.TensorPrincipalComponentAnalysis.
        numpy.ndarray
            The cardinals of the functional bases.
        tensap.TensorPrincipalComponentAnalysis
            A tensap.TensorPrincipalComponentAnalysis used to perform the
            principal component analysis.

        '''
        solver = deepcopy(self)
        solver.bases = tensap.FunctionalBases(self.bases)

        # Create the tensor product grid
        if solver.projection_type == 'interpolation':
            if solver.grid is None:
                solver.grid = solver.bases.interpolation_points()
            else:
                solver.grid = solver.bases.interpolation_points(solver.grid)
        else:
            raise ValueError('Wrong projection_type attribute.')
        solver.grid = tensap.FullTensorGrid(solver.grid)

        # Create the function which provides the values of the function on the
        # grid
        def t_fun(i):
            return fun(solver.grid.eval_at_indices(i))

        shape = solver.bases.cardinals()

        # Create a TensorPrincipalComponentAnalysis with the same values of
        # properties as the FunctionalPrincipalComponentAnalysis
        tpca = tensap.TensorPrincipalComponentAnalysis()
        tpca.display = solver.display
        tpca.pca_sampling_factor = solver.pca_sampling_factor
        tpca.pca_adaptive_sampling = solver.pca_adaptive_sampling
        tpca.tol = solver.tol
        tpca.max_rank = solver.max_rank

        return solver, t_fun, shape, tpca
Beispiel #3
0
    def tensorized_function_functional_bases(self, h=1):
        '''
        Return a tensap.FunctionalBases object associated with the provided
        basis or basis function(s) and the Tensorizer object.

        Parameters
        ----------
        h : tensap.FunctionalBases or tensap.FunctionalBasis or function or
        list or scalar, optional
            The function used to generate the basis. The default is the
            function 1.

        Returns
        -------
        tensap.FunctionalBases
            The functional bases.

        '''
        #if isinstance(h, (np.ndarray, list)) or np.isscalar(h):
        #    h = lambda y, h=h: h*np.ones(np.shape(y))
        
        

        if hasattr(h, '__call__'):
            h = tensap.UserDefinedFunctionalBasis([h])
            h.measure = self.Y.random_variables[0]

        if isinstance(h, tensap.FunctionalBasis):
            h = tensap.FunctionalBases.duplicate(h, self.dim)
            
        if np.isscalar(h):
            h = [tensap.PolynomialFunctionalBasis(y.orthonormal_polynomials(),
                                      range(h+1)) for y in self.Y.random_variables]
            h = tensap.FunctionalBases(h)    

        assert isinstance(h, tensap.FunctionalBases), \
            'Wrong type of argument for h.'

        p = tensap.DiscretePolynomials(tensap.DiscreteRandomVariable(
            np.reshape(np.arange(self.b), [-1, 1])))
        p = tensap.PolynomialFunctionalBasis(p, np.arange(self.b))

        bases = [p]*self.d*self.dim + list(h.bases)
        return tensap.FunctionalBases(bases)
Beispiel #4
0
    def __init__(self, bases):
        tensap.FunctionalBasis.__init__(self)

        if isinstance(bases, list):
            bases = tensap.FunctionalBases(bases)

        assert isinstance(bases, tensap.FunctionalBases), \
            'The first argument must be a FunctionalBases.'

        self.bases = bases
        self.measure = tensap.ProductMeasure([x.measure for x in bases.bases])
        self.is_orthonormal = np.all([x.is_orthonormal for x in bases.bases])
    X = tensap.RandomVector(tensap.NormalRandomVariable(), ORDER)

    def fun(x):
        return 1 / (10 + x[:, 0] + 0.5 * x[:, 1])**2
elif CHOICE == 2:
    fun, X = tensap.multivariate_functions_benchmark('borehole')
    ORDER = X.size

# %% Approximation basis
DEGREE = 8
BASES = [
    tensap.PolynomialFunctionalBasis(X_TRAIN.orthonormal_polynomials(),
                                     range(DEGREE + 1))
    for X_TRAIN in X.random_variables
]
BASES = tensap.FunctionalBases(BASES)

# %% Training and test samples
NUM_TRAIN = 1000
X_TRAIN = X.random(NUM_TRAIN)
Y_TRAIN = fun(X_TRAIN)

NUM_TEST = 10000
X_TEST = X.random(NUM_TEST)
Y_TEST = fun(X_TEST)

# %% Learning in canonical tensor format
SOLVER = tensap.CanonicalTensorLearning(ORDER, tensap.SquareLossFunction())
SOLVER.rank_adaptation = True
SOLVER.initialization_type = 'mean'
SOLVER.tolerance['on_error'] = 1e-6
Beispiel #6
0
T = tensap.Tensorizer(B, D, DIM)
X = T.X
Y = T.Y
T.ordering_type = 1
FUN = tensap.UserDefinedFunction('1/(1+x0+x1)', DIM)
FUN.evaluation_at_multiple_points = True
TENSORIZED_FUN = T.tensorize(FUN)

DEGREE = 1
P = [
    tensap.PolynomialFunctionalBasis(y.orthonormal_polynomials(),
                                     range(DEGREE + 1))
    for y in Y.random_variables
]
BASES = T.tensorized_function_functional_bases(tensap.FunctionalBases(P))

H = tensap.FullTensorProductFunctionalBasis(BASES)
GRIDS, _ = BASES.magic_points(BASES.measure.random(100))
G = tensap.FullTensorGrid(GRIDS)

FUN_INTERP, _ = H.tensor_product_interpolation(TENSORIZED_FUN, G)
TR = tensap.Truncator()
TR.tolerance = 1e-9
FUN_INTERP.tensor = TR.ttsvd(FUN_INTERP.tensor)
TENSORIZED_FUN_INTERP = tensap.TensorizedFunction(FUN_INTERP, T)
print('Representation ranks = %s' % FUN_INTERP.tensor.representation_rank)

X_TEST = X.random(1000)
F_X_TEST = TENSORIZED_FUN_INTERP(X_TEST)
Y_TEST = FUN(X_TEST)