utils (module)
Utility helpers exposed by gradnet.utils. This page lists all public
functions automatically from the module, so newly added public functions
appear in the docs without manual edits.
- gradnet.utils.random_seed(seed)[source]
Set random seed for reproducibility. Works with torch, numpy, and random.
- gradnet.utils.prune_edges(del_adj, *, threshold=None, target_edge_number=None, renorm=True)[source]
Prune edges in an adjacency-like tensor.
Use either a numeric
threshold(keeps entries withabs(x) >= threshold) or specifytarget_edge_numberto automatically determine a threshold that yields exactly that many unpruned entries. Exactly one of these must be provided.If
renormis True, rescales the pruned tensor to match the original L1 norm. If all entries are pruned, returns an all-zero tensor.
- gradnet.utils.to_networkx(gn, pruning_threshold=1e-08)[source]
Export the current adjacency to a NetworkX graph.
Edges with absolute weight below
pruning_thresholdare dropped. Supports both dense and sparse internal representations.- Parameters:
pruning_threshold (float) – Minimum absolute weight to keep an edge.
- Returns:
A
networkx.DiGraphifdirectedelse aGraph.- Return type:
networkx.Graph | networkx.DiGraph
- gradnet.utils.plot_adjacency_heatmap(gn, *, ax=None, title=None, xlabel='$j$', ylabel='$i$', cbar_label='$A_{ij}$', add_colorbar=True, cbar_kwargs=None, imshow_kwargs=None, plt_show=False)[source]
Plot an adjacency matrix as a heatmap.
If
axisNone, creates a new figure and axes.The colorbar attaches to
ax.figureunlessadd_colorbar=False.Accepts a GradNet-like object (callable with no args), a Torch tensor, or any array-like representing an adjacency.
- gradnet.utils.plot_graph(gn, *, ax=None, pruning_threshold=1e-08, layout='spring', node_size=15.0, edgecolors='black', edge_width_scaling=1.0, draw_kwargs=None, add_colorbar=False, colorbar_label=None, plt_show=False)[source]
Draw the NetworkX representation of
gn.If
axisNone, creates a new figure and axes.Uses
to_networkxand derives edge widths from weights.layoutcan be anetworkx.draw_*name or a callable.If add_colorbar=True, adds a colorbar when node_color is array-like.
- gradnet.utils.load_scalars(log_dir)[source]
Return shared steps and a dict of scalar series from Lightning logs.
The
log_dircan be either a specific version directory (e.g.,lightning_logs/gradnet/version_3) or the parent folder that contains multipleversion_*subdirectories (e.g.,lightning_logs/gradnet). This function prefers CSV logs when present and falls back to TensorBoard event files if available.Returns
(steps, series)wherestepsis a single list of integers (epoch/step) shared by all metrics, andseriesis a mapping{name: values}with values aligned tosteps. Missing values are filled withnan.- Usage:
>>> steps, series = load_scalars('lightning_logs/gradnet') >>> loss = series['loss']
- Parameters:
log_dir – Path to a logger directory or its parent.
- Returns:
tuple[list[int], dict[str, list[float]]]
- gradnet.utils.animate_adjacency(checkpoints, *, output_path=None, fps=30, dpi=100, figsize=None, title_template='Checkpoint {index}: {name}', imshow_kwargs=None)[source]
Animate adjacency heatmaps for GradNet checkpoints named
gn-periodic-*.ckpt.