Documentation for project/models/mnist.py
¤
Source Code Documentation
The source codedocumentation is generated from Python docstrings using MkDocs
and mkdocstrings
.
Classes:
Name | Description |
---|---|
MNISTLitModule | Example of a |
MNISTLitModule ¤
MNISTLitModule(optimizer: Optimizer, scheduler: _LRScheduler, input_size: int = 784, lin1_size: int = 256, lin2_size: int = 256, lin3_size: int = 256, output_size: int = 10, compile: bool = True)
Bases: LightningModule
Example of a LightningModule
for MNIST classification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer | Optimizer | The optimizer to use. | required |
scheduler | _LRScheduler | The learning rate scheduler to use. | required |
input_size | int | The size of the input layer. Defaults to 784. | 784 |
lin1_size | int | The size of the first linear layer. Defaults to 256. | 256 |
lin2_size | int | The size of the second linear layer. Defaults to 256. | 256 |
lin3_size | int | The size of the third linear layer. Defaults to 256. | 256 |
output_size | int | The size of the output layer. Defaults to 10. | 10 |
compile | bool | Whether to compile the module or not. Defaults to True. | True |
Returns:
Type | Description |
---|---|
None | None |
Methods:
Name | Description |
---|---|
configure_optimizers | Configure optimizers and learning-rate schedulers. |
forward | Performs a forward pass through the model |
model_step | Performs a single model step on a batch of data. |
on_test_epoch_end | Lightning hook that is called when a test epoch ends. |
on_train_epoch_end | Lightning hook that is called when a training epoch ends. |
on_train_start | Lightning hook that is called when training begins. |
on_validation_epoch_end | Lightning hook that is called when a validation epoch ends. |
setup | Called at the beginning of fit (train + validate), validate, test, or predict. |
test_step | Performs a single test step on a batch of data from the test set. |
training_step | Perform a single training step on a batch of data from the training set. |
validation_step | Perform a single validation step on a batch of data from the validation set. |
Source code in src/project/models/mnist.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
configure_optimizers ¤
configure_optimizers() -> Dict[str, Any]
Configure optimizers and learning-rate schedulers.
Returns:
Type | Description |
---|---|
Dict[str, Any] | A dict containing the configured optimizers and learning-rate schedulers to be used for training. |
Source code in src/project/models/mnist.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
forward ¤
forward(x: Tensor) -> Tensor
Performs a forward pass through the model self.net
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Tensor | A tensor of images. | required |
Returns:
Type | Description |
---|---|
Tensor | A tensor of logits. |
Source code in src/project/models/mnist.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
model_step ¤
model_step(batch: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tensor, Tensor]
Performs a single model step on a batch of data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch | Tuple[Tensor, Tensor] | A batch of data (a tuple) containing the input tensor of images and target labels. | required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor, Tensor] | A tuple containing (in order): - A tensor of losses. - A tensor of predictions. - A tensor of target labels. |
Source code in src/project/models/mnist.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
on_test_epoch_end ¤
on_test_epoch_end() -> None
Lightning hook that is called when a test epoch ends.
Source code in src/project/models/mnist.py
198 199 200 |
|
on_train_epoch_end ¤
on_train_epoch_end() -> None
Lightning hook that is called when a training epoch ends.
Source code in src/project/models/mnist.py
147 148 149 |
|
on_train_start ¤
on_train_start() -> None
Lightning hook that is called when training begins.
Source code in src/project/models/mnist.py
90 91 92 93 94 95 96 |
|
on_validation_epoch_end ¤
on_validation_epoch_end() -> None
Lightning hook that is called when a validation epoch ends.
Source code in src/project/models/mnist.py
168 169 170 171 172 173 174 175 176 |
|
setup ¤
setup(stage: str) -> None
Called at the beginning of fit (train + validate), validate, test, or predict.
This is a good place to build models dynamically or adjust something about them. This hook is called on every process when using DDP.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stage | str | One of "fit", "validate", "test", or "predict". | required |
Source code in src/project/models/mnist.py
202 203 204 205 206 207 208 209 210 211 212 |
|
test_step ¤
test_step(batch: Tuple[Tensor, Tensor], batch_idx: int) -> None
Performs a single test step on a batch of data from the test set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch | Tuple[Tensor, Tensor] | A batch of data containing the input tensor of images and target labels. | required |
batch_idx | int | The index of the current batch. | required |
Source code in src/project/models/mnist.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
training_step ¤
training_step(batch: Tuple[Tensor, Tensor], batch_idx: int) -> Tensor
Perform a single training step on a batch of data from the training set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch | Tuple[Tensor, Tensor] | A batch of data containing the input tensor of images and target labels. | required |
batch_idx | int | The index of the current batch. | required |
Returns:
Type | Description |
---|---|
Tensor | torch.Tensor: A tensor of losses between model predictions and targets. |
Source code in src/project/models/mnist.py
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 |
|
validation_step ¤
validation_step(batch: Tuple[Tensor, Tensor], batch_idx: int) -> None
Perform a single validation step on a batch of data from the validation set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch | Tuple[Tensor, Tensor] | A tuple containing the input tensor of images and target labels. | required |
batch_idx | int | The index of the current batch. | required |
Source code in src/project/models/mnist.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|