Skip to content

Load Method

Functions

load(cls, filename)

Load a Sequential model from a file.

Parameters:

Name Type Description Default
filename str

Path to the file containing the saved model.

required

Returns:

Name Type Description
Sequential

A new Sequential model instance loaded from the file.

Example
loaded_model = Sequential.load('my_model.json')
Source code in microkeras/models/sequential/load.py
def load(cls, filename):
    """
    Load a Sequential model from a file.

    Args:
        filename (str): Path to the file containing the saved model.

    Returns:
        Sequential: A new Sequential model instance loaded from the file.

    Example:
        ```python
        loaded_model = Sequential.load('my_model.json')
        ```
    """
    from microkeras.layers import Dense

    with open(filename, 'r') as f:
        model_data = json.load(f)

    new_model = cls([])

    for layer_data in model_data['layers']:
        layer = Dense(
            units=layer_data['units'],
            activation=layer_data['activation'],
            input_shape=layer_data['input_shape']
        )
        layer.W = np.array(layer_data['weights'])
        layer.b = np.array(layer_data['biases'])
        new_model.add(layer)

    return new_model