Model
Sequential
Sequential neural network model.
Represents a neural network composed of a sequence of layers, where the output of each layer is used as the input to the next one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_layers
|
list
|
Ordered list of layers defining the model architecture. |
required |
loss
|
Loss
|
Loss object that specifies loss function. |
required |
optimizer
|
BaseOptimizer
|
Optimizer object that updates the layer parameters during training. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
layers |
list
|
Stored |
loss |
Loss
|
Stored |
optimizer |
BaseOptimizer
|
Stored |
Source code in src/nnfs/model.py
9 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 | |
forward(X_inputs)
Runs the forward pass for the entire model.
Each layer takes the input of the previous layer (the first layer takes the input data) and passes its output to the next layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_inputs
|
ndarray
|
Input data to the model, with shape=(M samples, N features). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Predictions produced by the model. |
Source code in src/nnfs/model.py
backward(d_loss)
Runs the backward pass for the entire model.
Each layer takes the gradient of the next layer (the last layer takes it from the loss function) and passes its own gradient to the previous layer.
The gradient that the next layer represent dL / d_input_data (to the next layer)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_loss
|
ndarray
|
Gradient provided by the next layer. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
dL / d_input_data (to the current layer), which is passed to the previous layer. |
Source code in src/nnfs/model.py
update_weights()
Update all layer weights and biases using stored gradients.
The update mechanism depends on the optimizer.
Source code in src/nnfs/model.py
run_training_epoch(X_inputs, y_true)
Runs a training epoch, optimizing the model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_inputs
|
ndarray
|
Input data to the model, with shape=(M samples, N features) |
required |
y_true
|
ndarray
|
Ground-truth values for the prediction task. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The computed loss value. |
Source code in src/nnfs/model.py
fit(X_inputs, y_outputs, N_epochs, batch_size=-1, X_test_inputs=None, y_test_outputs=None, debug_flag=True)
Runs the training loop for the model.
In this loop, the forward and backward passes are computed to produce optimal gradients, and then the model parameters are updated according to those gradients. This loop runs for the specified number of epochs.
Each returned training metric is an array of shape=(N_epochs, N_batches).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_inputs
|
ndarray
|
Input data to the model, with shape=(M samples, N features). |
required |
y_outputs
|
ndarray
|
Ground-truth values for the prediction task. |
required |
N_epochs
|
int
|
Number of epochs to run during training. |
required |
batch_size
|
int
|
Number of samples per batch. By default, the entire dataset is used. |
-1
|
X_test_inputs
|
None | ndarray
|
Input data for validation. |
None
|
y_test_outputs
|
None | ndarray
|
Ground-truth values for validation. |
None
|
debug_flag
|
bool
|
Flag which specifies whether to output debugging logs. |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing training metrics (such as |
Source code in src/nnfs/model.py
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 | |
summary()
Prints a text summary of the model architecture.