utorch.utils
utorch.utils
count_nodes
Traverse the computational DAG and count leaf and interim nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Tensor
|
The output node from which to trace the graph backwards. |
required |
Returns:
| Type | Description |
|---|---|
GraphStats
|
GraphStats containing leaf count, interim count, edge count, and op breakdown. |
Example
x = Tensor(2.0, name="x") y = Tensor(3.0, name="y") z = x * y + 1.0 stats = count_nodes(z) stats.leaves 3 # x, y, and the constant 1.0 stats.interim 2 # the '*' node and the '+' node
Source code in utorch/utils/graph_stats.py
draw_graph
Render a computational graph with a tree-inspired aesthetic.
- Leaf nodes (inputs, constants, parameters) are rendered in green.
- Interim nodes (operations, intermediate results) are rendered in brown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Tensor
|
The output node (root of the evaluation) to trace backwards. |
required |
format
|
str
|
Output rendering format ('svg', 'png', 'pdf'). |
'png'
|
rankdir
|
str
|
Graph layout direction ('LR' = left-to-right, 'TB' = top-to-bottom). |
'LR'
|
Returns:
| Type | Description |
|---|---|
Any
|
graphviz.Digraph object ready for notebook rendering or file saving. |
Source code in utorch/utils/vizualization.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
show_drawing_canvas
Render an interactive 28x28 drawing canvas.
Source code in utorch/utils/notebook_utils.py
data
DataLoader
Data loader combining a dataset and sampler, yielding batched Tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
Dataset from which to load data. |
required |
batch_size
|
int
|
How many samples per batch to load (default: 1). |
1
|
shuffle
|
bool
|
If True, reshuffles indices at the start of every epoch. |
False
|
drop_last
|
bool
|
If True, drops the last incomplete batch. |
False
|
Initialize a DataLoader.
Source code in utorch/utils/data.py
__iter__
Return an iterator over the samples in the dataset.
Source code in utorch/utils/data.py
__len__
Return the total number of samples in the dataset.
Dataset
An abstract class representing a dataset.
All custom datasets must override __getitem__ (returning a sample at an
index) and __len__ (returning the total size of the dataset).
__getitem__
TensorDataset
Bases: Dataset
Dataset wrapping tensors where indexing retrieves slices along axis 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*tensors
|
Any
|
Tensors or NumPy arrays with identical length along axis 0. |
()
|
Initialize a TensorDataset.
Source code in utorch/utils/data.py
__getitem__
graph_stats
GraphStats
dataclass
Summary statistics for a computational graph.
Attributes:
| Name | Type | Description |
|---|---|---|
leaves |
int
|
Number of input/leaf nodes (parameters, inputs, constants). |
interim |
int
|
Number of operation/intermediate nodes. |
total |
int
|
Total unique nodes in the graph (leaves + interim). |
edges |
int
|
Total dependency connections (edges) between nodes. |
ops |
dict[str, int]
|
Breakdown of operations by frequency (e.g. {'add': 2, 'mul': 1}). |
__repr__
Return a string representation of the Graph stat for inspection.
Source code in utorch/utils/graph_stats.py
count_nodes
Traverse the computational DAG and count leaf and interim nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Tensor
|
The output node from which to trace the graph backwards. |
required |
Returns:
| Type | Description |
|---|---|
GraphStats
|
GraphStats containing leaf count, interim count, edge count, and op breakdown. |
Example
x = Tensor(2.0, name="x") y = Tensor(3.0, name="y") z = x * y + 1.0 stats = count_nodes(z) stats.leaves 3 # x, y, and the constant 1.0 stats.interim 2 # the '*' node and the '+' node
Source code in utorch/utils/graph_stats.py
notebook_utils
Interactive 28x28 drawing canvas for VS Code, JupyterLab, and Colab.
show_drawing_canvas
Render an interactive 28x28 drawing canvas.
Source code in utorch/utils/notebook_utils.py
vizualization
Visualization utilities for computational graphs.
draw_graph
Render a computational graph with a tree-inspired aesthetic.
- Leaf nodes (inputs, constants, parameters) are rendered in green.
- Interim nodes (operations, intermediate results) are rendered in brown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Tensor
|
The output node (root of the evaluation) to trace backwards. |
required |
format
|
str
|
Output rendering format ('svg', 'png', 'pdf'). |
'png'
|
rankdir
|
str
|
Graph layout direction ('LR' = left-to-right, 'TB' = top-to-bottom). |
'LR'
|
Returns:
| Type | Description |
|---|---|
Any
|
graphviz.Digraph object ready for notebook rendering or file saving. |
Source code in utorch/utils/vizualization.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |