mlgebra


Namemlgebra JSON
Version 1.0.0 PyPI version JSON
download
home_page
SummaryA machine learning tool for Python, in Python
upload_time2024-03-15 22:45:06
maintainer
docs_urlNone
authorAhmet Erdem
requires_python>=3.9
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MLgebra

A machine learning tool for Python, in Python.

## New code structure

I have prepared a new code structure for this library.
It is much more readable now. Layout is spread to 
different files which are imported as a chain. "Node" name
is now "Neuron". Layers are generated on a base class. I have
included only the Dense layers for now. Model imports layers
and neurons, and manages the usage of them.

Problems stated in the last commit is now solved.

## Project details

https://pypi.org/project/mlgebra/

[Github](https://github.com/ahmeterdem1/ml)

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

_pip install mlgebra_

This project is a continuation of [this](https://github.com/ahmeterdem1/MLgebra) 
repository. Which is also a continuation of _vectorgebra_ library. This particular
library therefore requires vectorgebra to be installed. Currently, the development
process continues and the code is probably unstable. 

This is a basic tool of machine learning that is created based on vectorgebra. All
internal operations are imported from vectorgebra. Specific algorithms for machine
learning are created based upon them. 

This library can be used in 2 different ways. These methods can be classified into 2:
Dynamic usage and Static usage.

Dynamic usage, is basically the machine learning library that this code aims to be. 
You can configure your models, load data, and train them. MLgebra also imports 
Vectorgebra, you can use all the tools there too to preprocess your data, etc. This
is what you would expect from a classic library. 

Static usage is inherently different from being a "library". Static usage turns this
library into a framework-like tool. 

Among libraries source files, there are some .spy files. This is short for "source python".
Those files are template mirrors of original libraries class files. If you chose to compile
your model with .compile(_destination_) method, those template files are filled according to your
configurations and a complete mirror of MLgebra library is generated at _destination_ folder. 

Working with this mirror is done by just importing it and using it as if it were MLgebra. 
You can edit the literal source mirror files as you wish. Despite the given flexibility 
to the user with this "Static" method, it is named static because it is still a
lot of lines of code which would take long considerations and trial-and-error cycles to
properly change and improve. 

You can share your static files as you wish and import other Python files etc., which is
one of the main points of this compilation method. Your model in Python, is also compiled
into Python.

### Notes

There is indeed no literal "compilation" like modern AI compilers do. Compilation operation
done here is just a template filling and loop unrolling. A different class is generated for
each layer and its neurons from the base classes Neuron and Layer. Neuron base class is absent
in original MLgebra. Attributes of objects may also differ from original MLgebra as compiled
class files do not require much of the information given in the original code. 

Deeper explanations for the Dynamic usage is given below.

<hr>

## Neuron

This is the basic class for "perceptron". Objects from this class are the building blocks
of layers. Weights connecting the perceptron to the previous layer, are stored in the
perceptron. ".next_weights" attribute is for updating the perceptron. Trained values
are stored in this attribute at each iteration. When ".update()" is called on the layer,
".next_weights" replaces ".weights".

## Layer

Layer is the base class for all types of layers that exists in the library, and that
will exist. Main skeleton for all subclasses are given here. Initializers takes many
arguments; input_shape, units, activation, bias, initialization, decimal, low, high,
bias_low, bias_high, cutoff, leak, template. 

### Dense

Basic dense layer for multilayer perceptron. Currently, the only general purpose
layer type in the library.

#### Initializer arguments

- input_shape: Dimension of the inputted vector to the layer.
- units: Dimension of the layer that will be generated.
- activation: Activation functions name; e.g. "relu", "sigmoid".
- bias: Bias generation method; e.g. "naive", "zero".
- initialization: Weight initialization method; e.g. "xavier", "he".
- decimal: Choice to use Decimal or not: boolean
- low: Parameter for weight generation
- high: Parameter for weight generation
- bias_low: Parameter for bias generation
- bias_high: Parameter for bias generation
- cutoff: Used for activation choices "relu" and "sigmoid". Processed internally.
- leak: Used for activation choice "relu". Processed internally.
- template: Internal boolean. Used by the library when a model is read from file.

#### startForward(input: Vector)

Start the forward pass process. Returns the resulting processed Vector. Input must
be preprocessed. 

#### forward(input: Vector)

Continues the forward pass. Returns the output Vector from the activation function.

#### startBackward(deltas, inputs, lr)

Starts the backpropagation process. "deltas" is the Vector of errors. "inputs" is the
previous inputs to this layer during the forward pass process. "lr" is the learning rate.
Must be a Decimal object if it is set to True in the model. Saves the recalculated weights
in Neurons' ".next_weights" and saves the recalculated biases in self.next_bias. 

#### backward(deltas, inputs, lr)

Continues the backpropagation process. Arguments are the same as .startBackward(). Applies
derivatives according to the activation function of the layer. Other functionalities are
the same as .startBackward().

#### .update()

Replaces weights and biases with the trained ones. Must be called after the backwards pass
ends completely. 

### Flatten

A preprocessing layer that flattens the matrix-tensor input into a Vector. 

#### Initializer arguments

- shape: The dimension of the required output Vector. 

#### startForward(), forward()

Flattens the input tensor into a Vector which is "shape" dimensional.


## Model

This is the class that collects neurons and layers together and governs them as to
train a "model". 

### Initializing arguments

- name: Name of the model. This must be different for each object, as file savings etc. are done according to this name.
- loss: Loss function choice. Only cross entropy loss is included in the library yet. You may leave this be.
- decimal: Main decimal choice of your model is done here. If set True, all other governed objects will have decimal=True.

### loadModel(path)

Loads a saved model file. You have to structure your model accordingly before loading the weights. 
Saved model files only store weights, biases and activation functions. "path" is a complete path
to your model file including its name.

### saveModel(path)

Saves your model to the specified directory. "path" is now a directory. File name is generated with your models
name. 

### addLayer(layer: Layer)

Add a layer to your model.

### structure(layers: List)

Give a complete list of layers of your model. A new layer list is generated from the "layers" argument.

### produce(input)

Produce an output with your model. "input" is what your model takes. It might be a Vector, or a Matrix
if you have a Flatten layer. Same for Tensor. Returns the output Vector of your model. 

### evaluate(x, y)

Returns the calculated absolute error of your model on given testing input set x and testing labels y.

### train(x, y, validate_x, validate_y, batch_size=1, lr=0.001)

Train your model on training set x and training labels y. "validate_x" and "validate_y" are optional. If
given, after each batch training, absolute error on validation set is logged to the screen. In the future,
updates utilizing validation sets in training will come. Batch size and learning rate is self-explanatory.

### describe()

Prints a basic description of your model to the screen.

### compile(destination)

Compiles your model into mirror-MLgebra files and saves them in a directory named with your models name into
the specified directory with "destination".

## Methods

### readMnist(path1, path2, path3, path4, decimal)

Read MNISt database with decimal choice. "path1" and "path2" are training set and training labels.
"path3" and "path4" are test set and labels. Returns a 4-Vector-tuple. 

## Exceptions

### FileStructureError

This exception is raised when files extension is wrong.

### ConfigError

This function is raised when any problem with the model configuration occurs at any point of the structuring.



            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "mlgebra",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "",
    "author": "Ahmet Erdem",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/38/24/10bb3c0c85efabb6fa65035514d3e71be82d9c47ee67a0251611af0bd67a/mlgebra-1.0.0.tar.gz",
    "platform": null,
    "description": "# MLgebra\n\nA machine learning tool for Python, in Python.\n\n## New code structure\n\nI have prepared a new code structure for this library.\nIt is much more readable now. Layout is spread to \ndifferent files which are imported as a chain. \"Node\" name\nis now \"Neuron\". Layers are generated on a base class. I have\nincluded only the Dense layers for now. Model imports layers\nand neurons, and manages the usage of them.\n\nProblems stated in the last commit is now solved.\n\n## Project details\n\nhttps://pypi.org/project/mlgebra/\n\n[Github](https://github.com/ahmeterdem1/ml)\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n_pip install mlgebra_\n\nThis project is a continuation of [this](https://github.com/ahmeterdem1/MLgebra) \nrepository. Which is also a continuation of _vectorgebra_ library. This particular\nlibrary therefore requires vectorgebra to be installed. Currently, the development\nprocess continues and the code is probably unstable. \n\nThis is a basic tool of machine learning that is created based on vectorgebra. All\ninternal operations are imported from vectorgebra. Specific algorithms for machine\nlearning are created based upon them. \n\nThis library can be used in 2 different ways. These methods can be classified into 2:\nDynamic usage and Static usage.\n\nDynamic usage, is basically the machine learning library that this code aims to be. \nYou can configure your models, load data, and train them. MLgebra also imports \nVectorgebra, you can use all the tools there too to preprocess your data, etc. This\nis what you would expect from a classic library. \n\nStatic usage is inherently different from being a \"library\". Static usage turns this\nlibrary into a framework-like tool. \n\nAmong libraries source files, there are some .spy files. This is short for \"source python\".\nThose files are template mirrors of original libraries class files. If you chose to compile\nyour model with .compile(_destination_) method, those template files are filled according to your\nconfigurations and a complete mirror of MLgebra library is generated at _destination_ folder. \n\nWorking with this mirror is done by just importing it and using it as if it were MLgebra. \nYou can edit the literal source mirror files as you wish. Despite the given flexibility \nto the user with this \"Static\" method, it is named static because it is still a\nlot of lines of code which would take long considerations and trial-and-error cycles to\nproperly change and improve. \n\nYou can share your static files as you wish and import other Python files etc., which is\none of the main points of this compilation method. Your model in Python, is also compiled\ninto Python.\n\n### Notes\n\nThere is indeed no literal \"compilation\" like modern AI compilers do. Compilation operation\ndone here is just a template filling and loop unrolling. A different class is generated for\neach layer and its neurons from the base classes Neuron and Layer. Neuron base class is absent\nin original MLgebra. Attributes of objects may also differ from original MLgebra as compiled\nclass files do not require much of the information given in the original code. \n\nDeeper explanations for the Dynamic usage is given below.\n\n<hr>\n\n## Neuron\n\nThis is the basic class for \"perceptron\". Objects from this class are the building blocks\nof layers. Weights connecting the perceptron to the previous layer, are stored in the\nperceptron. \".next_weights\" attribute is for updating the perceptron. Trained values\nare stored in this attribute at each iteration. When \".update()\" is called on the layer,\n\".next_weights\" replaces \".weights\".\n\n## Layer\n\nLayer is the base class for all types of layers that exists in the library, and that\nwill exist. Main skeleton for all subclasses are given here. Initializers takes many\narguments; input_shape, units, activation, bias, initialization, decimal, low, high,\nbias_low, bias_high, cutoff, leak, template. \n\n### Dense\n\nBasic dense layer for multilayer perceptron. Currently, the only general purpose\nlayer type in the library.\n\n#### Initializer arguments\n\n- input_shape: Dimension of the inputted vector to the layer.\n- units: Dimension of the layer that will be generated.\n- activation: Activation functions name; e.g. \"relu\", \"sigmoid\".\n- bias: Bias generation method; e.g. \"naive\", \"zero\".\n- initialization: Weight initialization method; e.g. \"xavier\", \"he\".\n- decimal: Choice to use Decimal or not: boolean\n- low: Parameter for weight generation\n- high: Parameter for weight generation\n- bias_low: Parameter for bias generation\n- bias_high: Parameter for bias generation\n- cutoff: Used for activation choices \"relu\" and \"sigmoid\". Processed internally.\n- leak: Used for activation choice \"relu\". Processed internally.\n- template: Internal boolean. Used by the library when a model is read from file.\n\n#### startForward(input: Vector)\n\nStart the forward pass process. Returns the resulting processed Vector. Input must\nbe preprocessed. \n\n#### forward(input: Vector)\n\nContinues the forward pass. Returns the output Vector from the activation function.\n\n#### startBackward(deltas, inputs, lr)\n\nStarts the backpropagation process. \"deltas\" is the Vector of errors. \"inputs\" is the\nprevious inputs to this layer during the forward pass process. \"lr\" is the learning rate.\nMust be a Decimal object if it is set to True in the model. Saves the recalculated weights\nin Neurons' \".next_weights\" and saves the recalculated biases in self.next_bias. \n\n#### backward(deltas, inputs, lr)\n\nContinues the backpropagation process. Arguments are the same as .startBackward(). Applies\nderivatives according to the activation function of the layer. Other functionalities are\nthe same as .startBackward().\n\n#### .update()\n\nReplaces weights and biases with the trained ones. Must be called after the backwards pass\nends completely. \n\n### Flatten\n\nA preprocessing layer that flattens the matrix-tensor input into a Vector. \n\n#### Initializer arguments\n\n- shape: The dimension of the required output Vector. \n\n#### startForward(), forward()\n\nFlattens the input tensor into a Vector which is \"shape\" dimensional.\n\n\n## Model\n\nThis is the class that collects neurons and layers together and governs them as to\ntrain a \"model\". \n\n### Initializing arguments\n\n- name: Name of the model. This must be different for each object, as file savings etc. are done according to this name.\n- loss: Loss function choice. Only cross entropy loss is included in the library yet. You may leave this be.\n- decimal: Main decimal choice of your model is done here. If set True, all other governed objects will have decimal=True.\n\n### loadModel(path)\n\nLoads a saved model file. You have to structure your model accordingly before loading the weights. \nSaved model files only store weights, biases and activation functions. \"path\" is a complete path\nto your model file including its name.\n\n### saveModel(path)\n\nSaves your model to the specified directory. \"path\" is now a directory. File name is generated with your models\nname. \n\n### addLayer(layer: Layer)\n\nAdd a layer to your model.\n\n### structure(layers: List)\n\nGive a complete list of layers of your model. A new layer list is generated from the \"layers\" argument.\n\n### produce(input)\n\nProduce an output with your model. \"input\" is what your model takes. It might be a Vector, or a Matrix\nif you have a Flatten layer. Same for Tensor. Returns the output Vector of your model. \n\n### evaluate(x, y)\n\nReturns the calculated absolute error of your model on given testing input set x and testing labels y.\n\n### train(x, y, validate_x, validate_y, batch_size=1, lr=0.001)\n\nTrain your model on training set x and training labels y. \"validate_x\" and \"validate_y\" are optional. If\ngiven, after each batch training, absolute error on validation set is logged to the screen. In the future,\nupdates utilizing validation sets in training will come. Batch size and learning rate is self-explanatory.\n\n### describe()\n\nPrints a basic description of your model to the screen.\n\n### compile(destination)\n\nCompiles your model into mirror-MLgebra files and saves them in a directory named with your models name into\nthe specified directory with \"destination\".\n\n## Methods\n\n### readMnist(path1, path2, path3, path4, decimal)\n\nRead MNISt database with decimal choice. \"path1\" and \"path2\" are training set and training labels.\n\"path3\" and \"path4\" are test set and labels. Returns a 4-Vector-tuple. \n\n## Exceptions\n\n### FileStructureError\n\nThis exception is raised when files extension is wrong.\n\n### ConfigError\n\nThis function is raised when any problem with the model configuration occurs at any point of the structuring.\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A machine learning tool for Python, in Python",
    "version": "1.0.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5593f450e18dab702f8e9bbe9ca35dcbb4606b509a03d93f5bf3f53637c9df28",
                "md5": "ff57b23fd9e0ba8022599e72de23085e",
                "sha256": "cdf838829208e15cf48deabe0a125e0691816c46f6be43253ff52cd5fa0bfc05"
            },
            "downloads": -1,
            "filename": "mlgebra-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ff57b23fd9e0ba8022599e72de23085e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 12885,
            "upload_time": "2024-03-15T22:45:02",
            "upload_time_iso_8601": "2024-03-15T22:45:02.889948Z",
            "url": "https://files.pythonhosted.org/packages/55/93/f450e18dab702f8e9bbe9ca35dcbb4606b509a03d93f5bf3f53637c9df28/mlgebra-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "382410bb3c0c85efabb6fa65035514d3e71be82d9c47ee67a0251611af0bd67a",
                "md5": "d5e5212b4c4ad5d026111a5b86d18293",
                "sha256": "f4d22a1194b0c1416715c1a5a72624a2d1a61668a97eab032c698e238329f411"
            },
            "downloads": -1,
            "filename": "mlgebra-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d5e5212b4c4ad5d026111a5b86d18293",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 14893,
            "upload_time": "2024-03-15T22:45:06",
            "upload_time_iso_8601": "2024-03-15T22:45:06.863603Z",
            "url": "https://files.pythonhosted.org/packages/38/24/10bb3c0c85efabb6fa65035514d3e71be82d9c47ee67a0251611af0bd67a/mlgebra-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-15 22:45:06",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "mlgebra"
}
        
Elapsed time: 0.29924s