Model¶
-
class
Model(layers: Sequence[bluebird.layers.layer.Layer])¶ Bases:
objectBase model class.
All other models inherit from it.
Example:
class CustomModel(Model): def __init__(self, layers: Sequence[Layer]): super.__init__() def build(self, iterator: DataIterator = BatchIterator(), loss: Loss = MSE(), optimizer: Optimizer = SGD()) -> None: super.build() def step(self, batch) -> float: ... return self.loss.loss(predicted, batch.targets)
-
__init__(layers: Sequence[bluebird.layers.layer.Layer]) → None¶ Initalizes the object.
- Parameters
layers (Sequence[Layer]) – sequence of layers of the newtwork
Methods
__init__(layers)Initalizes the object.
backward(grad)Backward propagates through the network.
build([loss, optimizer])Used to build the model.
fit(loader, num_epochs)Used to train the model.
forward(inputs)Forward propagates through the network.
Returns reversed layers.
Returns parameters and gradients for each layer.
load(path)Load the model from a file.
predict(inputs)Used to predict values after you finished the training.
save([path, filename])Save the model to a file.
step(batch)Step function is called during each training step.
summary(shape)Prints the information about the model (Layes, shapes, params).
-
backward(grad: numpy.ndarray) → numpy.ndarray¶ Backward propagates through the network.
- Parameters
inputs (
Tensor) – Gradient that you get from the loss function- Returns
Gradient from the first layer
- Return type
Tensor
-
build(loss: bluebird.loss.loss.Loss = <bluebird.loss.mse.MSE object>, optimizer: bluebird.optimizers.optimizer.Optimizer = <bluebird.optimizers.sgd.SGD object>) → None¶ Used to build the model.
Defines aditional parameters and initializes the weights.
- Parameters
loss (
Loss, otpional) – defines loss function Defaults to MSE()optimizer (
Optimizer, optional) – defines how the weights should be updated Defaults to SGD()
-
fit(loader: bluebird.dataloader.dataloaderbase.DataLoaderBase, num_epochs: int) → None¶ Used to train the model.
- Parameters
loader (
DataLoaderBase) – data loader type object used to load data for trainingnum_epochs (int) – number of epochs you want to train
-
forward(inputs: numpy.ndarray) → numpy.ndarray¶ Forward propagates through the network.
- Parameters
inputs (
Tensor) – Network input- Returns
Network output
- Return type
Tensor
-
get_layers() → Iterator[bluebird.layers.layer.Layer]¶ Returns reversed layers.
- Returns
List of layer objects
- Return type
Iterator[
Tensor]
-
get_params_and_grads() → Iterator[numpy.ndarray]¶ Returns parameters and gradients for each layer.
- Returns
List of parameter, gradient pairs
- Return type
Iterator[
Tensor]
-
load(path: str) → None¶ Load the model from a file.
- Parameters
path (
str) – path to the file where the model is saved
-
predict(inputs: numpy.ndarray) → numpy.ndarray¶ Used to predict values after you finished the training.
- Parameters
inputs (
Tensor) – values you wish to predict- Returns
Predicted values
- Return type
Tensor
-
save(path: str = '.', filename: str = 'model') → None¶ Save the model to a file.
It saves every layer, and everything within params object.
If you want your custom layer to be saved make sure, that everything you want to save is in params.
Everything is saved as a json object as follows:
{ 'layerName': [ { name: 'paramName', value: '0.5888 2.59874.......' }, .... ] }
- Parameters
path (
str, optional) – location where you want your model to be saved to, defaults to ‘.’ which means this directoryname (
str, optional) – name of the file, defaults to ‘model’, note: extension is added manually
-
step(batch: bluebird.data.Batch) → float¶ Step function is called during each training step.
It calculates the loss and updates weights and biases.
You must return loss.
- Parameters
batch (
Batch) – recives batch object, each batch object has inputs and targets- Raises
NotImplementedError –
-
summary(shape: tuple) → None¶ Prints the information about the model (Layes, shapes, params).
- Parameters
shape (tuple) – shape of the input vector
-