Skip to content

Variable saving and loading

Saving variables into a JSON file

Saves all module variables to a .variables.json file.

Parameters:

Name Type Description Default
filepath str | Path

str or pathlib.Path object. Path where to save the program. Must end in .variables.json.

required
overwrite bool

Whether we should overwrite any existing program at the target location, or instead ask the user via an interactive prompt.

True
Source code in synalinks/src/programs/program.py
def save_variables(self, filepath, overwrite=True):
    """Saves all module variables to a `.variables.json` file.

    Args:
        filepath (str | pathlib.Path): `str` or `pathlib.Path` object.
            Path where to save the program. Must end in `.variables.json`.
        overwrite (bool): Whether we should overwrite any existing program
            at the target location, or instead ask the user
            via an interactive prompt.
    """
    filepath = file_utils.path_to_string(filepath)
    if not filepath.endswith(".variables.json"):
        raise ValueError(
            "The filepath should ends with '.variables.json', "
            f"received filepath={filepath}"
        )
    config = self.get_state_tree()
    config_string = json.dumps(config, indent=2)
    if file_utils.exists(filepath) and not overwrite:
        io_utils.ask_to_proceed_with_overwrite(filepath)
    with open(filepath, "w") as f:
        f.write(config_string)

Saving variables into JSON dict

Retrieves tree-like structure of program variables.

This method allows retrieval of different program variables (trainable, non-trainable, optimizer, and metrics). The variables are returned in a nested dictionary format, where the keys correspond to the variable names and the values are the nested representations of the variables.

Example:

program.compile(
    optimizer=synalinks.optimizers.RandomFewShot(),
    reward=synalinks.rewards.ExactMatch(),
)
program.fit(x=x_train, y=y_train)
state_tree = program.get_state_tree()

Returns:

Type Description
dict

A dictionary containing the nested representations of the requested variables. The keys are the variable names, and the values are the corresponding nested dictionaries.

Source code in synalinks/src/programs/program.py
def get_state_tree(self):
    """Retrieves tree-like structure of program variables.

    This method allows retrieval of different program variables (trainable,
    non-trainable, optimizer, and metrics). The variables are returned in a
    nested dictionary format, where the keys correspond to the variable
    names and the values are the nested representations of the variables.

    Example:

    ```python
    program.compile(
        optimizer=synalinks.optimizers.RandomFewShot(),
        reward=synalinks.rewards.ExactMatch(),
    )
    program.fit(x=x_train, y=y_train)
    state_tree = program.get_state_tree()
    ```

    Returns:
        (dict): A dictionary containing the nested representations of the
            requested variables. The keys are the variable names, and the
            values are the corresponding nested dictionaries.
    """
    variables = {}
    variables["trainable_variables"] = self._create_nested_dict(
        self.trainable_variables
    )
    variables["non_trainable_variables"] = self._create_nested_dict(
        self.non_trainable_variables
    )
    if self.optimizer:
        variables["optimizer_variables"] = self._create_nested_dict(
            self.optimizer.variables
        )
    variables["metrics_variables"] = self._create_nested_dict(self.metrics_variables)
    return variables

Loading variables from a JSON file

Load all module variables from a .variable.json file.

Parameters:

Name Type Description Default
filepath str | Path

str or pathlib.Path object. Path to load the program's variables from. Must end in .variables.json.

required
Source code in synalinks/src/programs/program.py
def load_variables(self, filepath):
    """Load all module variables from a `.variable.json` file.

    Args:
        filepath (str | pathlib.Path): `str` or `pathlib.Path` object.
            Path to load the program's variables from.
            Must end in `.variables.json`.
    """
    filepath = file_utils.path_to_string(filepath)
    if not filepath.endswith(".variables.json"):
        raise ValueError(
            "The filepath should ends with '.variables.json', "
            f"received filepath={filepath}"
        )
    with open(filepath, "r") as f:
        state_tree_config = f.read()
    self.set_state_tree(state_tree_config)

Load variables from a JSON dict

Assigns values to variables of the program.

This method takes a dictionary of nested variable values, which represents the state tree of the program, and assigns them to the corresponding variables of the program. The dictionary keys represent the variable names (e.g., 'trainable_variables', 'optimizer_variables'), and the values are nested dictionaries containing the variable paths and their corresponding values.

Parameters:

Name Type Description Default
state_tree dict

A dictionary representing the state tree of the program. The keys are the variable names, and the values are nested dictionaries representing the variable paths and their values.

required
Source code in synalinks/src/programs/program.py
def set_state_tree(self, state_tree):
    """Assigns values to variables of the program.

    This method takes a dictionary of nested variable values, which
    represents the state tree of the program, and assigns them to the
    corresponding variables of the program. The dictionary keys represent the
    variable names (e.g., `'trainable_variables'`, `'optimizer_variables'`),
    and the values are nested dictionaries containing the variable
    paths and their corresponding values.

    Args:
        state_tree (dict): A dictionary representing the state tree of the program.
            The keys are the variable names, and the values are nested
            dictionaries representing the variable paths and their values.
    """
    for k, v in state_tree.items():
        path_value_dict = self._flatten_nested_dict(v)
        if k == "trainable_variables":
            self._assign_variable_values(self.trainable_variables, path_value_dict)
        elif k == "non_trainable_variables":
            self._assign_variable_values(
                self.non_trainable_variables, path_value_dict
            )
        elif k == "optimizer_variables":
            self._assign_variable_values(self.optimizer.variables, path_value_dict)
        elif k == "metrics_variables":
            self._assign_variable_values(self.metrics_variables, path_value_dict)
        else:
            raise ValueError(f"Unknown variable name: {k}")