Skip to content

A small machine learning framework with ONLY python and math

License

Notifications You must be signed in to change notification settings

marcos-venicius/smlf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SMLF

Small Machine Learning Framework

This is a small framework to machine learning models.

We have a class Matrix and a class NeuralNetwork.

Examples

Xor

view the file

cd examples

python3 xor.py

Output example:

Screenshot from 2023-12-12 11-59-20

Docs

Matrix docs

If you wanna print the Matrix:

m = Matrix(2, 2)

print(m)

If you wanna randomize the Matrix:

rand(self) -> None

If you wanna fill the Matrix with just one value:

fill(self, x: float) -> None

If you wanna initialize with specific values:

m1 = Matrix(2, 2, [[0, 1], [0, 1]]) # <-- use the multidimensional array as the third parameter

If you wanna set a specific value to a specific location:

set(self, row: int, col: int, x: float) -> None

If you wanna sum the Matrix with another:

m1 = Matrix(2, 2)
m1.rand()

m2 = Matrix(2, 2)
m2.rand()

m3 = m1.sum(m2) # <-- do this

If you wanna apply the sigmoid activation function:

apply_sigmoid(self) -> None

If you wanna get a row:

row(row: int) -> Matrix

If you wanna get a value a specifc row and column:

at(r: int, c: int) -> float

If you wanna multiply the Matrix by another:

m1 = Matrix(2, 2)
m2 = Matrix(2, 2)

m1.rand()
m2.rand()

m3 = m1.dot(m2) # <-- do this

NeuralNetwork docs

The 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 this

If you wanna get the NeuralNetwork input:

input(self) -> Matrix

If you wanna get the NeuralNetwork output:

output(self) -> Matrix

If you wanna set the input mannually:

set_input(self, input: Matrix) -> None

If you wanna randomize the Neural Network (between 0 and 1):

rand(self) -> None

If you wanna forward:

forward(self) -> None

If you wanna calculate the cost:

cost(self, train_input: Matrix, train_output: Matrix) -> float

If you wanna do a finite diff manually (it's made internally):

finite_diff(self, g: NeuralNetwork, epsilon: float, train_input: Matrix, train_output: Matrix) -> None

If you wanna make the model learn manually (it's made internally):

learn(self, g: NeuralNetwork, learning_rate: float) -> None

If you wanna train the model:

train(self, g: NeuralNetwork, learning_rate: float, epsilon: float, train_input: Matrix, train_output: Matrix, epochs: int) -> None