lanfactory
lanfactory.config
network_configs
This Module defines simple examples for network and training configurations that serve as inputs to the training classes in the package.
lanfactory.onnx
transform_onnx
transform_to_onnx
Transforms a TorchMLP model to ONNX format.
Arguments
network_config_file (str):
Path to the pickle file containing the network configuration.
state_dict_file (str):
Path to the file containing the state dictionary of the model.
input_shape (int):
The size of the input tensor for the model.
output_onnx_file (str):
Path to the output ONNX file.
transform_to_onnx
Transforms a TorchMLP model to ONNX format.
Arguments
network_config_file (str):
Path to the pickle file containing the network configuration.
state_dict_file (str):
Path to the file containing the state dictionary of the model.
input_shape (int):
The size of the input tensor for the model.
output_onnx_file (str):
Path to the output ONNX file.
lanfactory.trainers
DatasetTorch
DatasetTorch(file_ids, batch_size=32, label_lower_bound=None, label_upper_bound=None, features_key='data', label_key='labels', out_framework='torch')
Bases: Dataset
Dataset class for TorchMLP training.
Arguments
file_ids (list):
List of paths to the data files.
batch_size (int):
Batch size.
label_lower_bound (float):
Lower bound for the labels.
label_upper_bound (float):
Upper bound for the labels.
features_key (str):
Key for the features in the data files.
label_key (str):
Key for the labels in the data files.
out_framework (str):
Output framework.
LoadTorchMLP
Class to load TorchMLP models.
Arguments
model_file_path (str):
Path to the model file.
network_config (dict):
Network configuration.
input_dim (int):
Input dimension.
LoadTorchMLPInfer
Class to load TorchMLP models for inference. (This was originally useful directly for application in the HDDM toolbox).
Arguments
model_file_path (str):
Path to the model file.
network_config (dict):
Network configuration.
input_dim (int):
Input dimension.
predict_on_batch
Intended as function that computes trial wise log-likelihoods from a matrix input. To be used primarily through the HDDM toolbox.
Arguments
x (numpy.ndarray(dtype=numpy.float32)):
Matrix which will be passed through the network.
LANs expect the matrix columns to follow a specific order.
When used in HDDM, x will be passed as follows.
The first few columns are trial wise model parameters
(order specified in the model_config file under the 'params' key).
The last two columns are filled with trial wise
reaction times and choices.
When not used via HDDM, no such restriction applies.
Output
numpy.ndarray(dtype = numpy.float32):
Output of the network. When called through HDDM,
this is expected as trial-wise log likelihoods
of a given generative model.
MLPJax
Bases: Module
JaxMLP class.
Arguments
layer_sizes (Sequence[int]):
Sequence of integers containing the sizes of the layers.
activations (Sequence[str]):
Sequence of strings containing the activation functions.
train (bool):
Whether the model should be set to training mode or not.
train_output_type (str):
The output type of the model during training.
__call__
Call function for the JaxMLP class. Performs forward pass through the network.
Arguments
inputs (jax.numpy.ndarray):
Input tensor.
Returns:
-
jax.numpy.ndarray:–Output tensor.
load_state_from_file
Loads the state dictionary from a file.
Arguments
seed (int):
Seed for the random number generator.
input_dim (int):
Dimension of the input tensor.
file_path (str):
Path to the file containing the state dictionary.
Returns:
-
flax.core.frozen_dict.FrozenDict:–The state dictionary.
make_forward_partial
Creates a partial function for the forward pass of the network.
Arguments
seed (int):
Seed for the random number generator.
input_dim (int):
Dimension of the input tensor.
state (flax.core.frozen_dict.FrozenDict):
The state dictionary (if not loaded from file).
add_jitted (bool):
Whether the partial function should be jitted or not.
Returns:
-
Callable:–The partial function for the forward pass of the network.
MLPJaxFactory
Factory function to create a MLPJax object.
Arguments
network_config (dict):
Dictionary containing the network configuration.
train (bool):
Whether the model should be trained or not.
Returns:
-
MLPJax class initialized with the correct network configuration.–
ModelTrainerJaxMLP
ModelTrainerJaxMLP(train_config=None, model=None, train_dl=None, valid_dl=None, allow_abs_path_folder_generation=False, pin_memory=False, seed=None)
Arguments
train_config (dict):
Dictionary containing the training configuration.
model (MLPJax):
The MLPJax model to be trained.
train_dl (torch.utils.data.DataLoader):
The training data loader.
valid_dl (torch.utils.data.DataLoader):
The validation data loader.
allow_abs_path_folder_generation (bool):
Whether the folder for the output files should be created or not.
pin_memory (bool):
Whether the data loader should pin memory or not.
seed (int):
Seed for the random number generator.
Returns:
-
ModelTrainerJaxMLP:–The ModelTrainerJaxMLP object.
run_epoch
Run one epoch of training or validation
Arguments
state (flax.core.frozen_dict.FrozenDict):
The state dictionary.
train (bool):
Whether the model should is in training mode or not.
verbose (int):
The verbosity level.
epoch (int):
The current epoch.
max_epochs (int):
The maximum number of epochs.
Returns:
-
tuple (flax.core.frozen_dict.FrozenDict, float):–The state dictionary and the mean epoch loss.
train_and_evaluate
train_and_evaluate(output_folder='data/', output_file_id='fileid', run_id='runid', wandb_on=True, wandb_project_id='projectid', save_history=True, save_model=True, save_config=True, save_all=True, save_data_details=True, verbose=1)
Train and evaluate JAXMLP model.
Arguments
output_folder (str):
Path to the output folder.
output_file_id (str):
The file id.
run_id (str):
The run id.
wandb_on (bool):
Whether to use wandb or not.
wandb_project_id (str):
Project id for wandb.
save_history (bool):
Whether to save the training history or not.
save_model (bool):
Whether to save the model or not.
save_config (bool):
Whether to save the training configuration or not.
save_all (bool):
Whether to save all files or not.
save_data_details (bool):
Whether to save the data details or not.
verbose (int):
The verbosity level.
Returns:
-
flax.core.frozen_dict.FrozenDict:–The final state dictionary (model state).
ModelTrainerTorchMLP
ModelTrainerTorchMLP(train_config=None, model=None, train_dl=None, valid_dl=None, allow_abs_path_folder_generation=False, pin_memory=True, seed=None)
train_config (dict):
Training configuration.
model (TorchMLP):
TorchMLP model.
train_dl (DatasetTorch):
Training dataloader.
valid_dl (DatasetTorch):
Validation dataloader.
allow_abs_path_folder_generation (bool):
Whether to allow absolute path folder generation.
pin_memory (bool):
Whether to pin memory (dataloader). Can affect speed.
seed (int):
Random seed.
train_and_evaluate
train_and_evaluate(output_folder='data/', output_file_id='fileid', run_id='runid', wandb_on=True, wandb_project_id='projectid', save_history=True, save_model=True, save_config=True, save_onnx=True, save_all=True, save_data_details=True, verbose=1)
Train and evaluate the model.
Arguments
output_folder (str):
Output folder.
output_file_id (str):
Output file ID.
run_id (str):
Run ID.
wandb_on (bool):
Whether to use wandb.
wandb_project_id (str):
Wandb project ID.
save_history (bool):
Whether to save the training history.
save_model (bool):
Whether to save the model.
save_config (bool):
Whether to save the training configuration.
save_onnx (bool):
Whether to save the model to ONNX format.
save_all (bool):
Whether to save all.
save_data_details (bool):
Whether to save the data details.
verbose (int):
Verbosity level.
TorchMLP
jax_mlp
MLPJax
Bases: Module
JaxMLP class.
Arguments
layer_sizes (Sequence[int]):
Sequence of integers containing the sizes of the layers.
activations (Sequence[str]):
Sequence of strings containing the activation functions.
train (bool):
Whether the model should be set to training mode or not.
train_output_type (str):
The output type of the model during training.
__call__
Call function for the JaxMLP class. Performs forward pass through the network.
Arguments
inputs (jax.numpy.ndarray):
Input tensor.
Returns:
-
jax.numpy.ndarray:–Output tensor.
load_state_from_file
Loads the state dictionary from a file.
Arguments
seed (int):
Seed for the random number generator.
input_dim (int):
Dimension of the input tensor.
file_path (str):
Path to the file containing the state dictionary.
Returns:
-
flax.core.frozen_dict.FrozenDict:–The state dictionary.
make_forward_partial
Creates a partial function for the forward pass of the network.
Arguments
seed (int):
Seed for the random number generator.
input_dim (int):
Dimension of the input tensor.
state (flax.core.frozen_dict.FrozenDict):
The state dictionary (if not loaded from file).
add_jitted (bool):
Whether the partial function should be jitted or not.
Returns:
-
Callable:–The partial function for the forward pass of the network.
MLPJaxFactory
Factory function to create a MLPJax object.
Arguments
network_config (dict):
Dictionary containing the network configuration.
train (bool):
Whether the model should be trained or not.
Returns:
-
MLPJax class initialized with the correct network configuration.–
ModelTrainerJaxMLP
ModelTrainerJaxMLP(train_config=None, model=None, train_dl=None, valid_dl=None, allow_abs_path_folder_generation=False, pin_memory=False, seed=None)
Arguments
train_config (dict):
Dictionary containing the training configuration.
model (MLPJax):
The MLPJax model to be trained.
train_dl (torch.utils.data.DataLoader):
The training data loader.
valid_dl (torch.utils.data.DataLoader):
The validation data loader.
allow_abs_path_folder_generation (bool):
Whether the folder for the output files should be created or not.
pin_memory (bool):
Whether the data loader should pin memory or not.
seed (int):
Seed for the random number generator.
Returns:
-
ModelTrainerJaxMLP:–The ModelTrainerJaxMLP object.
run_epoch
Run one epoch of training or validation
Arguments
state (flax.core.frozen_dict.FrozenDict):
The state dictionary.
train (bool):
Whether the model should is in training mode or not.
verbose (int):
The verbosity level.
epoch (int):
The current epoch.
max_epochs (int):
The maximum number of epochs.
Returns:
-
tuple (flax.core.frozen_dict.FrozenDict, float):–The state dictionary and the mean epoch loss.
train_and_evaluate
train_and_evaluate(output_folder='data/', output_file_id='fileid', run_id='runid', wandb_on=True, wandb_project_id='projectid', save_history=True, save_model=True, save_config=True, save_all=True, save_data_details=True, verbose=1)
Train and evaluate JAXMLP model.
Arguments
output_folder (str):
Path to the output folder.
output_file_id (str):
The file id.
run_id (str):
The run id.
wandb_on (bool):
Whether to use wandb or not.
wandb_project_id (str):
Project id for wandb.
save_history (bool):
Whether to save the training history or not.
save_model (bool):
Whether to save the model or not.
save_config (bool):
Whether to save the training configuration or not.
save_all (bool):
Whether to save all files or not.
save_data_details (bool):
Whether to save the data details or not.
verbose (int):
The verbosity level.
Returns:
-
flax.core.frozen_dict.FrozenDict:–The final state dictionary (model state).
torch_mlp
DatasetTorch
DatasetTorch(file_ids, batch_size=32, label_lower_bound=None, label_upper_bound=None, features_key='data', label_key='labels', out_framework='torch')
Bases: Dataset
Dataset class for TorchMLP training.
Arguments
file_ids (list):
List of paths to the data files.
batch_size (int):
Batch size.
label_lower_bound (float):
Lower bound for the labels.
label_upper_bound (float):
Upper bound for the labels.
features_key (str):
Key for the features in the data files.
label_key (str):
Key for the labels in the data files.
out_framework (str):
Output framework.
LoadTorchMLP
Class to load TorchMLP models.
Arguments
model_file_path (str):
Path to the model file.
network_config (dict):
Network configuration.
input_dim (int):
Input dimension.
LoadTorchMLPInfer
Class to load TorchMLP models for inference. (This was originally useful directly for application in the HDDM toolbox).
Arguments
model_file_path (str):
Path to the model file.
network_config (dict):
Network configuration.
input_dim (int):
Input dimension.
predict_on_batch
Intended as function that computes trial wise log-likelihoods from a matrix input. To be used primarily through the HDDM toolbox.
Arguments
x (numpy.ndarray(dtype=numpy.float32)):
Matrix which will be passed through the network.
LANs expect the matrix columns to follow a specific order.
When used in HDDM, x will be passed as follows.
The first few columns are trial wise model parameters
(order specified in the model_config file under the 'params' key).
The last two columns are filled with trial wise
reaction times and choices.
When not used via HDDM, no such restriction applies.
Output
numpy.ndarray(dtype = numpy.float32):
Output of the network. When called through HDDM,
this is expected as trial-wise log likelihoods
of a given generative model.
ModelTrainerTorchMLP
ModelTrainerTorchMLP(train_config=None, model=None, train_dl=None, valid_dl=None, allow_abs_path_folder_generation=False, pin_memory=True, seed=None)
train_config (dict):
Training configuration.
model (TorchMLP):
TorchMLP model.
train_dl (DatasetTorch):
Training dataloader.
valid_dl (DatasetTorch):
Validation dataloader.
allow_abs_path_folder_generation (bool):
Whether to allow absolute path folder generation.
pin_memory (bool):
Whether to pin memory (dataloader). Can affect speed.
seed (int):
Random seed.
train_and_evaluate
train_and_evaluate(output_folder='data/', output_file_id='fileid', run_id='runid', wandb_on=True, wandb_project_id='projectid', save_history=True, save_model=True, save_config=True, save_onnx=True, save_all=True, save_data_details=True, verbose=1)
Train and evaluate the model.
Arguments
output_folder (str):
Output folder.
output_file_id (str):
Output file ID.
run_id (str):
Run ID.
wandb_on (bool):
Whether to use wandb.
wandb_project_id (str):
Wandb project ID.
save_history (bool):
Whether to save the training history.
save_model (bool):
Whether to save the model.
save_config (bool):
Whether to save the training configuration.
save_onnx (bool):
Whether to save the model to ONNX format.
save_all (bool):
Whether to save all.
save_data_details (bool):
Whether to save the data details.
verbose (int):
Verbosity level.
TorchMLP
lanfactory.utils
save_configs
save_configs(model_id=None, save_folder=None, network_config=None, train_config=None, allow_abs_path_folder_generation=True)
Function to save the network and training configurations to a folder.
Arguments
model_id (str):
The id of the model.
save_folder (str):
The folder to save the configurations to.
network_config (dict):
The network configuration dictionary.
train_config (dict):
The training configuration dictionary.
allow_abs_path_folder_generation (bool):
If True, the folder string is treated as an absolute path.
If False, the folder string is treated as a relative path.
try_gen_folder
Function to generate a folder from a string. If the folder already exists, it will not be generated.
Arguments
folder (str):
The folder string to generate.
allow_abs_path_folder_generation (bool):
If True, the folder string is treated as an absolute path.
If False, the folder string is treated as a relative path.
util_funs
save_configs
save_configs(model_id=None, save_folder=None, network_config=None, train_config=None, allow_abs_path_folder_generation=True)
Function to save the network and training configurations to a folder.
Arguments
model_id (str):
The id of the model.
save_folder (str):
The folder to save the configurations to.
network_config (dict):
The network configuration dictionary.
train_config (dict):
The training configuration dictionary.
allow_abs_path_folder_generation (bool):
If True, the folder string is treated as an absolute path.
If False, the folder string is treated as a relative path.
try_gen_folder
Function to generate a folder from a string. If the folder already exists, it will not be generated.
Arguments
folder (str):
The folder string to generate.
allow_abs_path_folder_generation (bool):
If True, the folder string is treated as an absolute path.
If False, the folder string is treated as a relative path.