Skip to content

Simulated Minimum Distance¤

Source code in blackbirds/infer/smd.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class SMD:
    def __init__(self, loss, optimizer, gradient_horizon=None, progress_bar=False):
        """
        Simulated Minimum Distance. Finds the point in parameter space that
        minimizes the distance between the model's output and the observed
        data given the loss function `loss_fn`.

        **Arguments:**

        - `loss` : A callable that returns a (differentiable) loss. Needs to take (parameters, data) as input and return a scalar tensor.
        - `optimizer`: A PyTorch optimizer (eg Adam)
        - `gradient_horizon`: The number of steps to look ahead when computing the gradient. If None, defaults to the number of parameters.
        - `progress_bar`: Whether to display a progress bar.
        """
        self.loss_fn = loss
        self.optimizer = optimizer
        self.gradient_horizon = gradient_horizon
        self.progress_bar = progress_bar
        self.loss = []

    def run(
        self,
        data,
        n_epochs=1000,
        max_epochs_without_improvement=100,
        parameters_save_dir="best_parameters.pt",
    ):
        """
        Runs the SMD algorithm for `n_epochs` epochs.

        **Arguments:**

        - `data`: The observed data.
        - `n_epochs`: The number of epochs to run.
        - `max_epochs_without_improvement`: The number of epochs to run without improvement before stopping.
        - `parameters_save_dir`: The directory to save the best parameters to.
        """
        best_loss = np.inf
        epochs_without_improvement = 0
        if self.progress_bar:
            iterator = tqdm(range(n_epochs))
        else:
            iterator = range(n_epochs)
        for _ in iterator:
            self.optimizer.zero_grad()
            parameters = self.optimizer.param_groups[0]["params"][0]
            loss = self.loss_fn(parameters, data)
            loss.backward()
            if loss < best_loss:
                best_loss = loss.item()
                epochs_without_improvement = 0
                torch.save(parameters, parameters_save_dir)
            else:
                epochs_without_improvement += 1
            if self.progress_bar:
                iterator.set_postfix(
                    {
                        "loss": loss.item(),
                        "best loss": best_loss,
                        "epochs since improv.": epochs_without_improvement,
                    }
                )

            if epochs_without_improvement >= max_epochs_without_improvement:
                break
            self.optimizer.step()
            self.loss.append(loss.item())

__init__(loss, optimizer, gradient_horizon=None, progress_bar=False) ¤

Simulated Minimum Distance. Finds the point in parameter space that minimizes the distance between the model's output and the observed data given the loss function loss_fn.

Arguments:

  • loss : A callable that returns a (differentiable) loss. Needs to take (parameters, data) as input and return a scalar tensor.
  • optimizer: A PyTorch optimizer (eg Adam)
  • gradient_horizon: The number of steps to look ahead when computing the gradient. If None, defaults to the number of parameters.
  • progress_bar: Whether to display a progress bar.
Source code in blackbirds/infer/smd.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def __init__(self, loss, optimizer, gradient_horizon=None, progress_bar=False):
    """
    Simulated Minimum Distance. Finds the point in parameter space that
    minimizes the distance between the model's output and the observed
    data given the loss function `loss_fn`.

    **Arguments:**

    - `loss` : A callable that returns a (differentiable) loss. Needs to take (parameters, data) as input and return a scalar tensor.
    - `optimizer`: A PyTorch optimizer (eg Adam)
    - `gradient_horizon`: The number of steps to look ahead when computing the gradient. If None, defaults to the number of parameters.
    - `progress_bar`: Whether to display a progress bar.
    """
    self.loss_fn = loss
    self.optimizer = optimizer
    self.gradient_horizon = gradient_horizon
    self.progress_bar = progress_bar
    self.loss = []

run(data, n_epochs=1000, max_epochs_without_improvement=100, parameters_save_dir='best_parameters.pt') ¤

Runs the SMD algorithm for n_epochs epochs.

Arguments:

  • data: The observed data.
  • n_epochs: The number of epochs to run.
  • max_epochs_without_improvement: The number of epochs to run without improvement before stopping.
  • parameters_save_dir: The directory to save the best parameters to.
Source code in blackbirds/infer/smd.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def run(
    self,
    data,
    n_epochs=1000,
    max_epochs_without_improvement=100,
    parameters_save_dir="best_parameters.pt",
):
    """
    Runs the SMD algorithm for `n_epochs` epochs.

    **Arguments:**

    - `data`: The observed data.
    - `n_epochs`: The number of epochs to run.
    - `max_epochs_without_improvement`: The number of epochs to run without improvement before stopping.
    - `parameters_save_dir`: The directory to save the best parameters to.
    """
    best_loss = np.inf
    epochs_without_improvement = 0
    if self.progress_bar:
        iterator = tqdm(range(n_epochs))
    else:
        iterator = range(n_epochs)
    for _ in iterator:
        self.optimizer.zero_grad()
        parameters = self.optimizer.param_groups[0]["params"][0]
        loss = self.loss_fn(parameters, data)
        loss.backward()
        if loss < best_loss:
            best_loss = loss.item()
            epochs_without_improvement = 0
            torch.save(parameters, parameters_save_dir)
        else:
            epochs_without_improvement += 1
        if self.progress_bar:
            iterator.set_postfix(
                {
                    "loss": loss.item(),
                    "best loss": best_loss,
                    "epochs since improv.": epochs_without_improvement,
                }
            )

        if epochs_without_improvement >= max_epochs_without_improvement:
            break
        self.optimizer.step()
        self.loss.append(loss.item())