Skip to content

Latest commit

 

History

History
67 lines (58 loc) · 2.85 KB

File metadata and controls

67 lines (58 loc) · 2.85 KB

Source code for deepxde.data.function

from .data import Data from ..utils import run_if_any_none

[docs] class Function(Data): """Approximate a function via a network.

Args:
    geometry: The domain of the function. Instance of \`\`Geometry\`\`.
    function: The function to be approximated. A callable function takes a NumPy array as the input and returns the
        a NumPy array of corresponding function values.
    num\_train (int): The number of training points sampled inside the domain.
    num\_test (int). The number of points for testing.
    train\_distribution (string): The distribution to sample training points. One of the following: "uniform"
        (equispaced grid), "pseudo" (pseudorandom), "LHS" (Latin hypercube sampling), "Halton" (Halton sequence),
        "Hammersley" (Hammersley sequence), or "Sobol" (Sobol sequence).
    online (bool): If \`\`True\`\`, resample the pseudorandom training points every training step, otherwise, use the
        same training points.
"""

def \_\_init\_\_(
    self,
    geometry,
    function,
    num\_train,
    num\_test,
    train\_distribution\="uniform",
    online\=False,
):
    self.geom \= geometry
    self.func \= function
    self.num\_train \= num\_train
    self.num\_test \= num\_test
    self.dist\_train \= train\_distribution
    self.online \= online

    if online and train\_distribution != "pseudo":
        print("Warning: Online learning should use pseudorandom sampling.")
        self.dist\_train \= "pseudo"

    self.train\_x, self.train\_y \= None, None
    self.test\_x, self.test\_y \= None, None

[docs] def losses(self, targets, outputs, loss_fn, inputs, model, aux=None): return loss_fn(targets, outputs)

[docs] def train_next_batch(self, batch_size=None): if self.train_x is None or self.online: if self.dist_train == "uniform": self.train_x = self.geom.uniform_points(self.num_train, boundary=True) else: self.train_x = self.geom.random_points( self.num_train, random=self.dist_train ) self.train_y = self.func(self.train_x) return self.train_x, self.train_y

[docs] @run_if_any_none("test_x", "test_y") def test(self): self.test_x = self.geom.uniform_points(self.num_test, boundary=True) self.test_y = self.func(self.test_x) return self.test_x, self.test_y