netlightbasemapk
NetLightBaseMAPK
¶
Bases: LightningModule
A LightningModule class for a neural network model.
Attributes:
Name | Type | Description |
---|---|---|
l1 |
int
|
The number of neurons in the first hidden layer. |
epochs |
int
|
The number of epochs to train the model for. |
batch_size |
int
|
The batch size to use during training. |
initialization |
str
|
The initialization method to use for the weights. |
act_fn |
Module
|
The activation function to use in the hidden layers. |
optimizer |
str
|
The optimizer to use during training. |
dropout_prob |
float
|
The probability of dropping out a neuron during training. |
lr_mult |
float
|
The learning rate multiplier for the optimizer. |
patience |
int
|
The number of epochs to wait before early stopping. |
_L_in |
int
|
The number of input features. |
_L_out |
int
|
The number of output classes. |
layers |
Sequential
|
The neural network model. |
Examples:
>>> from torch.utils.data import DataLoader
>>> from torchvision.datasets import MNIST
>>> from torchvision.transforms import ToTensor
>>> train_data = MNIST(PATH_DATASETS,
train=True,
download=True,
transform=ToTensor())
>>> train_loader = DataLoader(train_data,
batch_size=BATCH_SIZE)
>>> net_light_base = NetLightBase(l1=128,
epochs=10,
batch_size=BATCH_SIZE,
initialization='xavier',
act_fn=nn.ReLU(),
optimizer='Adam',
dropout_prob=0.1,
lr_mult=0.1,
patience=5)
>>> trainer = L.Trainer(max_epochs=10)
>>> trainer.fit(net_light_base, train_loader)
Source code in spotpython/light/classification/netlightbasemapk.py
10 11 12 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 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
__init__(l1, epochs, batch_size, initialization, act_fn, optimizer, dropout_prob, lr_mult, patience, _L_in, _L_out, *args, **kwargs)
¶
Initializes the NetLightBase object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
l1 |
int
|
The number of neurons in the first hidden layer. |
required |
epochs |
int
|
The number of epochs to train the model for. |
required |
batch_size |
int
|
The batch size to use during training. |
required |
initialization |
str
|
The initialization method to use for the weights. |
required |
act_fn |
Module
|
The activation function to use in the hidden layers. |
required |
optimizer |
str
|
The optimizer to use during training. |
required |
dropout_prob |
float
|
The probability of dropping out a neuron during training. |
required |
lr_mult |
float
|
The learning rate multiplier for the optimizer. |
required |
patience |
int
|
The number of epochs to wait before early stopping. |
required |
_L_in |
int
|
The number of input features. Not a hyperparameter, but needed to create the network. |
required |
_L_out |
int
|
The number of output classes. Not a hyperparameter, but needed to create the network. |
required |
Returns:
Type | Description |
---|---|
NoneType
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If l1 is less than 4. |
Examples:
>>> from torch.utils.data import DataLoader
>>> from torchvision.datasets import MNIST
>>> from torchvision.transforms import ToTensor
>>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
>>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
>>> net_light_base = NetLightBase(l1=128, epochs=10, batch_size=BATCH_SIZE,
initialization='xavier', act_fn=nn.ReLU(),
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
patience=5)
>>> trainer = L.Trainer(max_epochs=10)
>>> trainer.fit(net_light_base, train_loader)
Source code in spotpython/light/classification/netlightbasemapk.py
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 |
|
configure_optimizers()
¶
Configures the optimizer for the model.
Returns:
Type | Description |
---|---|
Optimizer
|
torch.optim.Optimizer: The optimizer to use during training. |
Source code in spotpython/light/classification/netlightbasemapk.py
275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
forward(x)
¶
Performs a forward pass through the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
A tensor containing a batch of input data. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: A tensor containing the probabilities for each class. |
Examples:
>>> from torch.utils.data import DataLoader
>>> from torchvision.datasets import MNIST
>>> from torchvision.transforms import ToTensor
>>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
>>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
>>> net_light_base = NetLightBase(l1=128,
epochs=10,
batch_size=BATCH_SIZE,
initialization='xavier', act_fn=nn.ReLU(),
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
patience=5)
Source code in spotpython/light/classification/netlightbasemapk.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
test_step(batch, batch_idx, prog_bar=False)
¶
Performs a single test step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
tuple
|
A tuple containing a batch of input data and labels. |
required |
batch_idx |
int
|
The index of the current batch. |
required |
prog_bar |
bool
|
Whether to display the progress bar. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
A tuple containing the loss and accuracy for this batch. |
Source code in spotpython/light/classification/netlightbasemapk.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
training_step(batch)
¶
Performs a single training step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
tuple
|
A tuple containing a batch of input data and labels. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: A tensor containing the loss for this batch. |
Examples:
>>> from torch.utils.data import DataLoader
>>> from torchvision.datasets import MNIST
>>> from torchvision.transforms import ToTensor
>>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
>>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
>>> net_light_base = NetLightBase(l1=128,
epochs=10,
batch_size=BATCH_SIZE,
initialization='xavier', act_fn=nn.ReLU(),
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
patience=5)
>>> trainer = L.Trainer(max_epochs=10)
>>> trainer.fit(net_light_base, train_loader)
Source code in spotpython/light/classification/netlightbasemapk.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
validation_step(batch, batch_idx, prog_bar=False)
¶
Performs a single validation step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
tuple
|
A tuple containing a batch of input data and labels. |
required |
batch_idx |
int
|
The index of the current batch. |
required |
prog_bar |
bool
|
Whether to display the progress bar. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
NoneType
|
None |
Examples:
>>> from torch.utils.data import DataLoader
>>> from torchvision.datasets import MNIST
>>> from torchvision.transforms import ToTensor
>>> val_data = MNIST(PATH_DATASETS, train=False, download=True, transform=ToTensor())
>>> val_loader = DataLoader(val_data, batch_size=BATCH_SIZE)
>>> net_light_base = NetLightBase(l1=128,
epochs=10,
batch_size=BATCH_SIZE,
initialization='xavier', act_fn=nn.ReLU(),
optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
patience=5)
>>> trainer = L.Trainer(max_epochs=10)
>>> trainer.fit(net_light_base, val_loader)
Source code in spotpython/light/classification/netlightbasemapk.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|