๐งฐ Utils๏
Utilities for Torchmate Library
ProgressBar๏
- class torchmate.utils.ProgressBar(total: int, bar_length: int = 30, fill: str = '=', prefix: str = '')[source]๏
Bases:
objectA progress bar for tracking the progress of iterative tasks.
Displays a progress bar with percentage completion, elapsed time, estimated time remaining, and optional verbose text.
Example usage:
no_iter = 100 pb = ProgressBar(total=no_iter, prefix="Processing", bar_length=50) for i in range(no_iter): pb.update(i + 1, message=f"Processing item {i + 1}")
Initialize the instance of ProgressBar class.
- Parameters:
total (int) โ Total number of iterations. Defaults to None.
bar_length (int, optional) โ Length of the progress bar. Defaults to
30.fill (str, optional) โ Character used to fill the progress bar. Defaults to
'='.prefix (str, optional) โ A text to display before the progress bar. Defaults to empty string
"".
- update(iteration: int, message: str = '') None[source]๏
Update the progress bar with current iteration and optional message text.
- Parameters:
iteration (int) โ Current iteration number.
message (str, optional) โ Optional text to display. Defaults to empty string.
- static format_time(duration_seconds: float) str[source]๏
Format a duration in seconds into a human-readable string.
- Parameters:
duration_seconds (float) โ Duration in seconds.
- Returns:
Formatted time string (e.g., โ1m 23sโ, โ45.67sโ, โ2h 35mโ).
- Return type:
str
Example usage:
formatted_time = ProgressBar.format_time(65.5) # Returns "1:05"
RunningAverage๏
HistoryPlotter๏
- class torchmate.utils.HistoryPlotter(trainer=None, history=None)[source]๏
Bases:
objectA class for plotting training and validation metrics, including loss, learning rate, and any additional metrics provided by the training history.
- Parameters:
trainer (Trainer, optional) โ An instance of
torchmate.trainer.Trainercontaining the training history to be plotted.history (Dict, optional) โ A dictionary containing the training history data, with keys such as
loss,lr, and other metric names.
- Raises:
ValueError โ If both
trainerandhistoryare None.
Notes
If both
trainerandhistoryare provided, the training history fromtrainerwill take precedence.Example usage:
# Using a Trainer object: trainer = Trainer(...) history = trainer.fit() plotter = Plotter(trainer=trainer) plotter.plot_all() # Using a history dictionary returned from trainer.fit() trainer = Trainer(...) history = trainer.fit() plotter = Plotter(history=history) plotter.plot_all() # Using a history dictionary: history = {"loss": [...], "lr": [...], "some_metric": [...]} plotter = Plotter(history=history) plotter.plot_all()
COLOR๏
- class torchmate.utils.COLOR[source]๏
Bases:
objectBG = Background
- Reference:
https://web.archive.org/web/20201214113226/http://ascii-table.com/ansi-escape-sequences.php
- BLACK = '\x1b[30m'๏
- RED = '\x1b[31m'๏
- GREEN = '\x1b[32m'๏
- YELLOW = '\x1b[33m'๏
- BLUE = '\x1b[34m'๏
- MAGENTA = '\x1b[35m'๏
- CYAN = '\x1b[36m'๏
- WHITE = '\x1b[37m'๏
- PURPLE = '\x1b[95m'๏
- BOLD = '\x1b[1m'๏
- UNDERLINE = '\x1b[4m'๏
- INVERSE = '\x1b[7m'๏
- END = '\x1b[0m'๏
- DEFAULT_BG = '\x1b[41m'๏
- BLACK_BG = '\x1b[40m'๏
- RED_BG = '\x1b[41m'๏
- GREEN_BG = '\x1b[42m'๏
- YELLOW_BG = '\x1b[43m'๏
- BLUE_BG = '\x1b[44m'๏
- MAGENTA_BG = '\x1b[45m'๏
- CYAN_BG = '\x1b[46m'๏
- WHITE_BG = '\x1b[47m'๏
colorize_text๏
- torchmate.utils.colorize_text(text: str, fore_tuple: Tuple[int, int, int] = (0, 0, 0), back_tuple: Tuple[int, int, int] | None = None, bold_text: bool = False) str[source]๏
Return text formatted with specified foreground color, background color, and optional bold formatting.
- Parameters:
text (str) โ The text to be colorized.
fore_tuple (tuple, optional) โ A tuple of RGB values (0-255) for the foreground color. Defaults to
(0, 0, 0).back_tuple (tuple, optional) โ A tuple of RGB values (0-255) for the background color. Defaults to
(255, 255, 255).bold_text (bool, optional) โ Whether to apply bold formatting to the text. Defaults to
False.
- Raises:
ValueError โ If RGB values in fore_tuple or back_tuple are outside the valid range of 0-255.
- Returns:
The formatted text with ANSI color codes embedded.
- Return type:
Str
model_info๏
- torchmate.utils.model_info(model: torch.nn.Module)[source]๏
Analyze a PyTorch model and returns a dictionary containing information about its size and parameters.
- Parameters:
model (torch.nn.Module) โ The PyTorch model to be analyzed.
- Returns:
- A dictionary containing the following information:
total_params (int): Total number of parameters in the model. trainable_params (int): Number of parameters that require gradient calculation during training. non_trainable_params (int): Number of parameters that do not require gradient calculation during training. total_size_mb (float): Total size of the model in megabytes. param_size_mb (float): Size of the model parameters in megabytes (excluding buffers). buffer_size_mb (float): Size of the model buffers in megabytes.
- Return type:
dict
- Prints:
This function also prints human-readable information about the model size and parameters including total, trainable, and non-trainable parameters for better understanding.