# Neural Network Using Python and Numpy
NeuralNetPy is a neural network library created using numpy that allows you to create, train, and save deep learning models. The library is inspired by PyTorch but simplified for educational purposes.
It includes common layers, activations, losses, and optimizers, making it easy to build a variety of models for different tasks. It also includes helpful utility functions for data preprocessing, training and testing the models, and saving and loading models.
While this library is not meant to replace existing deep learning frameworks, it can be a great tool for educational purposes or for small sized projects without using much external libraries other than Numpy and raw Python. It is also a good starting point for those interested in deep learning to learn about the fundamentals of building neural networks.
## The following components have been implemented:
Layers
* Dense
* Conv2D
* Flatten
* MaxPooling2D
Activations
* ReLU
* GeLU
* Sigmoid
* Softmax
Loss functions
* MSE
* CategoricalCrossentropy
Optimizers
* SGD
* Adam
Utils
This library has a Dataset class implemented which allows you to iterate over batches of data and other utlities useful for deep learning
## Install
```
pip install NeuralNetPy
```
PyPi: https://pypi.org/project/NeuralNetPy/
## Usage
Creating a Linear Model
Here is an example of how you can create a simple linear model:
```python
import NeuralNetPy as net
class LinearModel(net.utils.BaseModel):
def __init__(self):
super().__init__()
self.layers = [
net.layers.Flatten(),
net.layers.Dense(28 * 28, 128),
net.activations.GeLU(),
net.layers.Dense(128, 10),
net.activations.Softmax()
]
model = LinearModel()
```
The model consists of a Flatten layer to convert the input to a 1D array, a Dense layer with 128 units and a GeLU activation function, another Dense layer with 10 units and a Softmax activation function.
## Loading Data
You can load your own data using the np.load() method, and then pass the data into the Dataset class provided by the library. Here is an example using random data:
```python
import numpy as np
X_train = np.random.rand(1000, 784)
y_train = np.random.rand(1000, 10)
train = net.utils.Dataset(X_train, y_train, batch_size=32, shuffle=True)
```
## Training Loop
To train the model, you can use a for loop and iterate over the batches of data in the train dataset. Here is an example:
```python
loss_fn = net.losses.CategoricalCrossentropy()
optim = net.optimizers.Adam(model.layers, lr=0.001)
epochs = 10
for epoch in range(epochs):
running_loss_train = 0
running_acc_train = 0
for i, (batch_X, batch_y) in enumerate(train):
y_pred = model.forward(batch_X)
loss = loss_fn.forward(y_pred=y_pred, y_true=batch_y).mean()
acc = net.utils.acc_fn(y_pred=y_pred, y_true=batch_y)
grad = loss_fn.backward(y_pred=y_pred, y_true=batch_y)
model.backward(grad)
optim.step()
running_loss_train += loss
running_acc_train += acc
running_loss_train /= len(train)
running_acc_train /= len(train)
print(f"Epoch: {epoch+1} | Loss: {running_loss_train} | Acc: {running_acc_train}")
```
This will train the model for 10 epochs, iterating over the batches of data and updating the model parameters using the Adam optimizer.
## Saving and Loading Models
To save a model, you can use the save method provided by the model. Here is an example:
```python
model.save('linear_model_1')
```
This will save the model parameters to a .npz file with the specified name.
To load a saved model, you can create a new instance of MyModel and then call the load method with the path to the saved model file:
```python
model = LinearModel()
model.load('linear_model_1.npz')
```
Raw data
{
"_id": null,
"home_page": "",
"name": "NeuralNetPy",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "Neural Network,Python,AI,Numpy,Deep Learning",
"author": "Joel Maldonado-Ruiz",
"author_email": "jmaldonadoruiz0@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/75/b0/1335453bfab1a84e4fe9dec14aabf1bb511adc91e3c2ffd91abfcc475be1/NeuralNetPy-0.0.5.tar.gz",
"platform": null,
"description": "# Neural Network Using Python and Numpy\n\nNeuralNetPy is a neural network library created using numpy that allows you to create, train, and save deep learning models. The library is inspired by PyTorch but simplified for educational purposes. \n\nIt includes common layers, activations, losses, and optimizers, making it easy to build a variety of models for different tasks. It also includes helpful utility functions for data preprocessing, training and testing the models, and saving and loading models.\n\nWhile this library is not meant to replace existing deep learning frameworks, it can be a great tool for educational purposes or for small sized projects without using much external libraries other than Numpy and raw Python. It is also a good starting point for those interested in deep learning to learn about the fundamentals of building neural networks.\n\n## The following components have been implemented:\n\nLayers\n\n* Dense\n* Conv2D\n* Flatten\n* MaxPooling2D\n\nActivations\n\n* ReLU\n* GeLU\n* Sigmoid\n* Softmax\n\nLoss functions\n\n* MSE\n* CategoricalCrossentropy\n\nOptimizers\n\n* SGD\n* Adam\n\nUtils\n\nThis library has a Dataset class implemented which allows you to iterate over batches of data and other utlities useful for deep learning\n\n## Install\n```\npip install NeuralNetPy\n```\n\nPyPi: https://pypi.org/project/NeuralNetPy/\n\n## Usage\n\nCreating a Linear Model\n\nHere is an example of how you can create a simple linear model:\n\n```python\n\nimport NeuralNetPy as net\n\nclass LinearModel(net.utils.BaseModel):\n def __init__(self):\n super().__init__()\n \n self.layers = [\n net.layers.Flatten(),\n net.layers.Dense(28 * 28, 128),\n net.activations.GeLU(),\n net.layers.Dense(128, 10),\n net.activations.Softmax()\n ]\n\nmodel = LinearModel()\n\n```\n\nThe model consists of a Flatten layer to convert the input to a 1D array, a Dense layer with 128 units and a GeLU activation function, another Dense layer with 10 units and a Softmax activation function.\n\n## Loading Data\n\nYou can load your own data using the np.load() method, and then pass the data into the Dataset class provided by the library. Here is an example using random data:\n\n\n```python\n\nimport numpy as np\n\nX_train = np.random.rand(1000, 784)\ny_train = np.random.rand(1000, 10)\n\ntrain = net.utils.Dataset(X_train, y_train, batch_size=32, shuffle=True)\n```\n\n## Training Loop\n\nTo train the model, you can use a for loop and iterate over the batches of data in the train dataset. Here is an example:\n\n```python\n\nloss_fn = net.losses.CategoricalCrossentropy()\noptim = net.optimizers.Adam(model.layers, lr=0.001)\n\nepochs = 10\nfor epoch in range(epochs):\n running_loss_train = 0\n running_acc_train = 0\n\n for i, (batch_X, batch_y) in enumerate(train):\n y_pred = model.forward(batch_X)\n\n loss = loss_fn.forward(y_pred=y_pred, y_true=batch_y).mean()\n acc = net.utils.acc_fn(y_pred=y_pred, y_true=batch_y)\n\n grad = loss_fn.backward(y_pred=y_pred, y_true=batch_y)\n model.backward(grad)\n\n optim.step()\n\n running_loss_train += loss\n running_acc_train += acc\n\n running_loss_train /= len(train)\n running_acc_train /= len(train)\n\n print(f\"Epoch: {epoch+1} | Loss: {running_loss_train} | Acc: {running_acc_train}\")\n```\n\nThis will train the model for 10 epochs, iterating over the batches of data and updating the model parameters using the Adam optimizer.\n\n\n## Saving and Loading Models\n\nTo save a model, you can use the save method provided by the model. Here is an example:\n\n```python\nmodel.save('linear_model_1')\n```\nThis will save the model parameters to a .npz file with the specified name.\n\nTo load a saved model, you can create a new instance of MyModel and then call the load method with the path to the saved model file:\n\n```python\n\nmodel = LinearModel()\nmodel.load('linear_model_1.npz')\n\n```\n\n\n",
"bugtrack_url": null,
"license": "GNU General Public License v3.0",
"summary": "NeuralNetPy is a neural network library created using numpy that allows you to create, train, and save deep learning models.",
"version": "0.0.5",
"split_keywords": [
"neural network",
"python",
"ai",
"numpy",
"deep learning"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e88761e56a7d665eb73a3c66c005b45704cf27b97403e48426a32e0e5394adbb",
"md5": "6173e84bd0d2e121dab595273d28497f",
"sha256": "5d78ec740cf9fdeffcdc942a9e81e7640e028d6577e7a94f435efd3ecb520b30"
},
"downloads": -1,
"filename": "NeuralNetPy-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6173e84bd0d2e121dab595273d28497f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9937,
"upload_time": "2023-04-26T15:34:17",
"upload_time_iso_8601": "2023-04-26T15:34:17.441610Z",
"url": "https://files.pythonhosted.org/packages/e8/87/61e56a7d665eb73a3c66c005b45704cf27b97403e48426a32e0e5394adbb/NeuralNetPy-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "75b01335453bfab1a84e4fe9dec14aabf1bb511adc91e3c2ffd91abfcc475be1",
"md5": "8876c374f1064cd7489a2852352bfdd1",
"sha256": "8c4ec57b4b7c76312f1662cbfce1953b0a944dadb90a5d4c1eb223c679c5ce25"
},
"downloads": -1,
"filename": "NeuralNetPy-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "8876c374f1064cd7489a2852352bfdd1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7797,
"upload_time": "2023-04-26T15:34:20",
"upload_time_iso_8601": "2023-04-26T15:34:20.054336Z",
"url": "https://files.pythonhosted.org/packages/75/b0/1335453bfab1a84e4fe9dec14aabf1bb511adc91e3c2ffd91abfcc475be1/NeuralNetPy-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-26 15:34:20",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "neuralnetpy"
}