Small Machine Learning Framework
This is a small framework to machine learning models.
We have a class Matrix and a class NeuralNetwork.
cd examples
python3 xor.pyOutput example:
If you wanna print the Matrix:
m = Matrix(2, 2)
print(m)If you wanna randomize the Matrix:
rand(self) -> NoneIf you wanna fill the Matrix with just one value:
fill(self, x: float) -> NoneIf you wanna initialize with specific values:
m1 = Matrix(2, 2, [[0, 1], [0, 1]]) # <-- use the multidimensional array as the third parameterIf you wanna set a specific value to a specific location:
set(self, row: int, col: int, x: float) -> NoneIf you wanna sum the Matrix with another:
m1 = Matrix(2, 2)
m1.rand()
m2 = Matrix(2, 2)
m2.rand()
m3 = m1.sum(m2) # <-- do thisIf you wanna apply the sigmoid activation function:
apply_sigmoid(self) -> NoneIf you wanna get a row:
row(row: int) -> MatrixIf you wanna get a value a specifc row and column:
at(r: int, c: int) -> floatIf you wanna multiply the Matrix by another:
m1 = Matrix(2, 2)
m2 = Matrix(2, 2)
m1.rand()
m2.rand()
m3 = m1.dot(m2) # <-- do thisThe neural network expects an architecture like [2, 2, 1], that means:
- 2 inputs
- 1 hidden layer
- 1 output
If you wanna print the NeuralNetwork:
nn = NeuralNetwork([2, 2, 1])
print(nn) # <-- do thisIf you wanna get the NeuralNetwork input:
input(self) -> MatrixIf you wanna get the NeuralNetwork output:
output(self) -> MatrixIf you wanna set the input mannually:
set_input(self, input: Matrix) -> NoneIf you wanna randomize the Neural Network (between 0 and 1):
rand(self) -> NoneIf you wanna forward:
forward(self) -> NoneIf you wanna calculate the cost:
cost(self, train_input: Matrix, train_output: Matrix) -> floatIf you wanna do a finite diff manually (it's made internally):
finite_diff(self, g: NeuralNetwork, epsilon: float, train_input: Matrix, train_output: Matrix) -> NoneIf you wanna make the model learn manually (it's made internally):
learn(self, g: NeuralNetwork, learning_rate: float) -> NoneIf you wanna train the model:
train(self, g: NeuralNetwork, learning_rate: float, epsilon: float, train_input: Matrix, train_output: Matrix, epochs: int) -> None