Models¶
Models are used in machine learning to make predictions or decisions without being explicitly programmed to do so.
- class NeuralNetPy.models.Model¶
Bases:
pybind11_object
Base class for all models
- static load_from_file(arg0: str, arg1: NeuralNet::Network) None ¶
This function will load the parameters of a
Model
from a binary file.import NeuralNetPy as NNP # Initialize an empty network network = NNP.models.Network() # Populate it with previously saved parameters NNP.models.Model.load_from_file("network.bin", network)
- static save_to_file(arg0: str, arg1: NeuralNet::Network) None ¶
This function will save the given
Model
’s parameters in a binary file.import NeuralNetPy as NNP network = NNP.models.Network() network.setup(optimizer=NNP.optimizers.SGD(0.01)) network.addLayer(NNP.layers.Dense(3, NNP.ACTIVATION.RELU, NNP.WEIGHT_INIT.HE)) network.addLayer(NNP.layers.Dense(2, NNP.ACTIVATION.SIGMOID, NNP.WEIGHT_INIT.HE)) # ... after training NNP.models.Model.save_to_file(network, "network.bin")
Warning
The file content will be overwritten if it already exists.
- class NeuralNetPy.models.Network¶
Bases:
Model
This is the base of a Neural Network. You can setup the network with the given optimizer and loss function.
- Parameters:
optimizer (Optimizer) – The optimizer to be used from the
optimizers
moduleloss (LOSS) – The loss function to be used from the
LOSS
enum, defaults toQUADRATIC
import NeuralNetPy as NNP network = NNP.models.Network() network.setup(optimizer=NNP.SGD(0.01), loss=NNP.LOSS.MCQ)
- addLayer(self: NeuralNetPy.models.Network, arg0: NeuralNetPy.layers.Layer) None ¶
Add a layer to the network.
- Parameters:
layer (Layer) – The layer to be added
import NeuralNetPy as NNP network = NNP.models.Network() network.addLayer(NNP.layers.Dense(3, NNP.ACTIVATION.RELU, NNP.WEIGHT_INIT.HE))
Warning
The order of the layers added is important, it will reflect the overall structure of the network.
Danger
Under no circumstances you should add a
Flatten
layer as a hidden layer.
- getLayer(self: NeuralNetPy.models.Network, arg0: int) NeuralNetPy.layers.Layer ¶
Get a layer from the network by it’s index. They’re 0-indexed.
- Parameters:
index (int) – The index of the layer
- Returns:
The layer at the given index
- Return type:
Layer
import NeuralNetPy as NNP network = NNP.models.Network() network.addLayer(NNP.layers.Dense(3, NNP.ACTIVATION.RELU, NNP.WEIGHT_INIT.HE)) network.addLayer(NNP.layers.Dense(2, NNP.ACTIVATION.SIGMOID, NNP.WEIGHT_INIT.HE)) layer = network.getLayer(1) # Return Dense layer with 2 neurons
- getNumLayers(self: NeuralNetPy.models.Network) int ¶
Return the number of layers in the network.
- getSlug(self: NeuralNetPy.models.Network) str ¶
- predict(*args, **kwargs)¶
Overloaded function.
predict(self: NeuralNetPy.models.Network, arg0: List[List[float]]) -> numpy.ndarray[numpy.float64[m, n]]
Feed forward the given inputs through the network and return the predictions/outputs.
- param inputs:
A list of vectors representing the inputs
- type inputs:
list[list[float]]
- return:
A matrix representing the outputs of the network for the given inputs
- rtype:
numpy.ndarray
predict(self: NeuralNetPy.models.Network, arg0: List[List[List[float]]]) -> numpy.ndarray[numpy.float64[m, n]]
Feed forward the given inputs through the network and return the predictions/outputs.
- param inputs:
A list of vectors representing the inputs
- type inputs:
list[list[list[float]]]
- return:
A matrix representing the outputs of the network for the given inputs
- rtype:
numpy.ndarray
- setup(self: NeuralNetPy.models.Network, optimizer: NeuralNetPy.optimizers.Optimizer, loss: NeuralNetPy.LOSS = <LOSS.QUADRATIC: 1>) None ¶
- train(*args, **kwargs)¶
Overloaded function.
train(self: NeuralNetPy.models.Network, inputs: List[List[float]], targets: List[float], epochs: int, callbacks: List[NeuralNetPy.callbacks.Callback] = [], progBar: bool = True) -> float
Train the network by passing it 2 dimensional inputs (vectors).
- param inputs:
A list of vectors representing the inputs
- type inputs:
list[list[float]]
- param labels:
A list of labels
- type labels:
list[float]
- param epochs:
The number of epochs to train the network
- type epochs:
int
- param callbacks:
A list of callbacks to be used during the training
- type callbacks:
list[Callback]
- param progBar:
Whether or not to enable the progress bar
- type progBar:
bool
- return:
The average loss throughout the training
- rtype:
float
import NeuralNetPy as NNP network = NNP.models.Network() network.setup(optimizer=NNP.optimizers.SGD(0.01), loss=NNP.LOSS.MCQ) network.addLayer(NNP.layers.Dense(3, NNP.ACTIVATION.RELU, NNP.WEIGHT_INIT.HE)) network.addLayer(NNP.layers.Dense(2, NNP.ACTIVATION.SIGMOID, NNP.WEIGHT_INIT.HE)) inputs = [ [0.4, 0.5, 0.67], [0.3, 0.2, 0.1], [0.1, 0.2, 0.3] ] labels = [1, 0, 1] loss = network.train(inputs, labels, 10)
train(self: NeuralNetPy.models.Network, inputs: List[List[List[float]]], targets: List[float], epochs: int, callbacks: List[NeuralNetPy.callbacks.Callback] = [], progBar: bool = True) -> float
Train the network by passing it a list of 3 dimensional inputs (matrices).
- param inputs:
A list of matrices representing the inputs
- type inputs:
list[list[list[float]]]
- param labels:
A list of labels
- type labels:
list[float]
- param epochs:
The number of epochs to train the network
- type epochs:
int
- param callbacks:
A list of callbacks to be used during the training
- type callbacks:
list[Callback]
- param progBar:
Whether or not to enable the progress bar
- type progBar:
bool
- return:
The average loss throughout the training
- rtype:
float
train(self: NeuralNetPy.models.Network, trainingData: NeuralNetPy.TrainingData2dI, epochs: int, callbacks: List[NeuralNetPy.callbacks.Callback] = [], progBar: bool = True) -> float
Train the network by passing it a
TrainingData2dI
object.- param trainingData:
A
TrainingData2dI
object- type trainingData:
TrainingData2dI
- param epochs:
The number of epochs to train the network
- type epochs:
int
- param callbacks:
A list of callbacks to be used during the training
- type callbacks:
list[Callback]
- param progBar:
Whether or not to enable the progress bar
- type progBar:
bool
- return:
The average loss throughout the training
- rtype:
float
train(self: NeuralNetPy.models.Network, trainingData: NeuralNetPy.TrainingData3dI, epochs: int, callbacks: List[NeuralNetPy.callbacks.Callback] = [], progBar: bool = True) -> float
Train the network by passing it a
TrainingData3dI
object.- param trainingData:
A
TrainingData3dI
object- type trainingData:
TrainingData3dI
- param epochs:
The number of epochs to train the network
- type epochs:
int
- param callbacks:
A list of callbacks to be used during the training
- type callbacks:
list[Callback]
- param progBar:
Whether or not to enable the progress bar
- type progBar:
bool
- return:
The average loss throughout the training
- rtype:
float