├── neowise ├── neowise.egg-info │ ├── dependency_links.txt │ ├── top_level.txt │ ├── requires.txt │ ├── SOURCES.txt │ └── PKG-INFO ├── Visuals │ ├── 1.png │ ├── fit.png │ ├── accu.gif │ ├── costs.gif │ ├── costs.png │ ├── test.png │ ├── accuracy.png │ └── summary.png ├── dist │ ├── neowise-0.0.9.tar.gz │ └── neowise-0.0.9-py3-none-any.whl ├── .idea │ ├── vcs.xml │ ├── .gitignore │ ├── misc.xml │ ├── inspectionProfiles │ │ ├── profiles_settings.xml │ │ └── Project_Default.xml │ ├── modules.xml │ └── neowise.iml ├── neowise │ ├── __init__.py │ ├── regularizers.py │ ├── activations.py │ ├── cost_function.py │ ├── layers.py │ ├── optimizers.py │ ├── plots.py │ ├── functional.py │ └── neural_net.py ├── build │ └── lib │ │ └── neowise │ │ ├── __init__.py │ │ ├── regularizers.py │ │ ├── activations.py │ │ ├── cost_function.py │ │ ├── layers.py │ │ ├── optimizers.py │ │ ├── plots.py │ │ ├── functional.py │ │ └── neural_net.py ├── setup.py ├── LICENSE.md ├── README.md └── DOCUMENTATION.md ├── neowise.png ├── .idea ├── dictionaries │ └── pranavsastry.xml ├── vcs.xml ├── .gitignore ├── inspectionProfiles │ ├── profiles_settings.xml │ └── Project_Default.xml ├── modules.xml ├── neowise.iml └── misc.xml ├── .github └── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── Example Notebooks ├── README.md ├── mnist_nw.ipynb ├── moons_nw.ipynb ├── circles_nw.ipynb └── spirals_nw.ipynb ├── LICENSE ├── README.md ├── CODE_OF_CONDUCT.md └── DOCUMENTATION.md /neowise/neowise.egg-info/dependency_links.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /neowise/neowise.egg-info/top_level.txt: -------------------------------------------------------------------------------- 1 | neowise 2 | -------------------------------------------------------------------------------- /neowise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise.png -------------------------------------------------------------------------------- /neowise/Visuals/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/1.png -------------------------------------------------------------------------------- /neowise/Visuals/fit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/fit.png -------------------------------------------------------------------------------- /neowise/Visuals/accu.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/accu.gif -------------------------------------------------------------------------------- /neowise/Visuals/costs.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/costs.gif -------------------------------------------------------------------------------- /neowise/Visuals/costs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/costs.png -------------------------------------------------------------------------------- /neowise/Visuals/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/test.png -------------------------------------------------------------------------------- /neowise/neowise.egg-info/requires.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | matplotlib 3 | hdfdict 4 | prettytable 5 | tqdm 6 | -------------------------------------------------------------------------------- /neowise/Visuals/accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/accuracy.png -------------------------------------------------------------------------------- /neowise/Visuals/summary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/Visuals/summary.png -------------------------------------------------------------------------------- /neowise/dist/neowise-0.0.9.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/dist/neowise-0.0.9.tar.gz -------------------------------------------------------------------------------- /neowise/dist/neowise-0.0.9-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranftw/neowise/HEAD/neowise/dist/neowise-0.0.9-py3-none-any.whl -------------------------------------------------------------------------------- /.idea/dictionaries/pranavsastry.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | initi 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /neowise/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /neowise/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /neowise/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /neowise/.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /neowise/neowise/__init__.py: -------------------------------------------------------------------------------- 1 | import neowise.activations 2 | import neowise.cost_function 3 | import neowise.functional 4 | import neowise.layers 5 | import neowise.optimizers 6 | import neowise.plots 7 | import neowise.regularizers 8 | 9 | from neowise.neural_net import Model 10 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/__init__.py: -------------------------------------------------------------------------------- 1 | import neowise.activations 2 | import neowise.cost_function 3 | import neowise.functional 4 | import neowise.layers 5 | import neowise.optimizers 6 | import neowise.plots 7 | import neowise.regularizers 8 | 9 | from neowise.neural_net import Model 10 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /neowise/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/neowise.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /neowise/.idea/neowise.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /neowise/neowise.egg-info/SOURCES.txt: -------------------------------------------------------------------------------- 1 | README.md 2 | setup.py 3 | neowise/__init__.py 4 | neowise/activations.py 5 | neowise/cost_function.py 6 | neowise/functional.py 7 | neowise/layers.py 8 | neowise/neural_net.py 9 | neowise/optimizers.py 10 | neowise/plots.py 11 | neowise/regularizers.py 12 | neowise.egg-info/PKG-INFO 13 | neowise.egg-info/SOURCES.txt 14 | neowise.egg-info/dependency_links.txt 15 | neowise.egg-info/requires.txt 16 | neowise.egg-info/top_level.txt -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /neowise/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | with open("DOCUMENTATION.md", "r") as fh: 4 | long_description = fh.read() 5 | setup(name="neowise", 6 | version='0.1.0', 7 | description="A Deep Learning library built from scratch using Python and NumPy", 8 | author="Pranav Sastry", 9 | author_email="pranava.sri@gmail.com", 10 | long_description=long_description, 11 | long_description_content_type='text/markdown', 12 | maintainer="Pranav Sastry", 13 | url="https://github.com/pranavsastry/neowise", 14 | packages=find_packages(), 15 | install_requires=['numpy', 'matplotlib', 'hdfdict', 'prettytable', 'tqdm'], 16 | classifiers=[ 17 | "Programming Language :: Python :: 3", 18 | "License :: OSI Approved :: MIT License" 19 | ]) 20 | -------------------------------------------------------------------------------- /Example Notebooks/README.md: -------------------------------------------------------------------------------- 1 | ## **Example Notebooks** 2 | 3 | ### There are four datasets that are available for Google Colab: 4 | 1). [MNIST Colab](https://colab.research.google.com/drive/1Vz6oa1RzFLaNJJgx7eUYFnPJgPknRO2U?usp=sharing)
5 | 2). [Make Moons Colab](https://colab.research.google.com/drive/1lETY4PhNjw39G6yNLAROp_KJI4qYeVQM?usp=sharing)
6 | 3). [Make Circles Colab](https://colab.research.google.com/drive/1lLunGdxCgj-2Q2etxbBfTmhUG_e7LYaI?usp=sharing)
7 | 4). [Spirals Colab](https://colab.research.google.com/drive/1na0qAjzshP8J0HbuwdIePQhFZeD3JwEY?usp=sharing)
8 | 9 | ## Working through the notebook 10 | 1). Mount your Drive on Google Colab
11 | ![mount](/neowise/Visuals/1.png)
12 | 13 | 2). Create a folder on your drive and copy the path of the folder and create several folders inside it, preferably as in the notebook.
14 | 15 | ## Note: 16 | The credit for Binary Classification Decision Boundary plotting goes to Piotr Skalski! 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Pranav Sastry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /neowise/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 28 | -------------------------------------------------------------------------------- /neowise/LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Pranav Sastry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /neowise/README.md: -------------------------------------------------------------------------------- 1 | 2 | # neowise 3 | 4 | ### A Deep Learning library built from scratch with Python and NumPy 5 | ![logo](/neowise.png) 6 | 7 | ### pip install 8 | `pip install neowise` 9 | 10 | ### [Documentation](https://github.com/pranavsastry/neowise/blob/master/DOCUMENTATION.md) 11 | 12 | ### Features of *neowise* 13 | 14 | - Get summary of your model, by calling `model.summary` 15 | ![summary](Visuals/summary.png)
16 | - Save your model in a .h5 file using `model.save_model`
17 | - Load your saved model with `model.load_model`
18 | - Train your model with less than 10 lines of code (*excluding data processing*), with a simple API
19 | - Test your model with `model.test`
20 | ![test](Visuals/test.png) 21 | - Plot static graphs of Cost and Accuracy using `model.plot`
22 | ![costs](Visuals/costs.png)
23 | ![accuracy](Visuals/accuracy.png)
24 | - Train using optimizers such as Gradient Descent, Momentum, RMSprop, Adam, Batch Gradient Descent and Stochastic Gradient Descent
25 | - Train using Dropout regularization for deeper networks
26 | - While, training the models, keep track of your model's progress through tqdm's progress bar
27 | ![fit](Visuals/fit.png)
28 | - Create animated graphs of Cost and Accuracy with `model.plot` and set `animate=True` to save images of plots which can then be fed to a GIF creator to create an animated GIF
29 | ![costs_gif](Visuals/costs.gif)
30 | ![accu_gif](Visuals/accu.gif)
31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # neowise
3 | [![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fpranavsastry%2Fneowise)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fpranavsastry%2Fneowise) 4 | 5 | ### A Deep Learning library built from scratch with Python and NumPy 6 | ![logo](/neowise.png) 7 | 8 | ### pip install 9 | `pip install neowise` 10 | 11 | ### [Documentation](https://github.com/pranavsastry/neowise/blob/master/DOCUMENTATION.md)
12 | ### [pypi Package Website](https://pypi.org/project/neowise/)
13 | 14 | ### Features of *neowise* 15 | 16 | - Get summary of your model, by calling `model.summary` 17 | ![summary](neowise/Visuals/summary.png)
18 | - Save your model in a .h5 file using `model.save_model`
19 | - Load your saved model with `model.load_model`
20 | - Train your model with less than 10 lines of code (*excluding data processing*), with a simple API
21 | - Test your model with `model.test`
22 | ![test](neowise/Visuals/test.png) 23 | - Plot static graphs of Cost and Accuracy using `model.plot`
24 | ![costs](neowise/Visuals/costs.png)
25 | ![accuracy](neowise/Visuals/accuracy.png)
26 | - Train using optimizers such as Gradient Descent, Momentum, RMSprop, Adam, Batch Gradient Descent and Stochastic Gradient Descent
27 | - Train using Dropout regularization for deeper networks
28 | - While, training the models, keep track of your model's progress through tqdm's progress bar
29 | ![fit](neowise/Visuals/fit.png)
30 | - Create animated graphs of Cost and Accuracy with `model.plot` and set `animate=True` to save images of plots which can then be fed to a GIF creator to create an animated GIF
31 | ![costs_gif](neowise/Visuals/costs.gif)
32 | ![accu_gif](neowise/Visuals/accu.gif)
33 | -------------------------------------------------------------------------------- /neowise/neowise/regularizers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class RegularizationHelpers: 5 | """ 6 | Regularization Helpers 7 | Used to initiate the instance variables of nw.regularizers 8 | 9 | Arguments: 10 | layers_arr: Array containing the objects of nw.layers (List) 11 | lamb: Regularization parameter "lambda" (float) 12 | m_exam: Number of data examples (int) 13 | """ 14 | def __init__(self, layers_arr, lamb, m_exam): 15 | self.layers_arr, self.lamb, self.m_exam = layers_arr, lamb, m_exam 16 | 17 | 18 | class L1Reg(RegularizationHelpers): 19 | """ 20 | L1 Regularization 21 | Calculates the sum of all the layers' weights 22 | 23 | Arguments: 24 | layers_arr: Array containing the objects of nw.layers (List) 25 | lamb: Regularization parameter "lambda" (float) 26 | m_exam: Number of data examples (int) 27 | """ 28 | def __init__(self, layers_arr, lamb, m_exam): 29 | RegularizationHelpers.__init__(self, layers_arr, lamb, m_exam) 30 | 31 | def __call__(self): 32 | temp_sum = 0 33 | for layers in self.layers_arr: 34 | temp_sum = temp_sum + ((self.lamb / self.m_exam) * (np.sum(np.sum(layers.weights)))) 35 | layers.grad_reg = ((self.lamb / self.m_exam) * (layers.grad_L1)) 36 | return temp_sum 37 | 38 | 39 | class L2Reg(RegularizationHelpers): 40 | """ 41 | L2 Regularization 42 | Calculates the sum of the square of all the layers' weights 43 | 44 | Arguments: 45 | layers_arr: Array containing the objects of nw.layers (List) 46 | lamb: Regularization parameter "lambda" (float) 47 | m_exam: Number of data examples (int) 48 | """ 49 | def __init__(self, layers_arr, lamb, m_exam): 50 | RegularizationHelpers.__init__(self, layers_arr, lamb, m_exam) 51 | 52 | def __call__(self): 53 | temp_sum = 0 54 | for layers in self.layers_arr: 55 | temp_sum = temp_sum + ((self.lamb / (2 * self.m_exam)) * (np.sum(np.sum(np.square(layers.weights))))) 56 | layers.grad_reg = ((self.lamb / self.m_exam) * layers.weights) 57 | return temp_sum 58 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/regularizers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class RegularizationHelpers: 5 | """ 6 | Regularization Helpers 7 | Used to initiate the instance variables of nw.regularizers 8 | 9 | Arguments: 10 | layers_arr: Array containing the objects of nw.layers (List) 11 | lamb: Regularization parameter "lambda" (float) 12 | m_exam: Number of data examples (int) 13 | """ 14 | def __init__(self, layers_arr, lamb, m_exam): 15 | self.layers_arr, self.lamb, self.m_exam = layers_arr, lamb, m_exam 16 | 17 | 18 | class L1Reg(RegularizationHelpers): 19 | """ 20 | L1 Regularization 21 | Calculates the sum of all the layers' weights 22 | 23 | Arguments: 24 | layers_arr: Array containing the objects of nw.layers (List) 25 | lamb: Regularization parameter "lambda" (float) 26 | m_exam: Number of data examples (int) 27 | """ 28 | def __init__(self, layers_arr, lamb, m_exam): 29 | RegularizationHelpers.__init__(self, layers_arr, lamb, m_exam) 30 | 31 | def __call__(self): 32 | temp_sum = 0 33 | for layers in self.layers_arr: 34 | temp_sum = temp_sum + ((self.lamb / self.m_exam) * (np.sum(np.sum(layers.weights)))) 35 | layers.grad_reg = ((self.lamb / self.m_exam) * (layers.grad_L1)) 36 | return temp_sum 37 | 38 | 39 | class L2Reg(RegularizationHelpers): 40 | """ 41 | L2 Regularization 42 | Calculates the sum of the square of all the layers' weights 43 | 44 | Arguments: 45 | layers_arr: Array containing the objects of nw.layers (List) 46 | lamb: Regularization parameter "lambda" (float) 47 | m_exam: Number of data examples (int) 48 | """ 49 | def __init__(self, layers_arr, lamb, m_exam): 50 | RegularizationHelpers.__init__(self, layers_arr, lamb, m_exam) 51 | 52 | def __call__(self): 53 | temp_sum = 0 54 | for layers in self.layers_arr: 55 | temp_sum = temp_sum + ((self.lamb / (2 * self.m_exam)) * (np.sum(np.sum(np.square(layers.weights))))) 56 | layers.grad_reg = ((self.lamb / self.m_exam) * layers.weights) 57 | return temp_sum 58 | -------------------------------------------------------------------------------- /neowise/neowise/activations.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class Relu: 5 | """ 6 | Rectified Linear Unit 7 | 8 | Methods: 9 | forward(self): Returns a NumPy nd-array of Relu of argument 10 | backward(self): Returns the derivative of Relu w.r.t its argument 11 | Arguments: 12 | self: A NumPy nd-array 13 | Returns: 14 | NumPy nd-array of same shape as input 15 | """ 16 | def forward(self): 17 | return np.maximum(0, self) 18 | 19 | def backward(self): 20 | self[self <= 0] = 0 21 | self[self > 0] = 1 22 | return self 23 | 24 | 25 | class Tanh: 26 | """ 27 | Hyperbolic Tangent 28 | 29 | Methods: 30 | forward(self): Returns a NumPy nd-array of Tanh of argument 31 | backward(self): Returns the derivative of Tanh w.r.t its argument 32 | Arguments: 33 | self: A NumPy nd-array 34 | Returns: 35 | NumPy nd-array of same shape as input 36 | """ 37 | def forward(self): 38 | return np.tanh(self) 39 | 40 | def backward(self): 41 | return (1 - np.square(Tanh.forward(self))) 42 | 43 | 44 | class Sigmoid: 45 | """ 46 | Sigmoid 47 | 48 | Methods: 49 | forward(self): Returns a NumPy nd-array of Sigmoid of argument 50 | backward(self): Returns the derivative of Sigmoid w.r.t its argument 51 | Arguments: 52 | self: A NumPy nd-array 53 | Returns: 54 | NumPy nd-array of same shape as input 55 | """ 56 | def forward(self): 57 | return (1 / (1 + np.exp(-self))) 58 | 59 | def backward(self): 60 | return (Sigmoid.forward(self) * (1 - Sigmoid.forward(self))) 61 | 62 | 63 | class Softmax: 64 | """ 65 | Softmax 66 | 67 | Methods: 68 | forward(self): Returns a NumPy nd-array of Softmax of argument 69 | backward(self): Returns the derivative of Softmax w.r.t its argument 70 | Arguments: 71 | self: A NumPy nd-array 72 | Returns: 73 | NumPy nd-array of same shape as input 74 | """ 75 | def forward(self): 76 | soft = np.exp(self) / np.sum(np.exp(self), axis=0) 77 | return soft 78 | 79 | def backward(self): 80 | return (Softmax.forward(self) * (1 - Softmax.forward(self))) 81 | 82 | 83 | class Sine: 84 | """ 85 | Sinusoidal 86 | 87 | Methods: 88 | forward(self): Returns a NumPy nd-array of Sine of argument 89 | backward(self): Returns the derivative of Sine w.r.t its argument 90 | Arguments: 91 | self: A NumPy nd-array 92 | Returns: 93 | NumPy nd-array of same shape as input 94 | """ 95 | def forward(self): 96 | return np.sin(self) 97 | 98 | def backward(self): 99 | return np.cos(self) 100 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/activations.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class Relu: 5 | """ 6 | Rectified Linear Unit 7 | 8 | Methods: 9 | forward(self): Returns a NumPy nd-array of Relu of argument 10 | backward(self): Returns the derivative of Relu w.r.t its argument 11 | Arguments: 12 | self: A NumPy nd-array 13 | Returns: 14 | NumPy nd-array of same shape as input 15 | """ 16 | def forward(self): 17 | return np.maximum(0, self) 18 | 19 | def backward(self): 20 | self[self <= 0] = 0 21 | self[self > 0] = 1 22 | return self 23 | 24 | 25 | class Tanh: 26 | """ 27 | Hyperbolic Tangent 28 | 29 | Methods: 30 | forward(self): Returns a NumPy nd-array of Tanh of argument 31 | backward(self): Returns the derivative of Tanh w.r.t its argument 32 | Arguments: 33 | self: A NumPy nd-array 34 | Returns: 35 | NumPy nd-array of same shape as input 36 | """ 37 | def forward(self): 38 | return np.tanh(self) 39 | 40 | def backward(self): 41 | return (1 - np.square(Tanh.forward(self))) 42 | 43 | 44 | class Sigmoid: 45 | """ 46 | Sigmoid 47 | 48 | Methods: 49 | forward(self): Returns a NumPy nd-array of Sigmoid of argument 50 | backward(self): Returns the derivative of Sigmoid w.r.t its argument 51 | Arguments: 52 | self: A NumPy nd-array 53 | Returns: 54 | NumPy nd-array of same shape as input 55 | """ 56 | def forward(self): 57 | return (1 / (1 + np.exp(-self))) 58 | 59 | def backward(self): 60 | return (Sigmoid.forward(self) * (1 - Sigmoid.forward(self))) 61 | 62 | 63 | class Softmax: 64 | """ 65 | Softmax 66 | 67 | Methods: 68 | forward(self): Returns a NumPy nd-array of Softmax of argument 69 | backward(self): Returns the derivative of Softmax w.r.t its argument 70 | Arguments: 71 | self: A NumPy nd-array 72 | Returns: 73 | NumPy nd-array of same shape as input 74 | """ 75 | def forward(self): 76 | soft = np.exp(self) / np.sum(np.exp(self), axis=0) 77 | return soft 78 | 79 | def backward(self): 80 | return (Softmax.forward(self) * (1 - Softmax.forward(self))) 81 | 82 | 83 | class Sine: 84 | """ 85 | Sinusoidal 86 | 87 | Methods: 88 | forward(self): Returns a NumPy nd-array of Sine of argument 89 | backward(self): Returns the derivative of Sine w.r.t its argument 90 | Arguments: 91 | self: A NumPy nd-array 92 | Returns: 93 | NumPy nd-array of same shape as input 94 | """ 95 | def forward(self): 96 | return np.sin(self) 97 | 98 | def backward(self): 99 | return np.cos(self) 100 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at pranava.sri@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /neowise/neowise/cost_function.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from neowise.functional import * 3 | from neowise.regularizers import * 4 | 5 | 6 | class CostFunctionHelpers: 7 | """ 8 | Cost Function Helpers 9 | Used to initiate the instance variables of Cost Functions 10 | 11 | Arguments: 12 | y: Outputs of the train/test data (nd-array) 13 | A: Activations of the output layer of the network (nd-array) 14 | layers_arr: A Python list containing objects of nw.layers (list) 15 | lamb: Regularization parameter "lambda" (float) 16 | reg: Type of Regularization (str) 17 | """ 18 | def __init__(self, y, A, layers_arr, lamb, reg): 19 | self.y, self.A, self.layers_arr, self.lamb, self.reg = y, A, layers_arr, lamb, reg 20 | 21 | 22 | class BinaryCrossEntropy(CostFunctionHelpers): 23 | """ 24 | Binary Cross Entropy 25 | Used to calculate the cost for Binary Classification tasks 26 | 27 | Arguments: 28 | y: Outputs of the train/test data (nd-array) 29 | A: Activations of the output layer of the network (nd-array) 30 | layers_arr: A Python list containing objects of nw.layers (list) 31 | lamb: Regularization parameter "lambda" (float) 32 | reg: Type of Regularization (str) 33 | Returns: 34 | cost: Compares the outputs to the predicted labels and returns a floating point integer (float) 35 | grad: Returns the gradient of the Cost Function w.r.t its Activations (nd-array) 36 | """ 37 | def __init__(self, y, A, layers_arr, lamb, reg=None): 38 | CostFunctionHelpers.__init__(self, y, A, layers_arr, lamb, reg) 39 | 40 | def __call__(self): 41 | if self.reg is not None: 42 | if self.reg == "L1": 43 | GradL1Reg(self.layers_arr)() 44 | temp_sum = L1Reg(self.layers_arr, self.lamb, self.y.shape[1])() 45 | if self.reg == "L2": 46 | temp_sum = L2Reg(self.layers_arr, self.lamb, self.y.shape[1])() 47 | cost = (((-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A)) + ((1 - self.y) * (np.log(1 - self.A))))))) + temp_sum) 48 | grad = (-1 / self.y.shape[1]) * ((self.y / self.A) - ((1 - self.y) / (1 - self.A))) 49 | else: 50 | cost = (-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A)) + ((1 - self.y) * (np.log(1 - self.A)))))) 51 | grad = (-1 / self.y.shape[1]) * ((self.y / self.A) - ((1 - self.y) / (1 - self.A))) 52 | for layers in self.layers_arr: 53 | layers.grad_reg = 0 54 | return cost, grad 55 | 56 | 57 | class CrossEntropy(CostFunctionHelpers): 58 | """ 59 | Cross Entropy 60 | Used to calculate the cost for Multi Class Classification tasks 61 | 62 | Arguments: 63 | y: Outputs of the train/test data (nd-array) 64 | A: Activations of the output layer of the network (nd-array) 65 | layers_arr: A Python list containing objects of nw.layers (list) 66 | lamb: Regularization parameter "lambda" (float) 67 | reg: Type of Regularization (str) 68 | Returns: 69 | cost: Compares the outputs to the predicted labels and returns a floating point integer (float) 70 | grad: Returns the gradient of the Cost Function w.r.t its Activations (nd-array) 71 | """ 72 | def __init__(self, y, A, layers_arr, lamb, reg=None): 73 | CostFunctionHelpers.__init__(self, y, A, layers_arr, lamb, reg) 74 | 75 | def __call__(self): 76 | if self.reg is not None: 77 | if self.reg == "L1": 78 | GradL1Reg(self.layers_arr)() 79 | temp_sum = L1Reg(self.layers_arr, self.lamb, self.y.shape[1])() 80 | if self.reg == "L2": 81 | temp_sum = L2Reg(self.layers_arr, self.lamb, self.y.shape[1])() 82 | cost = (((-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A)))))) + temp_sum) 83 | grad = (-1 / self.y.shape[1]) * (self.y / self.A) 84 | else: 85 | cost = (-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A))))) 86 | grad = (-1 / self.y.shape[1]) * (self.y / self.A) 87 | for layers in self.layers_arr: 88 | layers.grad_reg = 0 89 | return cost, grad 90 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/cost_function.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from neowise.functional import * 3 | from neowise.regularizers import * 4 | 5 | 6 | class CostFunctionHelpers: 7 | """ 8 | Cost Function Helpers 9 | Used to initiate the instance variables of Cost Functions 10 | 11 | Arguments: 12 | y: Outputs of the train/test data (nd-array) 13 | A: Activations of the output layer of the network (nd-array) 14 | layers_arr: A Python list containing objects of nw.layers (list) 15 | lamb: Regularization parameter "lambda" (float) 16 | reg: Type of Regularization (str) 17 | """ 18 | def __init__(self, y, A, layers_arr, lamb, reg): 19 | self.y, self.A, self.layers_arr, self.lamb, self.reg = y, A, layers_arr, lamb, reg 20 | 21 | 22 | class BinaryCrossEntropy(CostFunctionHelpers): 23 | """ 24 | Binary Cross Entropy 25 | Used to calculate the cost for Binary Classification tasks 26 | 27 | Arguments: 28 | y: Outputs of the train/test data (nd-array) 29 | A: Activations of the output layer of the network (nd-array) 30 | layers_arr: A Python list containing objects of nw.layers (list) 31 | lamb: Regularization parameter "lambda" (float) 32 | reg: Type of Regularization (str) 33 | Returns: 34 | cost: Compares the outputs to the predicted labels and returns a floating point integer (float) 35 | grad: Returns the gradient of the Cost Function w.r.t its Activations (nd-array) 36 | """ 37 | def __init__(self, y, A, layers_arr, lamb, reg=None): 38 | CostFunctionHelpers.__init__(self, y, A, layers_arr, lamb, reg) 39 | 40 | def __call__(self): 41 | if self.reg is not None: 42 | if self.reg == "L1": 43 | GradL1Reg(self.layers_arr)() 44 | temp_sum = L1Reg(self.layers_arr, self.lamb, self.y.shape[1])() 45 | if self.reg == "L2": 46 | temp_sum = L2Reg(self.layers_arr, self.lamb, self.y.shape[1])() 47 | cost = (((-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A)) + ((1 - self.y) * (np.log(1 - self.A))))))) + temp_sum) 48 | grad = (-1 / self.y.shape[1]) * ((self.y / self.A) - ((1 - self.y) / (1 - self.A))) 49 | else: 50 | cost = (-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A)) + ((1 - self.y) * (np.log(1 - self.A)))))) 51 | grad = (-1 / self.y.shape[1]) * ((self.y / self.A) - ((1 - self.y) / (1 - self.A))) 52 | for layers in self.layers_arr: 53 | layers.grad_reg = 0 54 | return cost, grad 55 | 56 | 57 | class CrossEntropy(CostFunctionHelpers): 58 | """ 59 | Cross Entropy 60 | Used to calculate the cost for Multi Class Classification tasks 61 | 62 | Arguments: 63 | y: Outputs of the train/test data (nd-array) 64 | A: Activations of the output layer of the network (nd-array) 65 | layers_arr: A Python list containing objects of nw.layers (list) 66 | lamb: Regularization parameter "lambda" (float) 67 | reg: Type of Regularization (str) 68 | Returns: 69 | cost: Compares the outputs to the predicted labels and returns a floating point integer (float) 70 | grad: Returns the gradient of the Cost Function w.r.t its Activations (nd-array) 71 | """ 72 | def __init__(self, y, A, layers_arr, lamb, reg=None): 73 | CostFunctionHelpers.__init__(self, y, A, layers_arr, lamb, reg) 74 | 75 | def __call__(self): 76 | if self.reg is not None: 77 | if self.reg == "L1": 78 | GradL1Reg(self.layers_arr)() 79 | temp_sum = L1Reg(self.layers_arr, self.lamb, self.y.shape[1])() 80 | if self.reg == "L2": 81 | temp_sum = L2Reg(self.layers_arr, self.lamb, self.y.shape[1])() 82 | cost = (((-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A)))))) + temp_sum) 83 | grad = (-1 / self.y.shape[1]) * (self.y / self.A) 84 | else: 85 | cost = (-1 / self.y.shape[1]) * (np.sum(np.sum((self.y * np.log(self.A))))) 86 | grad = (-1 / self.y.shape[1]) * (self.y / self.A) 87 | for layers in self.layers_arr: 88 | layers.grad_reg = 0 89 | return cost, grad 90 | -------------------------------------------------------------------------------- /neowise/neowise/layers.py: -------------------------------------------------------------------------------- 1 | from neowise.activations import * 2 | 3 | 4 | class Dense: 5 | """ 6 | Dense 7 | Creates Fully connected Neural Network layer 8 | 9 | Arguments: 10 | num_inputs: Number of input units to the current layer (int) 11 | num_outputs: Number of units in the current layer (int) 12 | activation_fn: Activation function for the current layer (str) 13 | dropout: Value between 0 and 1 (default=1) (float) 14 | weights: Weights for the layer in dimensions (num_outputs,num_inputs) (nd-array) 15 | bias: Bias of the layer in dimensions (num_outputs,1) (nd-array) 16 | dZ: Gradient of Cost function w.r.t the outputs of the layer (nd-array) 17 | dW: Gradient of Cost function w.r.t the weights of the layer (nd-array) 18 | db: Gradient of Cost function w.r.t the bias of the layer (nd-array) 19 | dA: Gradient of Cost function w.r.t the activations of the layer (nd-array) 20 | grad_L1: Gradient of the weights w.r.t itself (nd-array) 21 | grad_reg: Calculates the gradient of Regularization functions (nd-array) 22 | """ 23 | def __init__(self, num_inputs, num_outputs, activation_fn, dropout=1.0, weights=None, bias=None, dZ=None, dW=None, 24 | db=None, dA=None, grad_L1=None, grad_reg=None): 25 | self.num_inputs = num_inputs 26 | self.num_outputs = num_outputs 27 | self.activation_fn = activation_fn 28 | self.dropout = dropout 29 | self.dZ, self.dW, self.db, self.dA = dZ, dW, db, dA 30 | self.grad_L1 = grad_L1 31 | self.grad_reg = grad_reg 32 | self.weights = weights 33 | self.bias = bias 34 | self.activ_dict = {"relu": [Relu.forward, Relu.backward, 2], 35 | "tanh": [Tanh.forward, Tanh.backward, 1], 36 | "sigmoid": [Sigmoid.forward, Sigmoid.backward, 1], 37 | "softmax": [Softmax.forward, Softmax.backward, 1], 38 | "sine": [Sine.forward, Sine.backward, 6]} 39 | 40 | def initialize_params(self): 41 | """ 42 | Initialize Parameters 43 | Initializes the weights and bias for the layer randomly using Xavier Initialization 44 | """ 45 | self.weights = np.random.randn(self.num_outputs, self.num_inputs) * ( 46 | np.sqrt(self.activ_dict[self.activation_fn][2] / self.num_inputs)) 47 | self.bias = np.random.randn(self.num_outputs, 1) * 0.01 48 | return self.weights, self.bias, self.grad_reg, self.grad_L1 49 | 50 | def get_params(self): 51 | """ 52 | Get Parameters 53 | Returns the weights and biases of the layer 54 | """ 55 | return self.weights, self.bias 56 | 57 | def forw_prop(self, A_prev, train=True): 58 | """ 59 | Forward Propagation 60 | Propagates the input data through the layer to produce outputs 61 | :param A_prev: Activations of the previous layer 62 | :param train: Boolean value, whether the network is being trained or not 63 | :return: outputs and activations of the layer 64 | """ 65 | if train is False: 66 | self.dropout = 1 67 | self.outputs = np.dot(self.weights, A_prev) + self.bias 68 | self.activations_temp = self.activ_dict[self.activation_fn][0](self.outputs) 69 | self.activations = self.activations_temp * ( 70 | (np.random.rand(self.outputs.shape[0], self.outputs.shape[1]) < self.dropout) / self.dropout) 71 | return self.outputs, self.activations 72 | 73 | def back_prop(self, dA_prev, A_prev): 74 | """ 75 | Backward Propagation 76 | Calculates the gradient of the Cost Function w.r.t the weights,bias,outputs and activations 77 | :param dA_prev: Gradient of the cost function w.r.t the activation of the previous layer 78 | :param A_prev: Activations of the previous layer 79 | :return: dZ, dW, db, dA 80 | """ 81 | self.dZ = dA_prev * self.activ_dict[self.activation_fn][1](self.outputs) 82 | self.dW = (np.dot(self.dZ, A_prev.T)) + self.grad_reg 83 | self.db = np.sum(self.dZ, axis=1, keepdims=True) 84 | self.dA = np.dot(self.weights.T, self.dZ) 85 | return self.dZ, self.dW, self.db, self.dA 86 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/layers.py: -------------------------------------------------------------------------------- 1 | from neowise.activations import * 2 | 3 | 4 | class Dense: 5 | """ 6 | Dense 7 | Creates Fully connected Neural Network layer 8 | 9 | Arguments: 10 | num_inputs: Number of input units to the current layer (int) 11 | num_outputs: Number of units in the current layer (int) 12 | activation_fn: Activation function for the current layer (str) 13 | dropout: Value between 0 and 1 (default=1) (float) 14 | weights: Weights for the layer in dimensions (num_outputs,num_inputs) (nd-array) 15 | bias: Bias of the layer in dimensions (num_outputs,1) (nd-array) 16 | dZ: Gradient of Cost function w.r.t the outputs of the layer (nd-array) 17 | dW: Gradient of Cost function w.r.t the weights of the layer (nd-array) 18 | db: Gradient of Cost function w.r.t the bias of the layer (nd-array) 19 | dA: Gradient of Cost function w.r.t the activations of the layer (nd-array) 20 | grad_L1: Gradient of the weights w.r.t itself (nd-array) 21 | grad_reg: Calculates the gradient of Regularization functions (nd-array) 22 | """ 23 | def __init__(self, num_inputs, num_outputs, activation_fn, dropout=1.0, weights=None, bias=None, dZ=None, dW=None, 24 | db=None, dA=None, grad_L1=None, grad_reg=None): 25 | self.num_inputs = num_inputs 26 | self.num_outputs = num_outputs 27 | self.activation_fn = activation_fn 28 | self.dropout = dropout 29 | self.dZ, self.dW, self.db, self.dA = dZ, dW, db, dA 30 | self.grad_L1 = grad_L1 31 | self.grad_reg = grad_reg 32 | self.weights = weights 33 | self.bias = bias 34 | self.activ_dict = {"relu": [Relu.forward, Relu.backward, 2], 35 | "tanh": [Tanh.forward, Tanh.backward, 1], 36 | "sigmoid": [Sigmoid.forward, Sigmoid.backward, 1], 37 | "softmax": [Softmax.forward, Softmax.backward, 1], 38 | "sine": [Sine.forward, Sine.backward, 6]} 39 | 40 | def initialize_params(self): 41 | """ 42 | Initialize Parameters 43 | Initializes the weights and bias for the layer randomly using Xavier Initialization 44 | """ 45 | self.weights = np.random.randn(self.num_outputs, self.num_inputs) * ( 46 | np.sqrt(self.activ_dict[self.activation_fn][2] / self.num_inputs)) 47 | self.bias = np.random.randn(self.num_outputs, 1) * 0.01 48 | return self.weights, self.bias, self.grad_reg, self.grad_L1 49 | 50 | def get_params(self): 51 | """ 52 | Get Parameters 53 | Returns the weights and biases of the layer 54 | """ 55 | return self.weights, self.bias 56 | 57 | def forw_prop(self, A_prev, train=True): 58 | """ 59 | Forward Propagation 60 | Propagates the input data through the layer to produce outputs 61 | :param A_prev: Activations of the previous layer 62 | :param train: Boolean value, whether the network is being trained or not 63 | :return: outputs and activations of the layer 64 | """ 65 | if train is False: 66 | self.dropout = 1 67 | self.outputs = np.dot(self.weights, A_prev) + self.bias 68 | self.activations_temp = self.activ_dict[self.activation_fn][0](self.outputs) 69 | self.activations = self.activations_temp * ( 70 | (np.random.rand(self.outputs.shape[0], self.outputs.shape[1]) < self.dropout) / self.dropout) 71 | return self.outputs, self.activations 72 | 73 | def back_prop(self, dA_prev, A_prev): 74 | """ 75 | Backward Propagation 76 | Calculates the gradient of the Cost Function w.r.t the weights,bias,outputs and activations 77 | :param dA_prev: Gradient of the cost function w.r.t the activation of the previous layer 78 | :param A_prev: Activations of the previous layer 79 | :return: dZ, dW, db, dA 80 | """ 81 | self.dZ = dA_prev * self.activ_dict[self.activation_fn][1](self.outputs) 82 | self.dW = (np.dot(self.dZ, A_prev.T)) + self.grad_reg 83 | self.db = np.sum(self.dZ, axis=1, keepdims=True) 84 | self.dA = np.dot(self.weights.T, self.dZ) 85 | return self.dZ, self.dW, self.db, self.dA 86 | -------------------------------------------------------------------------------- /DOCUMENTATION.md: -------------------------------------------------------------------------------- 1 | ## *neowise* Documentation 2 | 3 | ### Steps to train your own Neural Network using *neowise* 4 | 5 | 1. Install and `import neowise as nw` 6 | 7 | 2. Get your data and pre-process it. Your `data` should be in the dimensions as `(number_of_examples, number_of_features)` and `labels` should have `(number_of_output_units, number_of examples)` as its dimensions. 8 | This is a must, any changes here would raise errors! 9 | 10 | 3. Create a model by calling `model = nw.Model(your_train_data, your_train_labels, your_test_data, your_test_labels, your_crossval_data, your_crossval_labels)` If you do not have Cross Validation data, enter `None` for the last two arguments. 11 | 12 | 4. Add layers to your model by `model.add(layer_name,num_inputs,num_outputs,activation_function,dropout)`, where you give a unique name to each of your layers so that you know what type of layer it is. Example for `dense` layer, if it is your first layer, name it `dense1`. Enter the number of inputs to that layer, in `num_inputs` and number of units for that layer in `num_outputs`. 13 | For activation_function use *any of the following supported activation functions* **["relu", "sigmoid", "tanh", "softmax", "sine"]**. 14 | To prevent overflows and nans, we suggest that if you use a softmax classifier, to set the activation of the previous layer of the output layer as **"tanh"** as it squishes values between -1 and 1, thus preventing catastrophe. If you want to use Dropout, set the `dropout` anywhere between 0 and 1. Else, the default value is taken as 1, i.e no Dropout. 15 | 16 | 5. Just to be sure of your architecture and to know the amount of parameters that'll be trained call `model.summary()` which uses prettytable to print out a summary of your architecture. 17 | 18 | 6. Train your model using `model.fit(your_train_data, your_train_labels, learning_rate, number_of_iterations, optimizer, problem_type, mini_batch_size, regularization_type, lambda, learning_rate_decay)`, where you enter your training data, choose the learning rate, set the number of iterations to train for, choose which type of optimizer you want from **["GD" for Gradient Descent, "Momentum" for Gradient Descent using Momentum, "RMSprop" for Root Mean Square Propagation, "Adam" for Adaptive Moment Estimation]** If you want to train using ***Batch Gradient Descent***, choose **"GD"** as `optimizer` and set the `mini_batch_size` to the total number of examples in your training data and if you want to train using ***Stochastic Gradient Descent***, choose **"GD"** as `optimizer` and set `mini_batch_size` to 1. For `problem_type` choose any of the currently supported tasks **["Binary" for Binary Classification Tasks, "Multi" for Multi Class Classification Tasks]** Set `mini_batch_size` to the size you want each of your mini batches to be and be sure that this value is less than the total number of examples you have. If you want to use L1 or L2 regularization choose from **["L1" for L1 Regularization and "L2" for L2 Regularization]** and set the regularization parameter `lambda`. If you want your `learning_rate` to not be constant and be decreasing as the training progresses, set `alpha_decay` to True 19 | 20 | 7. Test the model on your test data by calling `model.test(your_test_data, your_test_labels, problem_type)` on your training data and set `problem_type` as you did for `model.fit`. This displays a prettytable with precision, recall and f1 scores and its accuracy on the test data. 21 | 22 | 8. For plotting the costs and accuracy vs number of iterations, call `model.plot(type_function, animate, directory, frequency)` and set `type_function` to **"Cost"** if you want to plot costs vs number of iterations and **"Accuracy"** for accuracy vs number of iterations. If you want to create animated graphs, set `animate` to True and specify the directory in which you want to save the plots in `directory` and set the frequency with which the graphs should update in `frequency`, then feed those images to a GIF creator to create animated plots. 23 | 24 | 9. To save the model, call `model.save_model(file_name)` and specify the directory in which you want to save the model with the name of the model with the extension .h5 in `filename` 25 | 26 | 10. To load a previously saved model, create a new model be calling `saved_model = nw.Model(your_train_data, your_train_labels, your_test_data, your_test_labels, your_crossval_data, your_crossval_labels)`, where these are the same data on which model was trained on. `Call saved_model.load_model(file_name)` to load the model from the directory specified in `file_name` 27 | 28 | 11. These are the functionalities that ***neowise*** offers. For detailed doc_strings visit [Source Code](https://github.com/pranavsastry/neowise/tree/master/neowise/neowise) to find more about the project! 29 | -------------------------------------------------------------------------------- /neowise/DOCUMENTATION.md: -------------------------------------------------------------------------------- 1 | ## *neowise* Documentation 2 | 3 | ### Steps to train your own Neural Network using *neowise* 4 | 5 | 1. Install and `import neowise as nw` 6 | 7 | 2. Get your data and pre-process it. Your `data` should be in the dimensions as `(number_of_examples, number_of_features)` and `labels` should have `(number_of_output_units, number_of examples)` as its dimensions. 8 | This is a must, any changes here would raise errors! 9 | 10 | 3. Create a model by calling `model = nw.Model(your_train_data, your_train_labels, your_test_data, your_test_labels, your_crossval_data, your_crossval_labels)` If you do not have Cross Validation data, enter `None` for the last two arguments. 11 | 12 | 4. Add layers to your model by `model.add(layer_name,num_inputs,num_outputs,activation_function,dropout)`, where you give a unique name to each of your layers so that you know what type of layer it is. Example for `dense` layer, if it is your first layer, name it `dense1`. Enter the number of inputs to that layer, in `num_inputs` and number of units for that layer in `num_outputs`. 13 | For activation_function use *any of the following supported activation functions* **["relu", "sigmoid", "tanh", "softmax", "sine"]**. 14 | To prevent overflows and nans, we suggest that if you use a softmax classifier, to set the activation of the previous layer of the output layer as **"tanh"** as it squishes values between -1 and 1, thus preventing catastrophe. If you want to use Dropout, set the `dropout` anywhere between 0 and 1. Else, the default value is taken as 1, i.e no Dropout. 15 | 16 | 5. Just to be sure of your architecture and to know the amount of parameters that'll be trained call `model.summary()` which uses prettytable to print out a summary of your architecture. 17 | 18 | 6. Train your model using `model.fit(your_train_data, your_train_labels, learning_rate, number_of_iterations, optimizer, problem_type, mini_batch_size, regularization_type, lambda, learning_rate_decay)`, where you enter your training data, choose the learning rate, set the number of iterations to train for, choose which type of optimizer you want from **["GD" for Gradient Descent, "Momentum" for Gradient Descent using Momentum, "RMSprop" for Root Mean Square Propagation, "Adam" for Adaptive Moment Estimation]** If you want to train using ***Batch Gradient Descent***, choose **"GD"** as `optimizer` and set the `mini_batch_size` to the total number of examples in your training data and if you want to train using ***Stochastic Gradient Descent***, choose **"GD"** as `optimizer` and set `mini_batch_size` to 1. For `problem_type` choose any of the currently supported tasks **["Binary" for Binary Classification Tasks, "Multi" for Multi Class Classification Tasks]** Set `mini_batch_size` to the size you want each of your mini batches to be and be sure that this value is less than the total number of examples you have. If you want to use L1 or L2 regularization choose from **["L1" for L1 Regularization and "L2" for L2 Regularization]** and set the regularization parameter `lambda`. If you want your `learning_rate` to not be constant and be decreasing as the training progresses, set `alpha_decay` to True 19 | 20 | 7. Test the model on your test data by calling `model.test(your_test_data, your_test_labels, problem_type)` on your training data and set `problem_type` as you did for `model.fit`. This displays a prettytable with precision, recall and f1 scores and its accuracy on the test data. 21 | 22 | 8. For plotting the costs and accuracy vs number of iterations, call `model.plot(type_function, animate, directory, frequency)` and set `type_function` to **"Cost"** if you want to plot costs vs number of iterations and **"Accuracy"** for accuracy vs number of iterations. If you want to create animated graphs, set `animate` to True and specify the directory in which you want to save the plots in `directory` and set the frequency with which the graphs should update in `frequency`, then feed those images to a GIF creator to create animated plots. 23 | 24 | 9. To save the model, call `model.save_model(file_name)` and specify the directory in which you want to save the model with the name of the model with the extension .h5 in `filename` 25 | 26 | 10. To load a previously saved model, create a new model be calling `saved_model = nw.Model(your_train_data, your_train_labels, your_test_data, your_test_labels, your_crossval_data, your_crossval_labels)`, where these are the same data on which model was trained on. `Call saved_model.load_model(file_name)` to load the model from the directory specified in `file_name` 27 | 28 | 11. These are the functionalities that ***neowise*** offers. For detailed doc_strings visit [Source Code](https://github.com/pranavsastry/neowise/tree/master/neowise/neowise) to find more about the project! 29 | -------------------------------------------------------------------------------- /neowise/neowise.egg-info/PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 2.1 2 | Name: neowise 3 | Version: 0.0.9 4 | Summary: A Deep Learning library built from scratch using Python and NumPy 5 | Home-page: https://github.com/pranavsastry/neowise 6 | Author: Pranav Sastry 7 | Author-email: pranava.sri@gmail.com 8 | Maintainer: Pranav Sastry 9 | License: UNKNOWN 10 | Description: ## *neowise* Documentation 11 | 12 | ### Steps to train your own Neural Network using *neowise* 13 | 14 | 1. Install and `import neowise as nw` 15 | 16 | 2. Get your data and pre-process it. Your `data` should be in the dimensions as `(number_of_examples, number_of_features)` and `labels` should have `(number_of_output_units, number_of examples)` as its dimensions. 17 | This is a must, any changes here would raise errors! 18 | 19 | 3. Create a model by calling `model = nw.Model(your_train_data, your_train_labels, your_test_data, your_test_labels, your_crossval_data, your_crossval_labels)` If you do not have Cross Validation data, enter `None` for the last two arguments. 20 | 21 | 4. Add layers to your model by `model.add(layer_name,num_inputs,num_outputs,activation_function,dropout)`, where you give a unique name to each of your layers so that you know what type of layer it is. Example for `dense` layer, if it is your first layer, name it `dense1`. Enter the number of inputs to that layer, in `num_inputs` and number of units for that layer in `num_outputs`. 22 | For activation_function use *any of the following supported activation functions* **["relu", "sigmoid", "tanh", "softmax", "sine"]**. 23 | To prevent overflows and nans, we suggest that if you use a softmax classifier, to set the activation of the previous layer of the output layer as **"tanh"** as it squishes values between -1 and 1, thus preventing catastrophe. If you want to use Dropout, set the `dropout` anywhere between 0 and 1. Else, the default value is taken as 1, i.e no Dropout. 24 | 25 | 5. Just to be sure of your architecture and to know the amount of parameters that'll be trained call `model.summary()` which uses prettytable to print out a summary of your architecture. 26 | 27 | 6. Train your model using `model.fit(your_train_data, your_train_labels, learning_rate, number_of_iterations, optimizer, problem_type, mini_batch_size, regularization_type, lambda, learning_rate_decay)`, where you enter your training data, choose the learning rate, set the number of iterations to train for, choose which type of optimizer you want from **["GD" for Gradient Descent, "Momentum" for Gradient Descent using Momentum, "RMSprop" for Root Mean Square Propagation, "Adam" for Adaptive Moment Estimation]** If you want to train using ***Batch Gradient Descent***, choose **"GD"** as `optimizer` and set the `mini_batch_size` to the total number of examples in your training data and if you want to train using ***Stochastic Gradient Descent***, choose **"GD"** as `optimizer` and set `mini_batch_size` to 1. For `problem_type` choose any of the currently supported tasks **["Binary" for Binary Classification Tasks, "Multi" for Multi Class Classification Tasks]** Set `mini_batch_size` to the size you want each of your mini batches to be and be sure that this value is less than the total number of examples you have. If you want to use L1 or L2 regularization choose from **["L1" for L1 Regularization and "L2" for L2 Regularization]** and set the regularization parameter `lambda`. If you want your `learning_rate` to not be constant and be decreasing as the training progresses, set `alpha_decay` to True 28 | 29 | 7. Test the model on your test data by calling `model.test(your_test_data, your_test_labels, problem_type)` on your training data and set `problem_type` as you did for `model.fit`. This displays a prettytable with precision, recall and f1 scores and its accuracy on the test data. 30 | 31 | 8. For plotting the costs and accuracy vs number of iterations, call `model.plot(type_function, animate, directory, frequency)` and set `type_function` to **"Cost"** if you want to plot costs vs number of iterations and **"Accuracy"** for accuracy vs number of iterations. If you want to create animated graphs, set `animate` to True and specify the directory in which you want to save the plots in `directory` and set the frequency with which the graphs should update in `frequency`, then feed those images to a GIF creator to create animated plots. 32 | 33 | 9. To save the model, call `model.save_model(file_name)` and specify the directory in which you want to save the model with the name of the model with the extension .h5 in `filename` 34 | 35 | 10. To load a previously saved model, create a new model be calling `saved_model = nw.Model(your_train_data, your_train_labels, your_test_data, your_test_labels, your_crossval_data, your_crossval_labels)`, where these are the same data on which model was trained on. `Call saved_model.load_model(file_name)` to load the model from the directory specified in `file_name` 36 | 37 | 11. These are the functionalities that ***neowise*** offers. For detailed doc_strings visit [Source Code](https://github.com/pranavsastry/neowise/tree/master/neowise/neowise) to find more about the project! 38 | 39 | Platform: UNKNOWN 40 | Classifier: Programming Language :: Python :: 3 41 | Classifier: License :: OSI Approved :: MIT License 42 | Description-Content-Type: text/markdown 43 | -------------------------------------------------------------------------------- /neowise/neowise/optimizers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class OptimizerHelpers: 5 | """ 6 | Optimizer Helpers 7 | Used to initiate the instance variables of nw.optimizers 8 | 9 | Arguments: 10 | alpha: Learning rate of the network (float) 11 | layers_arr: List containing the objects of nw.layers (List) 12 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 13 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 14 | t: Number of iterations elapsed (int) 15 | """ 16 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 17 | self.alpha, self.layers_arr, self.V_dict, self.S_dict, self.t = alpha, layers_arr, V_dict, S_dict, t 18 | 19 | 20 | class GradientDescent(OptimizerHelpers): 21 | """ 22 | Gradient Descent 23 | 24 | Arguments: 25 | alpha: Learning rate of the network (float) 26 | layers_arr: List containing the objects of nw.layers (List) 27 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 28 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 29 | t: Number of iterations elapsed (int) 30 | """ 31 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 32 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 33 | 34 | def __call__(self): 35 | for layers in self.layers_arr: 36 | layers.weights -= (self.alpha * layers.dW) 37 | layers.bias -= (self.alpha * layers.db) 38 | 39 | 40 | class Momentum(OptimizerHelpers): 41 | """ 42 | Gradient Descent with Momentum 43 | 44 | Arguments: 45 | alpha: Learning rate of the network (float) 46 | layers_arr: List containing the objects of nw.layers (List) 47 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 48 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 49 | t: Number of iterations elapsed (int) 50 | """ 51 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 52 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 53 | 54 | def __call__(self): 55 | beta1 = 0.9 56 | for h in range(1, len(self.layers_arr) + 1): 57 | self.V_dict["Vdw" + str(h)] = (beta1 * self.V_dict["Vdw" + str(h)]) + ( 58 | (1 - beta1) * self.layers_arr[h - 1].dW) 59 | self.V_dict["Vdb" + str(h)] = (beta1 * self.V_dict["Vdb" + str(h)]) + ( 60 | (1 - beta1) * self.layers_arr[h - 1].db) 61 | for g in range(1, len(self.layers_arr) + 1): 62 | self.layers_arr[g - 1].weights -= (self.alpha * self.V_dict["Vdw" + str(g)]) 63 | self.layers_arr[g - 1].bias -= (self.alpha * self.V_dict["Vdb" + str(g)]) 64 | 65 | 66 | class RMSProp(OptimizerHelpers): 67 | """ 68 | Root Mean Square Propagation 69 | 70 | Arguments: 71 | alpha: Learning rate of the network (float) 72 | layers_arr: List containing the objects of nw.layers (List) 73 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 74 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 75 | t: Number of iterations elapsed (int) 76 | """ 77 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 78 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 79 | 80 | def __call__(self): 81 | beta2 = 0.999 82 | epsilon = 1e-8 83 | for h in range(1, len(self.layers_arr) + 1): 84 | self.S_dict["Sdw" + str(h)] = (beta2 * self.S_dict["Sdw" + str(h)]) + ( 85 | (1 - beta2) * np.square(self.layers_arr[h - 1].dW)) 86 | self.S_dict["Sdb" + str(h)] = (beta2 * self.S_dict["Sdb" + str(h)]) + ( 87 | (1 - beta2) * np.square(self.layers_arr[h - 1].db)) 88 | for g in range(1, len(self.layers_arr) + 1): 89 | self.layers_arr[g - 1].weights -= ( 90 | (self.alpha * self.layers_arr[g - 1].dW) / (np.sqrt(self.S_dict["Sdw" + str(g)]) + epsilon)) 91 | self.layers_arr[g - 1].bias -= ( 92 | (self.alpha * self.layers_arr[g - 1].db) / (np.sqrt(self.S_dict["Sdb" + str(g)]) + epsilon)) 93 | 94 | 95 | class Adam(OptimizerHelpers): 96 | """ 97 | Adam 98 | 99 | Arguments: 100 | alpha: Learning rate of the network (float) 101 | layers_arr: List containing the objects of nw.layers (List) 102 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 103 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 104 | t: Number of iterations elapsed (int) 105 | """ 106 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 107 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 108 | 109 | def __call__(self): 110 | beta1 = 0.9 111 | beta2 = 0.999 112 | epsilon = 1e-8 113 | S_dict_corr = {} 114 | V_dict_corr = {} 115 | for h in range(1, len(self.layers_arr) + 1): 116 | self.V_dict["Vdw" + str(h)] = (beta1 * self.V_dict["Vdw" + str(h)]) + ( 117 | (1 - beta1) * self.layers_arr[h - 1].dW) 118 | self.V_dict["Vdb" + str(h)] = (beta1 * self.V_dict["Vdb" + str(h)]) + ( 119 | (1 - beta1) * self.layers_arr[h - 1].db) 120 | self.S_dict["Sdw" + str(h)] = (beta2 * self.S_dict["Sdw" + str(h)]) + ( 121 | (1 - beta2) * np.square(self.layers_arr[h - 1].dW)) 122 | self.S_dict["Sdb" + str(h)] = (beta2 * self.S_dict["Sdb" + str(h)]) + ( 123 | (1 - beta2) * np.square(self.layers_arr[h - 1].db)) 124 | for n in range(1, len(self.layers_arr) + 1): 125 | S_dict_corr["Sdw" + str(n)] = self.S_dict["Sdw" + str(n)] / (1 - np.power(beta2, self.t)) 126 | S_dict_corr["Sdb" + str(n)] = self.S_dict["Sdb" + str(n)] / (1 - np.power(beta2, self.t)) 127 | V_dict_corr["Vdw" + str(n)] = self.V_dict["Vdw" + str(n)] / (1 - np.power(beta1, self.t)) 128 | V_dict_corr["Vdb" + str(n)] = self.V_dict["Vdb" + str(n)] / (1 - np.power(beta1, self.t)) 129 | for g in range(1, len(self.layers_arr) + 1): 130 | self.layers_arr[g - 1].weights -= ( 131 | (self.alpha * V_dict_corr["Vdw" + str(g)]) / (np.sqrt(S_dict_corr["Sdw" + str(g)]) + epsilon)) 132 | self.layers_arr[g - 1].bias -= ( 133 | (self.alpha * V_dict_corr["Vdb" + str(g)]) / (np.sqrt(S_dict_corr["Sdb" + str(g)]) + epsilon)) 134 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/optimizers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class OptimizerHelpers: 5 | """ 6 | Optimizer Helpers 7 | Used to initiate the instance variables of nw.optimizers 8 | 9 | Arguments: 10 | alpha: Learning rate of the network (float) 11 | layers_arr: List containing the objects of nw.layers (List) 12 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 13 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 14 | t: Number of iterations elapsed (int) 15 | """ 16 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 17 | self.alpha, self.layers_arr, self.V_dict, self.S_dict, self.t = alpha, layers_arr, V_dict, S_dict, t 18 | 19 | 20 | class GradientDescent(OptimizerHelpers): 21 | """ 22 | Gradient Descent 23 | 24 | Arguments: 25 | alpha: Learning rate of the network (float) 26 | layers_arr: List containing the objects of nw.layers (List) 27 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 28 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 29 | t: Number of iterations elapsed (int) 30 | """ 31 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 32 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 33 | 34 | def __call__(self): 35 | for layers in self.layers_arr: 36 | layers.weights -= (self.alpha * layers.dW) 37 | layers.bias -= (self.alpha * layers.db) 38 | 39 | 40 | class Momentum(OptimizerHelpers): 41 | """ 42 | Gradient Descent with Momentum 43 | 44 | Arguments: 45 | alpha: Learning rate of the network (float) 46 | layers_arr: List containing the objects of nw.layers (List) 47 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 48 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 49 | t: Number of iterations elapsed (int) 50 | """ 51 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 52 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 53 | 54 | def __call__(self): 55 | beta1 = 0.9 56 | for h in range(1, len(self.layers_arr) + 1): 57 | self.V_dict["Vdw" + str(h)] = (beta1 * self.V_dict["Vdw" + str(h)]) + ( 58 | (1 - beta1) * self.layers_arr[h - 1].dW) 59 | self.V_dict["Vdb" + str(h)] = (beta1 * self.V_dict["Vdb" + str(h)]) + ( 60 | (1 - beta1) * self.layers_arr[h - 1].db) 61 | for g in range(1, len(self.layers_arr) + 1): 62 | self.layers_arr[g - 1].weights -= (self.alpha * self.V_dict["Vdw" + str(g)]) 63 | self.layers_arr[g - 1].bias -= (self.alpha * self.V_dict["Vdb" + str(g)]) 64 | 65 | 66 | class RMSProp(OptimizerHelpers): 67 | """ 68 | Root Mean Square Propagation 69 | 70 | Arguments: 71 | alpha: Learning rate of the network (float) 72 | layers_arr: List containing the objects of nw.layers (List) 73 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 74 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 75 | t: Number of iterations elapsed (int) 76 | """ 77 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 78 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 79 | 80 | def __call__(self): 81 | beta2 = 0.999 82 | epsilon = 1e-8 83 | for h in range(1, len(self.layers_arr) + 1): 84 | self.S_dict["Sdw" + str(h)] = (beta2 * self.S_dict["Sdw" + str(h)]) + ( 85 | (1 - beta2) * np.square(self.layers_arr[h - 1].dW)) 86 | self.S_dict["Sdb" + str(h)] = (beta2 * self.S_dict["Sdb" + str(h)]) + ( 87 | (1 - beta2) * np.square(self.layers_arr[h - 1].db)) 88 | for g in range(1, len(self.layers_arr) + 1): 89 | self.layers_arr[g - 1].weights -= ( 90 | (self.alpha * self.layers_arr[g - 1].dW) / (np.sqrt(self.S_dict["Sdw" + str(g)]) + epsilon)) 91 | self.layers_arr[g - 1].bias -= ( 92 | (self.alpha * self.layers_arr[g - 1].db) / (np.sqrt(self.S_dict["Sdb" + str(g)]) + epsilon)) 93 | 94 | 95 | class Adam(OptimizerHelpers): 96 | """ 97 | Adam 98 | 99 | Arguments: 100 | alpha: Learning rate of the network (float) 101 | layers_arr: List containing the objects of nw.layers (List) 102 | V_dict: Dictionary containing the moving averages of the gradient of weights of all the layers (nd-array) 103 | S_dict: Dictionary containing the moving averages of the squared gradient of weights of all the layers (nd-array) 104 | t: Number of iterations elapsed (int) 105 | """ 106 | def __init__(self, alpha, layers_arr, V_dict, S_dict, t): 107 | OptimizerHelpers.__init__(self, alpha, layers_arr, V_dict, S_dict, t) 108 | 109 | def __call__(self): 110 | beta1 = 0.9 111 | beta2 = 0.999 112 | epsilon = 1e-8 113 | S_dict_corr = {} 114 | V_dict_corr = {} 115 | for h in range(1, len(self.layers_arr) + 1): 116 | self.V_dict["Vdw" + str(h)] = (beta1 * self.V_dict["Vdw" + str(h)]) + ( 117 | (1 - beta1) * self.layers_arr[h - 1].dW) 118 | self.V_dict["Vdb" + str(h)] = (beta1 * self.V_dict["Vdb" + str(h)]) + ( 119 | (1 - beta1) * self.layers_arr[h - 1].db) 120 | self.S_dict["Sdw" + str(h)] = (beta2 * self.S_dict["Sdw" + str(h)]) + ( 121 | (1 - beta2) * np.square(self.layers_arr[h - 1].dW)) 122 | self.S_dict["Sdb" + str(h)] = (beta2 * self.S_dict["Sdb" + str(h)]) + ( 123 | (1 - beta2) * np.square(self.layers_arr[h - 1].db)) 124 | for n in range(1, len(self.layers_arr) + 1): 125 | S_dict_corr["Sdw" + str(n)] = self.S_dict["Sdw" + str(n)] / (1 - np.power(beta2, self.t)) 126 | S_dict_corr["Sdb" + str(n)] = self.S_dict["Sdb" + str(n)] / (1 - np.power(beta2, self.t)) 127 | V_dict_corr["Vdw" + str(n)] = self.V_dict["Vdw" + str(n)] / (1 - np.power(beta1, self.t)) 128 | V_dict_corr["Vdb" + str(n)] = self.V_dict["Vdb" + str(n)] / (1 - np.power(beta1, self.t)) 129 | for g in range(1, len(self.layers_arr) + 1): 130 | self.layers_arr[g - 1].weights -= ( 131 | (self.alpha * V_dict_corr["Vdw" + str(g)]) / (np.sqrt(S_dict_corr["Sdw" + str(g)]) + epsilon)) 132 | self.layers_arr[g - 1].bias -= ( 133 | (self.alpha * V_dict_corr["Vdb" + str(g)]) / (np.sqrt(S_dict_corr["Sdb" + str(g)]) + epsilon)) 134 | -------------------------------------------------------------------------------- /neowise/neowise/plots.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | class StaticPlotHelpers: 6 | """ 7 | Static Plot Helpers 8 | Used to initiate the instance variables of Static Plots 9 | 10 | Arguments: 11 | costs_tr: Array of costs of training data through the entire training duration of the network 12 | costs_cv: Array of costs of cross validation data through the entire duration of the network 13 | accu_tr_arr: Array containing the accuracy of the training data of the network through the entire training duration 14 | accu_cv_arr: Array containing the accuracy of the cross validation data of the network through the entire training duration 15 | """ 16 | def __init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr): 17 | self.costs_tr, self.costs_cv = costs_tr, costs_cv 18 | self.accu_tr_arr, self.accu_cv_arr = accu_tr_arr, accu_cv_arr 19 | 20 | 21 | class AnimatePlotHelpers: 22 | """ 23 | Animate Plot Helpers 24 | Used to initiate the instance variables of Animated Plots 25 | 26 | Arguments: 27 | x_ax: X axis data for plotting 28 | y_ax: Y axis data for plotting 29 | X_lab: Label of the X axis of the plot 30 | Y_lab: Label of the Y axis of the plot 31 | plot_title: Title of the plot 32 | leg: Legend of the plot 33 | loca: Location of legends 34 | plot_col: Color of the plotted line 35 | direc: Directory in which the images should be saved 36 | freq: Frequency with which the plot should update 37 | """ 38 | def __init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq): 39 | self.x_ax, self.y_ax, self.X_lab, self.Y_lab = x_ax, y_ax, X_lab, Y_lab 40 | self.plot_title, self.leg, self.loca, self.plot_col, self.direc, self.freq = plot_title, leg, loca, plot_col, direc, freq 41 | 42 | 43 | class PlotCostStatic(StaticPlotHelpers): 44 | """ 45 | Plot Cost Static 46 | Plots the Cost vs Number of iterations Plot 47 | 48 | Arguments: 49 | costs_tr: Array of costs of training data through the entire training duration of the network 50 | costs_cv: Array of costs of cross validation data through the entire duration of the network 51 | accu_tr_arr: Array containing the accuracy of the training data of the network through the entire training duration 52 | accu_cv_arr: Array containing the accuracy of the cross validation data of the network through the entire training duration 53 | """ 54 | def __init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr): 55 | StaticPlotHelpers.__init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr) 56 | 57 | def __call__(self): 58 | itera = np.arange(1, len(self.costs_tr) + 1, 1) 59 | plt.xlabel('Number of Iterations') 60 | plt.ylabel('Cost') 61 | plt.title('Cost Function Variation') 62 | plt.plot(itera, self.costs_tr, color='c', linewidth=2) 63 | if len(self.costs_cv) != 0: 64 | plt.plot(itera, self.costs_cv, color='#9ef705', linewidth=2) 65 | plt.legend(["Train", "Cross Val"], loc='upper right') 66 | 67 | 68 | class PlotTrCvStatic(StaticPlotHelpers): 69 | """ 70 | Plot Train CrossVal Static 71 | Plots the accuracies of train and cross val vs number of iterations 72 | 73 | Arguments: 74 | costs_tr: Array of costs of training data through the entire training duration of the network 75 | costs_cv: Array of costs of cross validation data through the entire duration of the network 76 | accu_tr_arr: Array containing the accuracy of the training data of the network through the entire training duration 77 | accu_cv_arr: Array containing the accuracy of the cross validation data of the network through the entire training duration 78 | """ 79 | def __init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr): 80 | StaticPlotHelpers.__init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr) 81 | 82 | def __call__(self): 83 | itera = np.arange(1, len(self.costs_tr) + 1, 1) 84 | plt.xlabel('Number of Iterations') 85 | plt.ylabel('Accuracy') 86 | plt.title('Train - Cross Val Accuracy Curve') 87 | plt.plot(itera, self.accu_tr_arr, color='m', linewidth=2) 88 | if len(self.accu_cv_arr) != 0: 89 | plt.plot(itera, self.accu_cv_arr, color='r', linewidth=2) 90 | plt.legend(["Train", "Cross Val"], loc='lower right') 91 | 92 | 93 | class AnimatePlot(AnimatePlotHelpers): 94 | """ 95 | Animate Plot 96 | Animates the plot which has only one argument for y axis 97 | 98 | Arguments: 99 | x_ax: X axis data for plotting 100 | y_ax: Y axis data for plotting 101 | X_lab: Label of the X axis of the plot 102 | Y_lab: Label of the Y axis of the plot 103 | plot_title: Title of the plot 104 | leg: Legend of the plot 105 | loca: Location of legends 106 | plot_col: Color of the plotted line 107 | direc: Directory in which the images should be saved 108 | freq: Frequency with which the plot should update 109 | """ 110 | def __init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq): 111 | AnimatePlotHelpers.__init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq) 112 | 113 | def __call__(self): 114 | y_vals = self.y_ax 115 | x_vals = self.x_ax 116 | l = 0 117 | for k in range(1, len(x_vals) + 1): 118 | plt.xlabel(self.X_lab) 119 | plt.ylabel(self.Y_lab) 120 | plt.title(self.plot_title) 121 | plt.legend([self.leg], loc=self.loca) 122 | plt.plot(x_vals[0:l], y_vals[0:l], color=self.plot_col) 123 | l = l + 1 124 | if k % self.freq == 0: 125 | plt.savefig(self.direc + 'plot{}.png'.format(k // self.freq)) 126 | return 127 | 128 | 129 | class AnimatePlotMulti(AnimatePlotHelpers): 130 | """ 131 | Animate Plot Multi 132 | Animates the plot with more than one arguments for Y axis 133 | 134 | Arguments: 135 | x_ax: X axis data for plotting 136 | y_ax: Y axis data for plotting 137 | X_lab: Label of the X axis of the plot 138 | Y_lab: Label of the Y axis of the plot 139 | plot_title: Title of the plot 140 | leg: Legend of the plot 141 | loca: Location of legends 142 | plot_col: Color of the plotted line 143 | direc: Directory in which the images should be saved 144 | freq: Frequency with which the plot should update 145 | """ 146 | def __init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq): 147 | AnimatePlotHelpers.__init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq) 148 | 149 | def __call__(self): 150 | x_vals = {} 151 | y_vals = {} 152 | for m in range(0, len(self.x_ax)): 153 | x_vals["X" + str(m)] = self.x_ax[m] 154 | for g in range(0, len(self.y_ax)): 155 | y_vals["Y" + str(g)] = self.y_ax[g] 156 | for h in range(0, len(self.leg)): 157 | plt.plot([], [], color=self.plot_col[h], label=self.leg[h]) 158 | plt.legend(loc=self.loca) 159 | l = 0 160 | for k in range(1, len(self.x_ax[0]) + 1): 161 | plt.xlabel(self.X_lab) 162 | plt.ylabel(self.Y_lab) 163 | plt.title(self.plot_title) 164 | for d in range(0, len(self.x_ax)): 165 | if len(x_vals["X" + str(d)]) != 0: 166 | plt.plot(x_vals["X" + str(d)][0:l], y_vals["Y" + str(d)][0:l], color=self.plot_col[d]) 167 | l = l + 1 168 | if k % self.freq == 0: 169 | plt.savefig(self.direc + 'plot{}.png'.format(k // self.freq)) 170 | return 171 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/plots.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | class StaticPlotHelpers: 6 | """ 7 | Static Plot Helpers 8 | Used to initiate the instance variables of Static Plots 9 | 10 | Arguments: 11 | costs_tr: Array of costs of training data through the entire training duration of the network 12 | costs_cv: Array of costs of cross validation data through the entire duration of the network 13 | accu_tr_arr: Array containing the accuracy of the training data of the network through the entire training duration 14 | accu_cv_arr: Array containing the accuracy of the cross validation data of the network through the entire training duration 15 | """ 16 | def __init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr): 17 | self.costs_tr, self.costs_cv = costs_tr, costs_cv 18 | self.accu_tr_arr, self.accu_cv_arr = accu_tr_arr, accu_cv_arr 19 | 20 | 21 | class AnimatePlotHelpers: 22 | """ 23 | Animate Plot Helpers 24 | Used to initiate the instance variables of Animated Plots 25 | 26 | Arguments: 27 | x_ax: X axis data for plotting 28 | y_ax: Y axis data for plotting 29 | X_lab: Label of the X axis of the plot 30 | Y_lab: Label of the Y axis of the plot 31 | plot_title: Title of the plot 32 | leg: Legend of the plot 33 | loca: Location of legends 34 | plot_col: Color of the plotted line 35 | direc: Directory in which the images should be saved 36 | freq: Frequency with which the plot should update 37 | """ 38 | def __init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq): 39 | self.x_ax, self.y_ax, self.X_lab, self.Y_lab = x_ax, y_ax, X_lab, Y_lab 40 | self.plot_title, self.leg, self.loca, self.plot_col, self.direc, self.freq = plot_title, leg, loca, plot_col, direc, freq 41 | 42 | 43 | class PlotCostStatic(StaticPlotHelpers): 44 | """ 45 | Plot Cost Static 46 | Plots the Cost vs Number of iterations Plot 47 | 48 | Arguments: 49 | costs_tr: Array of costs of training data through the entire training duration of the network 50 | costs_cv: Array of costs of cross validation data through the entire duration of the network 51 | accu_tr_arr: Array containing the accuracy of the training data of the network through the entire training duration 52 | accu_cv_arr: Array containing the accuracy of the cross validation data of the network through the entire training duration 53 | """ 54 | def __init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr): 55 | StaticPlotHelpers.__init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr) 56 | 57 | def __call__(self): 58 | itera = np.arange(1, len(self.costs_tr) + 1, 1) 59 | plt.xlabel('Number of Iterations') 60 | plt.ylabel('Cost') 61 | plt.title('Cost Function Variation') 62 | plt.plot(itera, self.costs_tr, color='c', linewidth=2) 63 | if len(self.costs_cv) != 0: 64 | plt.plot(itera, self.costs_cv, color='#9ef705', linewidth=2) 65 | plt.legend(["Train", "Cross Val"], loc='upper right') 66 | 67 | 68 | class PlotTrCvStatic(StaticPlotHelpers): 69 | """ 70 | Plot Train CrossVal Static 71 | Plots the accuracies of train and cross val vs number of iterations 72 | 73 | Arguments: 74 | costs_tr: Array of costs of training data through the entire training duration of the network 75 | costs_cv: Array of costs of cross validation data through the entire duration of the network 76 | accu_tr_arr: Array containing the accuracy of the training data of the network through the entire training duration 77 | accu_cv_arr: Array containing the accuracy of the cross validation data of the network through the entire training duration 78 | """ 79 | def __init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr): 80 | StaticPlotHelpers.__init__(self, costs_tr, costs_cv, accu_tr_arr, accu_cv_arr) 81 | 82 | def __call__(self): 83 | itera = np.arange(1, len(self.costs_tr) + 1, 1) 84 | plt.xlabel('Number of Iterations') 85 | plt.ylabel('Accuracy') 86 | plt.title('Train - Cross Val Accuracy Curve') 87 | plt.plot(itera, self.accu_tr_arr, color='m', linewidth=2) 88 | if len(self.accu_cv_arr) != 0: 89 | plt.plot(itera, self.accu_cv_arr, color='r', linewidth=2) 90 | plt.legend(["Train", "Cross Val"], loc='lower right') 91 | 92 | 93 | class AnimatePlot(AnimatePlotHelpers): 94 | """ 95 | Animate Plot 96 | Animates the plot which has only one argument for y axis 97 | 98 | Arguments: 99 | x_ax: X axis data for plotting 100 | y_ax: Y axis data for plotting 101 | X_lab: Label of the X axis of the plot 102 | Y_lab: Label of the Y axis of the plot 103 | plot_title: Title of the plot 104 | leg: Legend of the plot 105 | loca: Location of legends 106 | plot_col: Color of the plotted line 107 | direc: Directory in which the images should be saved 108 | freq: Frequency with which the plot should update 109 | """ 110 | def __init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq): 111 | AnimatePlotHelpers.__init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq) 112 | 113 | def __call__(self): 114 | y_vals = self.y_ax 115 | x_vals = self.x_ax 116 | l = 0 117 | for k in range(1, len(x_vals) + 1): 118 | plt.xlabel(self.X_lab) 119 | plt.ylabel(self.Y_lab) 120 | plt.title(self.plot_title) 121 | plt.legend([self.leg], loc=self.loca) 122 | plt.plot(x_vals[0:l], y_vals[0:l], color=self.plot_col) 123 | l = l + 1 124 | if k % self.freq == 0: 125 | plt.savefig(self.direc + 'plot{}.png'.format(k // self.freq)) 126 | return 127 | 128 | 129 | class AnimatePlotMulti(AnimatePlotHelpers): 130 | """ 131 | Animate Plot Multi 132 | Animates the plot with more than one arguments for Y axis 133 | 134 | Arguments: 135 | x_ax: X axis data for plotting 136 | y_ax: Y axis data for plotting 137 | X_lab: Label of the X axis of the plot 138 | Y_lab: Label of the Y axis of the plot 139 | plot_title: Title of the plot 140 | leg: Legend of the plot 141 | loca: Location of legends 142 | plot_col: Color of the plotted line 143 | direc: Directory in which the images should be saved 144 | freq: Frequency with which the plot should update 145 | """ 146 | def __init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq): 147 | AnimatePlotHelpers.__init__(self, x_ax, y_ax, X_lab, Y_lab, plot_title, leg, loca, plot_col, direc, freq) 148 | 149 | def __call__(self): 150 | x_vals = {} 151 | y_vals = {} 152 | for m in range(0, len(self.x_ax)): 153 | x_vals["X" + str(m)] = self.x_ax[m] 154 | for g in range(0, len(self.y_ax)): 155 | y_vals["Y" + str(g)] = self.y_ax[g] 156 | for h in range(0, len(self.leg)): 157 | plt.plot([], [], color=self.plot_col[h], label=self.leg[h]) 158 | plt.legend(loc=self.loca) 159 | l = 0 160 | for k in range(1, len(self.x_ax[0]) + 1): 161 | plt.xlabel(self.X_lab) 162 | plt.ylabel(self.Y_lab) 163 | plt.title(self.plot_title) 164 | for d in range(0, len(self.x_ax)): 165 | if len(x_vals["X" + str(d)]) != 0: 166 | plt.plot(x_vals["X" + str(d)][0:l], y_vals["Y" + str(d)][0:l], color=self.plot_col[d]) 167 | l = l + 1 168 | if k % self.freq == 0: 169 | plt.savefig(self.direc + 'plot{}.png'.format(k // self.freq)) 170 | return 171 | -------------------------------------------------------------------------------- /neowise/neowise/functional.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class Predict: 5 | """ 6 | Predict 7 | Used to calculate the predictions of the network based on the activation of the output layer 8 | of the network for Binary Classification tasks 9 | 10 | Arguments: 11 | A: Activations of the output layer (nd-array) 12 | threshold: Value between 0 and 1, above which the prediction is 1 and below which is 0 (default=0.5) (float) 13 | Returns: 14 | predictions: Predictions made based on the activations of the output layer (nd-array) 15 | """ 16 | def __init__(self, A, threshold=0.5): 17 | self.A = A 18 | self.threshold = threshold 19 | 20 | def __call__(self): 21 | predictions = np.zeros((self.A.shape)) 22 | for g in range(0, self.A.shape[1]): 23 | if self.A[:, g] >= self.threshold: 24 | predictions[:, g] = 1 25 | return predictions 26 | 27 | 28 | class PredictMulti: 29 | """ 30 | Predict Multi 31 | Used to calculate the predictions of the network based on the activation of the output layer 32 | of the network for Multi Class Classification tasks 33 | 34 | Arguments: 35 | A: Activations of the output layer of the network (nd-array) 36 | Returns: 37 | predictions: Predictions made based on the activations of the output layer (nd-array) 38 | """ 39 | def __init__(self, A): 40 | self.A = A 41 | 42 | def __call__(self): 43 | predictions_multi = np.zeros(self.A.shape) 44 | for v in range(0, self.A.shape[1]): 45 | temp = max(self.A[:, v]) 46 | for w in range(0, self.A.shape[0]): 47 | if self.A[w, v] == temp: 48 | predictions_multi[w, v] = 1 49 | else: 50 | predictions_multi[w, v] = 0 51 | return predictions_multi 52 | 53 | 54 | class Evaluate: 55 | """ 56 | Evaluate 57 | Calculates the accuracy of the network, i.e gives the percentage of the ratio between the data that is correctly 58 | classified to the total number of examples of data for Binary Classification tasks 59 | 60 | Arguments: 61 | y: Output labels of the train/test data (nd-array) 62 | preds: Predictions of the network (nd-array) 63 | Returns: 64 | accuracy: percentage of data correctly predicted when compared to the labels (float) 65 | """ 66 | def __init__(self, y, preds): 67 | self.y, self.preds = y, preds 68 | 69 | def __call__(self): 70 | accuracy = float(np.mean(self.preds == self.y, axis=1) * 100) 71 | return accuracy 72 | 73 | 74 | class EvaluateMulti: 75 | """ 76 | Evaluate Multi 77 | Calculates the accuracy of the network, i.e gives the percentage of the ratio between the data that is correctly 78 | classified to the total number of examples of data for Multi Class Classification tasks 79 | 80 | Arguments: 81 | y: Output labels of the train/test data (nd-array) 82 | preds: Predictions of the network (nd-array) 83 | Returns: 84 | accuracy: percentage of data correctly predicted when compared to the labels (float) 85 | """ 86 | def __init__(self, y, preds): 87 | self.y, self.preds = y, preds 88 | 89 | def __call__(self): 90 | ones_array = np.ones(self.preds.shape) 91 | temp1 = self.preds == ones_array 92 | ind = [] 93 | for gee in range(0, temp1.shape[1]): 94 | for jee in range(0, temp1.shape[0]): 95 | if temp1[jee, gee] == True: 96 | ind.append(jee) 97 | ind_arr = np.array(ind) 98 | y_list = [] 99 | for gee in range(0, self.y.shape[1]): 100 | for jee in range(0, self.y.shape[0]): 101 | if self.y[jee, gee] == 1: 102 | y_list.append(jee) 103 | y_arr = np.array(y_list) 104 | accuracy = float(np.mean(ind_arr == y_arr.T)) * 100 105 | return accuracy 106 | 107 | 108 | class PrecisionRecall: 109 | """ 110 | Precision Recall 111 | Calculates Precision, recall and F1 score of the network for Binary Classification tasks 112 | 113 | Arguments: 114 | A: Predictions of the network (nd-array) 115 | y: Labelled outputs of train/test set (nd-array) 116 | Returns: 117 | Precision, Recall and F1 score: All values between 0 and 1 (float) 118 | """ 119 | def __init__(self, A, y): 120 | self.A, self.y = A, y 121 | 122 | def __call__(self): 123 | tp = 0 124 | fp = 0 125 | fn = 0 126 | for i in range(0, self.y.shape[1]): 127 | if ((self.A[0, i] == 1) and (self.y[0, i] == 1)): 128 | tp = tp + 1 129 | if ((self.A[0, i] == 1) and (self.y[0, i] == 0)): 130 | fp = fp + 1 131 | if (self.A[0, i] == 0) and (self.y[0, i] == 1): 132 | fn = fn + 1 133 | prec = tp / (tp + fp) 134 | rec = tp / (tp + fn) 135 | f1 = (2 * prec * rec) / (prec + rec) 136 | return prec, rec, f1 137 | 138 | 139 | class PrecisionRecallMulti: 140 | """ 141 | Precision Recall Multi 142 | Calculates Precision, recall and F1 score of the network for each of the classes of 143 | Multi Class Classification tasks 144 | 145 | Arguments: 146 | A: Predictions of the network (nd-array) 147 | y: Labelled outputs of train/test set (nd-array) 148 | Returns: 149 | Precision, Recall and F1 score: All values between 0 and 1 for all of the classes in Multi Class Classification 150 | """ 151 | def __init__(self, A, y): 152 | self.A, self.y = A, y 153 | 154 | def __call__(self): 155 | epsilon = 1e-5 156 | tp_multi = {} 157 | fp_multi = {} 158 | fn_multi = {} 159 | prec_multi = {} 160 | rec_multi = {} 161 | f1_multi = {} 162 | num_classes = self.y.shape[0] 163 | for r in range(0, num_classes): 164 | tp_multi["class" + str(r)] = 0 165 | fp_multi["class" + str(r)] = 0 166 | fn_multi["class" + str(r)] = 0 167 | for c in range(0, self.y.shape[1]): 168 | for g in range(0, self.y.shape[0]): 169 | if ((self.A[g, c] == 1) and (self.y[g, c] == 1)): 170 | tp_multi["class" + str(g)] = tp_multi["class" + str(g)] + 1 171 | if ((self.A[g, c] == 1) and (self.y[g, c] == 0)): 172 | fp_multi["class" + str(g)] = fp_multi["class" + str(g)] + 1 173 | if ((self.A[g, c] == 0) and (self.y[g, c] == 1)): 174 | fn_multi["class" + str(g)] = fn_multi["class" + str(g)] + 1 175 | for n in range(0, num_classes): 176 | prec_multi["class" + str(n)] = tp_multi["class" + str(n)] / ( 177 | tp_multi["class" + str(n)] + fp_multi["class" + str(n)] + epsilon) 178 | rec_multi["class" + str(n)] = tp_multi["class" + str(n)] / ( 179 | tp_multi["class" + str(n)] + fn_multi["class" + str(n)] + epsilon) 180 | f1_multi["class" + str(n)] = (2 * prec_multi["class" + str(n)] * rec_multi["class" + str(n)]) / ( 181 | prec_multi["class" + str(n)] + rec_multi["class" + str(n)] + epsilon) 182 | return prec_multi, rec_multi, f1_multi 183 | 184 | 185 | class GradL1Reg: 186 | """ 187 | Grad L1 Reg 188 | Calculates the derivative of the weights of the array to itself 189 | 190 | Arguments: 191 | layers_arr: List containing the objects of nw.layers classes 192 | """ 193 | def __init__(self, layers_arr): 194 | self.layers_arr = layers_arr 195 | 196 | def __call__(self): 197 | for layer in self.layers_arr: 198 | layer.grad_L1 = np.zeros(layer.weights.shape) 199 | for p in range(0, layer.weights.shape[0]): 200 | for n in range(0, layer.weights.shape[1]): 201 | if layer.weights[p, n] > 0: 202 | layer.grad_L1[p, n] = 1 203 | else: 204 | layer.grad_L1[p, n] = -1 205 | 206 | 207 | class CreateMiniBatches: 208 | """ 209 | Create Mini Batches 210 | Creates mini batches for training with the specified mini-batch size 211 | 212 | Arguments: 213 | X: Training data (nd-array) 214 | y: Training data outputs (nd-array) 215 | mb_size: Number of training examples in each mini-batch (int) 216 | Returns: 217 | mini_batch: Dictionary containing all the mini-batches (dict) 218 | num: Number of mini-batches created with the specified mb_size (int) 219 | """ 220 | def __init__(self, X, y, mb_size): 221 | self.X, self.y, self.mb_size = X, y, mb_size 222 | 223 | def __call__(self): 224 | m_ex = self.y.shape[1] 225 | mini_batch = {} 226 | num = m_ex // self.mb_size 227 | if m_ex % self.mb_size != 0: 228 | f = 0 229 | for p in range(0, num): 230 | mini_batch["MB_X" + str(p)] = self.X[f:(f + self.mb_size), :] 231 | mini_batch["MB_Y" + str(p)] = self.y[:, f:(f + self.mb_size)] 232 | f = f + self.mb_size 233 | mini_batch["MB_X" + str(num)] = self.X[f:m_ex, :] 234 | mini_batch["MB_Y" + str(num)] = self.y[:, f:m_ex] 235 | return mini_batch, num 236 | else: 237 | f = 0 238 | for p in range(0, num - 1): 239 | mini_batch["MB_X" + str(p)] = self.X[f:(f + self.mb_size), :] 240 | mini_batch["MB_Y" + str(p)] = self.y[:, f:(f + self.mb_size)] 241 | f = f + self.mb_size 242 | mini_batch["MB_X" + str(num - 1)] = self.X[f:m_ex, :] 243 | mini_batch["MB_Y" + str(num - 1)] = self.y[:, f:m_ex] 244 | return mini_batch, num - 1 245 | -------------------------------------------------------------------------------- /neowise/build/lib/neowise/functional.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class Predict: 5 | """ 6 | Predict 7 | Used to calculate the predictions of the network based on the activation of the output layer 8 | of the network for Binary Classification tasks 9 | 10 | Arguments: 11 | A: Activations of the output layer (nd-array) 12 | threshold: Value between 0 and 1, above which the prediction is 1 and below which is 0 (default=0.5) (float) 13 | Returns: 14 | predictions: Predictions made based on the activations of the output layer (nd-array) 15 | """ 16 | def __init__(self, A, threshold=0.5): 17 | self.A = A 18 | self.threshold = threshold 19 | 20 | def __call__(self): 21 | predictions = np.zeros((self.A.shape)) 22 | for g in range(0, self.A.shape[1]): 23 | if self.A[:, g] >= self.threshold: 24 | predictions[:, g] = 1 25 | return predictions 26 | 27 | 28 | class PredictMulti: 29 | """ 30 | Predict Multi 31 | Used to calculate the predictions of the network based on the activation of the output layer 32 | of the network for Multi Class Classification tasks 33 | 34 | Arguments: 35 | A: Activations of the output layer of the network (nd-array) 36 | Returns: 37 | predictions: Predictions made based on the activations of the output layer (nd-array) 38 | """ 39 | def __init__(self, A): 40 | self.A = A 41 | 42 | def __call__(self): 43 | predictions_multi = np.zeros(self.A.shape) 44 | for v in range(0, self.A.shape[1]): 45 | temp = max(self.A[:, v]) 46 | for w in range(0, self.A.shape[0]): 47 | if self.A[w, v] == temp: 48 | predictions_multi[w, v] = 1 49 | else: 50 | predictions_multi[w, v] = 0 51 | return predictions_multi 52 | 53 | 54 | class Evaluate: 55 | """ 56 | Evaluate 57 | Calculates the accuracy of the network, i.e gives the percentage of the ratio between the data that is correctly 58 | classified to the total number of examples of data for Binary Classification tasks 59 | 60 | Arguments: 61 | y: Output labels of the train/test data (nd-array) 62 | preds: Predictions of the network (nd-array) 63 | Returns: 64 | accuracy: percentage of data correctly predicted when compared to the labels (float) 65 | """ 66 | def __init__(self, y, preds): 67 | self.y, self.preds = y, preds 68 | 69 | def __call__(self): 70 | accuracy = float(np.mean(self.preds == self.y, axis=1) * 100) 71 | return accuracy 72 | 73 | 74 | class EvaluateMulti: 75 | """ 76 | Evaluate Multi 77 | Calculates the accuracy of the network, i.e gives the percentage of the ratio between the data that is correctly 78 | classified to the total number of examples of data for Multi Class Classification tasks 79 | 80 | Arguments: 81 | y: Output labels of the train/test data (nd-array) 82 | preds: Predictions of the network (nd-array) 83 | Returns: 84 | accuracy: percentage of data correctly predicted when compared to the labels (float) 85 | """ 86 | def __init__(self, y, preds): 87 | self.y, self.preds = y, preds 88 | 89 | def __call__(self): 90 | ones_array = np.ones(self.preds.shape) 91 | temp1 = self.preds == ones_array 92 | ind = [] 93 | for gee in range(0, temp1.shape[1]): 94 | for jee in range(0, temp1.shape[0]): 95 | if temp1[jee, gee] == True: 96 | ind.append(jee) 97 | ind_arr = np.array(ind) 98 | y_list = [] 99 | for gee in range(0, self.y.shape[1]): 100 | for jee in range(0, self.y.shape[0]): 101 | if self.y[jee, gee] == 1: 102 | y_list.append(jee) 103 | y_arr = np.array(y_list) 104 | accuracy = float(np.mean(ind_arr == y_arr.T)) * 100 105 | return accuracy 106 | 107 | 108 | class PrecisionRecall: 109 | """ 110 | Precision Recall 111 | Calculates Precision, recall and F1 score of the network for Binary Classification tasks 112 | 113 | Arguments: 114 | A: Predictions of the network (nd-array) 115 | y: Labelled outputs of train/test set (nd-array) 116 | Returns: 117 | Precision, Recall and F1 score: All values between 0 and 1 (float) 118 | """ 119 | def __init__(self, A, y): 120 | self.A, self.y = A, y 121 | 122 | def __call__(self): 123 | tp = 0 124 | fp = 0 125 | fn = 0 126 | for i in range(0, self.y.shape[1]): 127 | if ((self.A[0, i] == 1) and (self.y[0, i] == 1)): 128 | tp = tp + 1 129 | if ((self.A[0, i] == 1) and (self.y[0, i] == 0)): 130 | fp = fp + 1 131 | if (self.A[0, i] == 0) and (self.y[0, i] == 1): 132 | fn = fn + 1 133 | prec = tp / (tp + fp) 134 | rec = tp / (tp + fn) 135 | f1 = (2 * prec * rec) / (prec + rec) 136 | return prec, rec, f1 137 | 138 | 139 | class PrecisionRecallMulti: 140 | """ 141 | Precision Recall Multi 142 | Calculates Precision, recall and F1 score of the network for each of the classes of 143 | Multi Class Classification tasks 144 | 145 | Arguments: 146 | A: Predictions of the network (nd-array) 147 | y: Labelled outputs of train/test set (nd-array) 148 | Returns: 149 | Precision, Recall and F1 score: All values between 0 and 1 for all of the classes in Multi Class Classification 150 | """ 151 | def __init__(self, A, y): 152 | self.A, self.y = A, y 153 | 154 | def __call__(self): 155 | epsilon = 1e-5 156 | tp_multi = {} 157 | fp_multi = {} 158 | fn_multi = {} 159 | prec_multi = {} 160 | rec_multi = {} 161 | f1_multi = {} 162 | num_classes = self.y.shape[0] 163 | for r in range(0, num_classes): 164 | tp_multi["class" + str(r)] = 0 165 | fp_multi["class" + str(r)] = 0 166 | fn_multi["class" + str(r)] = 0 167 | for c in range(0, self.y.shape[1]): 168 | for g in range(0, self.y.shape[0]): 169 | if ((self.A[g, c] == 1) and (self.y[g, c] == 1)): 170 | tp_multi["class" + str(g)] = tp_multi["class" + str(g)] + 1 171 | if ((self.A[g, c] == 1) and (self.y[g, c] == 0)): 172 | fp_multi["class" + str(g)] = fp_multi["class" + str(g)] + 1 173 | if ((self.A[g, c] == 0) and (self.y[g, c] == 1)): 174 | fn_multi["class" + str(g)] = fn_multi["class" + str(g)] + 1 175 | for n in range(0, num_classes): 176 | prec_multi["class" + str(n)] = tp_multi["class" + str(n)] / ( 177 | tp_multi["class" + str(n)] + fp_multi["class" + str(n)] + epsilon) 178 | rec_multi["class" + str(n)] = tp_multi["class" + str(n)] / ( 179 | tp_multi["class" + str(n)] + fn_multi["class" + str(n)] + epsilon) 180 | f1_multi["class" + str(n)] = (2 * prec_multi["class" + str(n)] * rec_multi["class" + str(n)]) / ( 181 | prec_multi["class" + str(n)] + rec_multi["class" + str(n)] + epsilon) 182 | return prec_multi, rec_multi, f1_multi 183 | 184 | 185 | class GradL1Reg: 186 | """ 187 | Grad L1 Reg 188 | Calculates the derivative of the weights of the array to itself 189 | 190 | Arguments: 191 | layers_arr: List containing the objects of nw.layers classes 192 | """ 193 | def __init__(self, layers_arr): 194 | self.layers_arr = layers_arr 195 | 196 | def __call__(self): 197 | for layer in self.layers_arr: 198 | layer.grad_L1 = np.zeros(layer.weights.shape) 199 | for p in range(0, layer.weights.shape[0]): 200 | for n in range(0, layer.weights.shape[1]): 201 | if layer.weights[p, n] > 0: 202 | layer.grad_L1[p, n] = 1 203 | else: 204 | layer.grad_L1[p, n] = -1 205 | 206 | 207 | class CreateMiniBatches: 208 | """ 209 | Create Mini Batches 210 | Creates mini batches for training with the specified mini-batch size 211 | 212 | Arguments: 213 | X: Training data (nd-array) 214 | y: Training data outputs (nd-array) 215 | mb_size: Number of training examples in each mini-batch (int) 216 | Returns: 217 | mini_batch: Dictionary containing all the mini-batches (dict) 218 | num: Number of mini-batches created with the specified mb_size (int) 219 | """ 220 | def __init__(self, X, y, mb_size): 221 | self.X, self.y, self.mb_size = X, y, mb_size 222 | 223 | def __call__(self): 224 | m_ex = self.y.shape[1] 225 | mini_batch = {} 226 | num = m_ex // self.mb_size 227 | if m_ex % self.mb_size != 0: 228 | f = 0 229 | for p in range(0, num): 230 | mini_batch["MB_X" + str(p)] = self.X[f:(f + self.mb_size), :] 231 | mini_batch["MB_Y" + str(p)] = self.y[:, f:(f + self.mb_size)] 232 | f = f + self.mb_size 233 | mini_batch["MB_X" + str(num)] = self.X[f:m_ex, :] 234 | mini_batch["MB_Y" + str(num)] = self.y[:, f:m_ex] 235 | return mini_batch, num 236 | else: 237 | f = 0 238 | for p in range(0, num - 1): 239 | mini_batch["MB_X" + str(p)] = self.X[f:(f + self.mb_size), :] 240 | mini_batch["MB_Y" + str(p)] = self.y[:, f:(f + self.mb_size)] 241 | f = f + self.mb_size 242 | mini_batch["MB_X" + str(num - 1)] = self.X[f:m_ex, :] 243 | mini_batch["MB_Y" + str(num - 1)] = self.y[:, f:m_ex] 244 | return mini_batch, num - 1 245 | -------------------------------------------------------------------------------- /Example Notebooks/mnist_nw.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "mnist-nw.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "6KQndZIhthbx", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "!pip install neowise" 25 | ], 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "vALgRk7Stq01", 33 | "colab_type": "code", 34 | "colab": {} 35 | }, 36 | "source": [ 37 | "import neowise as nw\n", 38 | "import numpy as np\n", 39 | "import matplotlib.pyplot as plt" 40 | ], 41 | "execution_count": null, 42 | "outputs": [] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "metadata": { 47 | "id": "oTPDkVefwGgV", 48 | "colab_type": "code", 49 | "colab": {} 50 | }, 51 | "source": [ 52 | "from tensorflow.keras.datasets import mnist\n", 53 | "from keras.utils import to_categorical" 54 | ], 55 | "execution_count": null, 56 | "outputs": [] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "metadata": { 61 | "id": "KcdayUlMwJwG", 62 | "colab_type": "code", 63 | "colab": {} 64 | }, 65 | "source": [ 66 | "# Data Preparation --- MNIST\n", 67 | "(X_train_orig, Y_train_orig), (X_test_orig, Y_test_orig) = mnist.load_data()\n", 68 | "\n", 69 | "# Preparing the data\n", 70 | "Y_tr_resh = Y_train_orig.reshape(60000, 1)\n", 71 | "Y_te_resh = Y_test_orig.reshape(10000, 1)\n", 72 | "Y_tr_T = to_categorical(Y_tr_resh, num_classes=10)\n", 73 | "Y_te_T = to_categorical(Y_te_resh, num_classes=10)\n", 74 | "y_tr = Y_tr_T.T\n", 75 | "y_te = Y_te_T.T\n", 76 | "\n", 77 | "\n", 78 | "# Flattening of inputs\n", 79 | "X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T\n", 80 | "X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T\n", 81 | "\n", 82 | "# Preprocessing of Inputs\n", 83 | "X_tr = X_train_flatten.T / 255.\n", 84 | "X_te = X_test_flatten.T / 255.\n", 85 | "num_classes = y_tr.shape[0]\n", 86 | "m_tr = X_tr.shape[0]\n", 87 | "m_te = X_te.shape[0]" 88 | ], 89 | "execution_count": null, 90 | "outputs": [] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "metadata": { 95 | "id": "b9g8aXEjwO7-", 96 | "colab_type": "code", 97 | "colab": {} 98 | }, 99 | "source": [ 100 | "model = nw.Model(X_tr,y_tr,X_te,y_te,None,None)" 101 | ], 102 | "execution_count": null, 103 | "outputs": [] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "metadata": { 108 | "id": "iGkQbn6w0S9u", 109 | "colab_type": "code", 110 | "colab": {} 111 | }, 112 | "source": [ 113 | "model.reset()\n", 114 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 115 | "model.add(\"dense2\",500,250,\"relu\")\n", 116 | "model.add(\"dense3\",250,150,\"relu\")\n", 117 | "model.add(\"dense4\",150,100,\"sine\")\n", 118 | "model.add(\"dense5\",100,60,\"sine\")\n", 119 | "model.add(\"dense6\",60,30,\"relu\")\n", 120 | "model.add(\"dense7\",30,15,\"tanh\")\n", 121 | "model.add(\"dense8\",15,10,\"softmax\")" 122 | ], 123 | "execution_count": null, 124 | "outputs": [] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "metadata": { 129 | "id": "LW43Pr1s-NDl", 130 | "colab_type": "code", 131 | "colab": {} 132 | }, 133 | "source": [ 134 | "model.summary()" 135 | ], 136 | "execution_count": null, 137 | "outputs": [] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "metadata": { 142 | "id": "N3FQcIlG-N0E", 143 | "colab_type": "code", 144 | "colab": {} 145 | }, 146 | "source": [ 147 | "model.fit(X_tr,y_tr,0.0005,15,\"Adam\",\"Multi\",mb=512,alpha_decay=False)" 148 | ], 149 | "execution_count": null, 150 | "outputs": [] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "metadata": { 155 | "id": "RV91MFnQAzBA", 156 | "colab_type": "code", 157 | "colab": {} 158 | }, 159 | "source": [ 160 | "model.test(X_te,y_te,\"Multi\")" 161 | ], 162 | "execution_count": null, 163 | "outputs": [] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "metadata": { 168 | "id": "PJUjhBqeBDQj", 169 | "colab_type": "code", 170 | "colab": {} 171 | }, 172 | "source": [ 173 | "model.plot(\"Cost\")" 174 | ], 175 | "execution_count": null, 176 | "outputs": [] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "metadata": { 181 | "id": "L6-nqk8cBNm1", 182 | "colab_type": "code", 183 | "colab": {} 184 | }, 185 | "source": [ 186 | "model.plot(\"Accuracy\")" 187 | ], 188 | "execution_count": null, 189 | "outputs": [] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": { 194 | "id": "JcrL5tPpSKM7", 195 | "colab_type": "text" 196 | }, 197 | "source": [ 198 | "## Replace contents of argument \"direc\" with the directory you want to save the images!\n", 199 | "\n", 200 | "## For saving and loading the model, pass the directory in which you want to save the file as the argument and append it by the name of the file with .h5 extension!" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "metadata": { 206 | "id": "EkC3zD-cBath", 207 | "colab_type": "code", 208 | "colab": {} 209 | }, 210 | "source": [ 211 | "model.plot(\"Cost\",animate=True,direc='/content/drive/My Drive/neowise/mnist/mnist-costs/')" 212 | ], 213 | "execution_count": null, 214 | "outputs": [] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "metadata": { 219 | "id": "5jeY95hrBmRi", 220 | "colab_type": "code", 221 | "colab": {} 222 | }, 223 | "source": [ 224 | "model.plot(\"Accuracy\",animate=True,direc='/content/drive/My Drive/neowise/mnist/mnist-accu/')" 225 | ], 226 | "execution_count": null, 227 | "outputs": [] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "metadata": { 232 | "id": "03MmxGqTCtb6", 233 | "colab_type": "code", 234 | "colab": {} 235 | }, 236 | "source": [ 237 | "model.save_model('/content/drive/My Drive/neowise/mnist/mnist.h5')" 238 | ], 239 | "execution_count": null, 240 | "outputs": [] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "metadata": { 245 | "id": "4kvqFD3SDSS6", 246 | "colab_type": "code", 247 | "colab": {} 248 | }, 249 | "source": [ 250 | "model1 = nw.Model(X_tr,y_tr,X_te,y_te,None,None)" 251 | ], 252 | "execution_count": null, 253 | "outputs": [] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "metadata": { 258 | "id": "zrUiqI5oEGYv", 259 | "colab_type": "code", 260 | "colab": {} 261 | }, 262 | "source": [ 263 | "model1.load_model('/content/drive/My Drive/neowise/mnist/mnist.h5')" 264 | ], 265 | "execution_count": null, 266 | "outputs": [] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "metadata": { 271 | "id": "K09ikCk8HRmt", 272 | "colab_type": "code", 273 | "colab": {} 274 | }, 275 | "source": [ 276 | "model1.summary()" 277 | ], 278 | "execution_count": null, 279 | "outputs": [] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "metadata": { 284 | "id": "jK4Gsj2dH8Fs", 285 | "colab_type": "code", 286 | "colab": {} 287 | }, 288 | "source": [ 289 | "model1.test(X_te,y_te,\"Multi\")" 290 | ], 291 | "execution_count": null, 292 | "outputs": [] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "metadata": { 297 | "id": "8u3CBbqAITwd", 298 | "colab_type": "code", 299 | "colab": {} 300 | }, 301 | "source": [ 302 | "act_te = model1.forward_prop(X_te,False)\n", 303 | "predictions_te = nw.functional.PredictMulti(act_te[\"A\" + str(len(model1.layer_names))])()\n", 304 | "ones_arr = np.ones(predictions_te.shape)\n", 305 | "temp1 = predictions_te == ones_arr\n", 306 | "ind = []\n", 307 | "for gee in range(0, temp1.shape[1]):\n", 308 | " for jee in range(0, temp1.shape[0]):\n", 309 | " if temp1[jee, gee] == True:\n", 310 | " ind.append(jee)\n", 311 | "ind_arr = np.array(ind)" 312 | ], 313 | "execution_count": null, 314 | "outputs": [] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "metadata": { 319 | "id": "WKmjLS8AIU-n", 320 | "colab_type": "code", 321 | "colab": {} 322 | }, 323 | "source": [ 324 | "perm = np.random.permutation(9999)\n", 325 | "l = 0\n", 326 | "\n", 327 | "fig, axs = plt.subplots(5, 5, figsize=(100,100))\n", 328 | "\n", 329 | "for g in range(0,5):\n", 330 | " for h in range(0,5):\n", 331 | " foo = perm[l]\n", 332 | " second_image = X_test_orig[foo, :]\n", 333 | " second_label = Y_te_resh[foo]\n", 334 | " second_pred_label = ind_arr[foo]\n", 335 | " axs[h,g].set_title('Digit Label: {} Predicted Label: {}'.format(second_label,second_pred_label),fontsize=50)\n", 336 | " axs[h,g].imshow(second_image,cmap='gray_r')\n", 337 | " l = l+1" 338 | ], 339 | "execution_count": null, 340 | "outputs": [] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "metadata": { 345 | "id": "hhNhMlX8L-in", 346 | "colab_type": "code", 347 | "colab": {} 348 | }, 349 | "source": [ 350 | "" 351 | ], 352 | "execution_count": null, 353 | "outputs": [] 354 | } 355 | ] 356 | } 357 | -------------------------------------------------------------------------------- /Example Notebooks/moons_nw.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "moons-nw.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "mGnkQqSYwd-J", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "!pip install neowise" 25 | ], 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "4FEEo3pLxYRb", 33 | "colab_type": "code", 34 | "colab": {} 35 | }, 36 | "source": [ 37 | "import neowise as nw\n", 38 | "from sklearn.datasets import make_moons\n", 39 | "import numpy as np\n", 40 | "import math\n", 41 | "import pandas as pd\n", 42 | "from pandas import DataFrame\n", 43 | "import matplotlib.pyplot as plt\n", 44 | "import seaborn as sns\n", 45 | "from matplotlib import pyplot\n", 46 | "from matplotlib import cm" 47 | ], 48 | "execution_count": null, 49 | "outputs": [] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "metadata": { 54 | "id": "4jLMM9tXx_en", 55 | "colab_type": "code", 56 | "colab": {} 57 | }, 58 | "source": [ 59 | "# Data Preparation --- Make Moons\n", 60 | "N_SAMPLES = 1000\n", 61 | "X_data, y_data_t = make_moons(n_samples = N_SAMPLES, noise=0.2, random_state=100)\n", 62 | "y_data = y_data_t.reshape(N_SAMPLES,1)\n", 63 | "m = int(X_data.shape[0])\n", 64 | "m_tr = int(math.ceil((90/100)*m))\n", 65 | "m_cv = int(math.ceil((5/100)*m))\n", 66 | "m_te = m - (m_tr + m_cv)\n", 67 | "X_tr = np.zeros((m_tr,X_data.shape[1]))\n", 68 | "y_tr_t = np.zeros((m_tr,1))\n", 69 | "X_cv = np.zeros((m_cv,X_data.shape[1]))\n", 70 | "y_cv_t = np.zeros((m_cv,1))\n", 71 | "X_te = np.zeros((m_te,X_data.shape[1]))\n", 72 | "y_te_t = np.zeros((m_te,1))\n", 73 | "perm = np.random.permutation(m)\n", 74 | "p = perm.reshape(m,1)\n", 75 | "for i in range(0,m_tr):\n", 76 | " X_tr[i] = X_data[p[i]]\n", 77 | " y_tr_t[i] = y_data[p[i]]\n", 78 | "for i in range(0,m_cv):\n", 79 | " X_cv[i] = X_data[p[i+m_tr]]\n", 80 | " y_cv_t[i] = y_data[p[i+m_tr]]\n", 81 | "for i in range(0,m_te):\n", 82 | " X_te[i] = X_data[p[i+m_tr+m_cv]]\n", 83 | " y_te_t[i] = y_data[p[i+m_tr+m_cv]]\n", 84 | "y_tr = y_tr_t.T\n", 85 | "y_cv = y_cv_t.T\n", 86 | "y_te = y_te_t.T\n", 87 | "\n", 88 | "# Visualising the data\n", 89 | "df = DataFrame(dict(x=X_data[:,0], y=X_data[:,1], label=y_data_t))\n", 90 | "colors = {0:'red', 1:'blue'}\n", 91 | "fig, ax = plt.subplots()\n", 92 | "grouped = df.groupby('label')\n", 93 | "for key, group in grouped:\n", 94 | " group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])\n", 95 | "plt.show()" 96 | ], 97 | "execution_count": null, 98 | "outputs": [] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "metadata": { 103 | "id": "Cgc5KnNoy6xr", 104 | "colab_type": "code", 105 | "colab": {} 106 | }, 107 | "source": [ 108 | "model = nw.Model(X_tr,y_tr,X_te,y_te,X_cv,y_cv)" 109 | ], 110 | "execution_count": null, 111 | "outputs": [] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "metadata": { 116 | "id": "S6svd31v0T8l", 117 | "colab_type": "code", 118 | "colab": {} 119 | }, 120 | "source": [ 121 | "model.reset()\n", 122 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 123 | "model.add(\"dense2\",500,250,\"relu\")\n", 124 | "model.add(\"dense3\",250,150,\"tanh\")\n", 125 | "model.add(\"dense4\",150,100,\"tanh\")\n", 126 | "model.add(\"dense5\",100,60,\"tanh\")\n", 127 | "model.add(\"dense6\",60,30,\"relu\")\n", 128 | "model.add(\"dense7\",30,15,\"tanh\")\n", 129 | "model.add(\"dense8\",15,1,\"sigmoid\")" 130 | ], 131 | "execution_count": null, 132 | "outputs": [] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "metadata": { 137 | "id": "rd6n8YiY-PDy", 138 | "colab_type": "code", 139 | "colab": {} 140 | }, 141 | "source": [ 142 | "model.summary()" 143 | ], 144 | "execution_count": null, 145 | "outputs": [] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "metadata": { 150 | "id": "xtV8twjz-PeR", 151 | "colab_type": "code", 152 | "colab": {} 153 | }, 154 | "source": [ 155 | "model.fit(X_tr,y_tr,0.00005,15,\"RMSprop\",\"Binary\",mb=16,alpha_decay=False)" 156 | ], 157 | "execution_count": null, 158 | "outputs": [] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "metadata": { 163 | "id": "JgN4QQvDAzgq", 164 | "colab_type": "code", 165 | "colab": {} 166 | }, 167 | "source": [ 168 | "model.test(X_te,y_te,\"Binary\")" 169 | ], 170 | "execution_count": null, 171 | "outputs": [] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "metadata": { 176 | "id": "-jUOFrYLBE1Q", 177 | "colab_type": "code", 178 | "colab": {} 179 | }, 180 | "source": [ 181 | "model.plot(\"Cost\")" 182 | ], 183 | "execution_count": null, 184 | "outputs": [] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "metadata": { 189 | "id": "3HBKDZncBOJ_", 190 | "colab_type": "code", 191 | "colab": {} 192 | }, 193 | "source": [ 194 | "model.plot(\"Accuracy\")" 195 | ], 196 | "execution_count": null, 197 | "outputs": [] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": { 202 | "id": "xlofZHBfUc40", 203 | "colab_type": "text" 204 | }, 205 | "source": [ 206 | "## Replace contents of argument \"direc\" with the directory you want to save the images!\n", 207 | "\n", 208 | "## For saving and loading the model, pass the directory in which you want to save the file as the argument and append it by the name of the file with .h5 extension!" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "metadata": { 214 | "id": "jQDbaiyrBgJk", 215 | "colab_type": "code", 216 | "colab": {} 217 | }, 218 | "source": [ 219 | "model.plot(\"Cost\",animate=True,direc='/content/drive/My Drive/neowise/moons/moons-costs/')" 220 | ], 221 | "execution_count": null, 222 | "outputs": [] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "metadata": { 227 | "id": "OmlGdRHXBqAn", 228 | "colab_type": "code", 229 | "colab": {} 230 | }, 231 | "source": [ 232 | "model.plot(\"Accuracy\",animate=True,direc='/content/drive/My Drive/neowise/moons/moons-accu/')" 233 | ], 234 | "execution_count": null, 235 | "outputs": [] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "metadata": { 240 | "id": "repFqyqWCt89", 241 | "colab_type": "code", 242 | "colab": {} 243 | }, 244 | "source": [ 245 | "model.save_model('/content/drive/My Drive/neowise/moons/moons.h5')" 246 | ], 247 | "execution_count": null, 248 | "outputs": [] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "metadata": { 253 | "id": "RzWKk4D2DTAz", 254 | "colab_type": "code", 255 | "colab": {} 256 | }, 257 | "source": [ 258 | "model1 = nw.Model(X_tr,y_tr,X_te,y_te,X_cv,y_cv)" 259 | ], 260 | "execution_count": null, 261 | "outputs": [] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "metadata": { 266 | "id": "6qdtNrneEG9J", 267 | "colab_type": "code", 268 | "colab": {} 269 | }, 270 | "source": [ 271 | "model1.load_model('/content/drive/My Drive/neowise/moons/moons.h5')" 272 | ], 273 | "execution_count": null, 274 | "outputs": [] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "metadata": { 279 | "id": "KWXzXMXpH2U1", 280 | "colab_type": "code", 281 | "colab": {} 282 | }, 283 | "source": [ 284 | "model1.summary()" 285 | ], 286 | "execution_count": null, 287 | "outputs": [] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "metadata": { 292 | "id": "UALu6H6wH8oe", 293 | "colab_type": "code", 294 | "colab": {} 295 | }, 296 | "source": [ 297 | "model1.test(X_te,y_te,\"Binary\")" 298 | ], 299 | "execution_count": null, 300 | "outputs": [] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "metadata": { 305 | "id": "I383vkf-I5Lp", 306 | "colab_type": "code", 307 | "colab": {} 308 | }, 309 | "source": [ 310 | "sns.set_style(\"whitegrid\")\n", 311 | "GRID_X_START = -1.5\n", 312 | "GRID_X_END = 2.5\n", 313 | "GRID_Y_START = -1.0\n", 314 | "GRID_Y_END = 2\n", 315 | "OUTPUT_DIR = \"/content/drive/My Drive/neowise/moons/moons-vis\"\n", 316 | "grid = np.mgrid[GRID_X_START:GRID_X_END:100j,GRID_X_START:GRID_Y_END:100j]\n", 317 | "grid_2d = grid.reshape(2,-1)\n", 318 | "XX, YY = grid\n", 319 | "def make_plot(X, y, plot_name, file_name=None, XX=None, YY=None, preds=None, dark=False):\n", 320 | " if (dark):\n", 321 | " plt.style.use('dark_background')\n", 322 | " else:\n", 323 | " sns.set_style(\"whitegrid\")\n", 324 | " plt.figure(figsize=(16,12))\n", 325 | " axes = plt.gca()\n", 326 | " axes.set(xlabel=\"$X_1$\", ylabel=\"$X_2$\")\n", 327 | " plt.title(plot_name, fontsize=30)\n", 328 | " plt.subplots_adjust(left=0.20)\n", 329 | " plt.subplots_adjust(right=0.80)\n", 330 | " if(XX is not None and YY is not None and preds is not None):\n", 331 | " plt.contourf(XX, YY, preds.reshape(XX.shape), 25, alpha = 1, cmap=cm.Spectral)\n", 332 | " plt.contour(XX, YY, preds.reshape(XX.shape), levels=[.5], cmap=\"Greys\", vmin=0, vmax=.6)\n", 333 | " plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), s=40, cmap=plt.cm.Spectral, edgecolors='black')\n", 334 | " if(file_name):\n", 335 | " plt.savefig(file_name)\n", 336 | " plt.close()\n", 337 | "import os\n", 338 | "def callback_numpy_plot(index, init_params):\n", 339 | " plot_title = \"Iteration {:05}\".format(index)\n", 340 | " file_name = \"numpy_model_{:05}.png\".format(index//1)\n", 341 | " file_path = os.path.join(OUTPUT_DIR, file_name)\n", 342 | " act = model.forward_prop(np.transpose(grid_2d),train_model=False)\n", 343 | " prediction_probs = act[\"A\" + str(len(model.layer_names))]\n", 344 | " prediction_probs = prediction_probs.reshape(prediction_probs.shape[1], 1)\n", 345 | " make_plot(X_te, y_te, plot_title, file_name=file_path, XX=XX, YY=YY, preds=prediction_probs, dark=True)" 346 | ], 347 | "execution_count": null, 348 | "outputs": [] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "metadata": { 353 | "id": "yP3tqsdaJd_z", 354 | "colab_type": "code", 355 | "colab": {} 356 | }, 357 | "source": [ 358 | "model.reset()\n", 359 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 360 | "model.add(\"dense2\",500,250,\"relu\")\n", 361 | "model.add(\"dense3\",250,150,\"tanh\")\n", 362 | "model.add(\"dense4\",150,100,\"tanh\")\n", 363 | "model.add(\"dense5\",100,60,\"tanh\")\n", 364 | "model.add(\"dense6\",60,30,\"relu\")\n", 365 | "model.add(\"dense7\",30,15,\"tanh\")\n", 366 | "model.add(\"dense8\",15,1,\"sigmoid\")\n", 367 | "\n", 368 | "model.fit(X_tr,y_tr,0.00005,15,\"RMSprop\",\"Binary\",mb=16,alpha_decay=False,print_cost=False,callback=callback_numpy_plot)" 369 | ], 370 | "execution_count": null, 371 | "outputs": [] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "metadata": { 376 | "id": "fU5cB36bV5Za", 377 | "colab_type": "code", 378 | "colab": {} 379 | }, 380 | "source": [ 381 | "act = model.forward_prop(np.transpose(grid_2d),train_model=True)\n", 382 | "prediction_probs_np = act[\"A\" + str(len(model.layer_names))]\n", 383 | "prediction_probs_np = prediction_probs_np.reshape(prediction_probs_np.shape[1], 1)\n", 384 | "make_plot(X_te, y_te, \"Final Iteration\", file_name=None, XX=XX, YY=YY, preds=prediction_probs_np, dark=True)" 385 | ], 386 | "execution_count": null, 387 | "outputs": [] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "metadata": { 392 | "id": "G8FfacgxcKR_", 393 | "colab_type": "code", 394 | "colab": {} 395 | }, 396 | "source": [ 397 | "" 398 | ], 399 | "execution_count": null, 400 | "outputs": [] 401 | } 402 | ] 403 | } -------------------------------------------------------------------------------- /Example Notebooks/circles_nw.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "circles-nw.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "IbllYH6Mxr7l", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "!pip install neowise" 25 | ], 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "1oKi3E-dxvb2", 33 | "colab_type": "code", 34 | "colab": {} 35 | }, 36 | "source": [ 37 | "import neowise as nw\n", 38 | "from sklearn.datasets import make_circles\n", 39 | "import numpy as np\n", 40 | "import math\n", 41 | "import pandas as pd\n", 42 | "from pandas import DataFrame\n", 43 | "import matplotlib.pyplot as plt\n", 44 | "import seaborn as sns\n", 45 | "from matplotlib import pyplot\n", 46 | "from matplotlib import cm" 47 | ], 48 | "execution_count": null, 49 | "outputs": [] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "metadata": { 54 | "id": "LRan2pAJyw64", 55 | "colab_type": "code", 56 | "colab": {} 57 | }, 58 | "source": [ 59 | "# Data Preparation --- Make Circles\n", 60 | "N_SAMPLES = 1000\n", 61 | "TEST_SIZE = 0.1\n", 62 | "X_data, y_data_t = make_circles(n_samples = N_SAMPLES, noise=0.05, random_state=100)\n", 63 | "y_data = y_data_t.reshape(N_SAMPLES,1)\n", 64 | "m = int(X_data.shape[0])\n", 65 | "m_tr = int(math.ceil((90/100)*m))\n", 66 | "m_cv = int(math.ceil((5/100)*m))\n", 67 | "m_te = m - (m_tr + m_cv)\n", 68 | "X_tr = np.zeros((m_tr,X_data.shape[1]))\n", 69 | "y_tr_t = np.zeros((m_tr,1))\n", 70 | "X_cv = np.zeros((m_cv,X_data.shape[1]))\n", 71 | "y_cv_t = np.zeros((m_cv,1))\n", 72 | "X_te = np.zeros((m_te,X_data.shape[1]))\n", 73 | "y_te_t = np.zeros((m_te,1))\n", 74 | "perm = np.random.permutation(m)\n", 75 | "p = perm.reshape(m,1)\n", 76 | "for i in range(0,m_tr):\n", 77 | " X_tr[i] = X_data[p[i]]\n", 78 | " y_tr_t[i] = y_data[p[i]]\n", 79 | "for i in range(0,m_cv):\n", 80 | " X_cv[i] = X_data[p[i+m_tr]]\n", 81 | " y_cv_t[i] = y_data[p[i+m_tr]]\n", 82 | "for i in range(0,m_te):\n", 83 | " X_te[i] = X_data[p[i+m_tr+m_cv]]\n", 84 | " y_te_t[i] = y_data[p[i+m_tr+m_cv]]\n", 85 | "y_tr = y_tr_t.T\n", 86 | "y_cv = y_cv_t.T\n", 87 | "y_te = y_te_t.T\n", 88 | " \n", 89 | "# Plotting the data\n", 90 | "df = DataFrame(dict(x=X_data[:,0], y=X_data[:,1], label=y_data_t))\n", 91 | "colors = {0:'red', 1:'blue'}\n", 92 | "fig, ax = plt.subplots()\n", 93 | "grouped = df.groupby('label')\n", 94 | "for key, group in grouped:\n", 95 | " group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])\n", 96 | "plt.show()" 97 | ], 98 | "execution_count": null, 99 | "outputs": [] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "metadata": { 104 | "id": "O3gp0jeAzQbZ", 105 | "colab_type": "code", 106 | "colab": {} 107 | }, 108 | "source": [ 109 | "model = nw.Model(X_tr,y_tr,X_te,y_te,X_cv,y_cv)" 110 | ], 111 | "execution_count": null, 112 | "outputs": [] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "metadata": { 117 | "id": "JbkpV7N10WMX", 118 | "colab_type": "code", 119 | "colab": {} 120 | }, 121 | "source": [ 122 | "model.reset()\n", 123 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 124 | "model.add(\"dense2\",500,250,\"relu\")\n", 125 | "model.add(\"dense3\",250,150,\"tanh\")\n", 126 | "model.add(\"dense4\",150,100,\"tanh\")\n", 127 | "model.add(\"dense5\",100,60,\"tanh\")\n", 128 | "model.add(\"dense6\",60,30,\"relu\")\n", 129 | "model.add(\"dense7\",30,15,\"tanh\")\n", 130 | "model.add(\"dense8\",15,1,\"sigmoid\")" 131 | ], 132 | "execution_count": null, 133 | "outputs": [] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "metadata": { 138 | "id": "qHKxLnBN-RcD", 139 | "colab_type": "code", 140 | "colab": {} 141 | }, 142 | "source": [ 143 | "model.summary()" 144 | ], 145 | "execution_count": null, 146 | "outputs": [] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "metadata": { 151 | "id": "L3ruSbrL-R4Z", 152 | "colab_type": "code", 153 | "colab": {} 154 | }, 155 | "source": [ 156 | "model.fit(X_tr,y_tr,0.005,50,\"Momentum\",\"Binary\",mb=16,alpha_decay=True)" 157 | ], 158 | "execution_count": null, 159 | "outputs": [] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "metadata": { 164 | "id": "guSnh7SiA1E0", 165 | "colab_type": "code", 166 | "colab": {} 167 | }, 168 | "source": [ 169 | "model.test(X_te,y_te,\"Binary\")" 170 | ], 171 | "execution_count": null, 172 | "outputs": [] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "metadata": { 177 | "id": "41gyFPVhBG6T", 178 | "colab_type": "code", 179 | "colab": {} 180 | }, 181 | "source": [ 182 | "model.plot(\"Cost\")" 183 | ], 184 | "execution_count": null, 185 | "outputs": [] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "metadata": { 190 | "id": "TgczGmCfBPev", 191 | "colab_type": "code", 192 | "colab": {} 193 | }, 194 | "source": [ 195 | "model.plot(\"Accuracy\")" 196 | ], 197 | "execution_count": null, 198 | "outputs": [] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": { 203 | "id": "p92jeeQ2U1nT", 204 | "colab_type": "text" 205 | }, 206 | "source": [ 207 | "## Replace contents of argument \"direc\" with the directory you want to save the images!\n", 208 | "\n", 209 | "## For saving and loading the model, pass the directory in which you want to save the file as the argument and append it by the name of the file with .h5 extension!" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "metadata": { 215 | "id": "yGanv8mKBhab", 216 | "colab_type": "code", 217 | "colab": {} 218 | }, 219 | "source": [ 220 | "model.plot(\"Cost\",animate=True,direc='/content/drive/My Drive/neowise/circles/circles-costs/')" 221 | ], 222 | "execution_count": null, 223 | "outputs": [] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "metadata": { 228 | "id": "yzV7j8dYBrb3", 229 | "colab_type": "code", 230 | "colab": {} 231 | }, 232 | "source": [ 233 | "model.plot(\"Accuracy\",animate=True,direc='/content/drive/My Drive/neowise/circles/circles-accu/')" 234 | ], 235 | "execution_count": null, 236 | "outputs": [] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "metadata": { 241 | "id": "MC99K17LCvWR", 242 | "colab_type": "code", 243 | "colab": {} 244 | }, 245 | "source": [ 246 | "model.save_model('/content/drive/My Drive/neowise/circles/circles.h5')" 247 | ], 248 | "execution_count": null, 249 | "outputs": [] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "metadata": { 254 | "id": "W01qNXqnDUm7", 255 | "colab_type": "code", 256 | "colab": {} 257 | }, 258 | "source": [ 259 | "model1 = nw.Model(X_tr,y_tr,X_te,y_te,X_cv,y_cv)" 260 | ], 261 | "execution_count": null, 262 | "outputs": [] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "metadata": { 267 | "id": "21HqhoELEIYL", 268 | "colab_type": "code", 269 | "colab": {} 270 | }, 271 | "source": [ 272 | "model1.load_model('/content/drive/My Drive/neowise/circles/circles.h5')" 273 | ], 274 | "execution_count": null, 275 | "outputs": [] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "metadata": { 280 | "id": "vcDNCxXJHTTr", 281 | "colab_type": "code", 282 | "colab": {} 283 | }, 284 | "source": [ 285 | "model1.summary()" 286 | ], 287 | "execution_count": null, 288 | "outputs": [] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "metadata": { 293 | "id": "x5T4ngTgH97g", 294 | "colab_type": "code", 295 | "colab": {} 296 | }, 297 | "source": [ 298 | "model1.test(X_te,y_te,\"Binary\")" 299 | ], 300 | "execution_count": null, 301 | "outputs": [] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "metadata": { 306 | "id": "-dBlLahqI6bi", 307 | "colab_type": "code", 308 | "colab": {} 309 | }, 310 | "source": [ 311 | "sns.set_style(\"whitegrid\")\n", 312 | "GRID_X_START = 2.5\n", 313 | "GRID_X_END = -1.25\n", 314 | "GRID_Y_START = 1.5\n", 315 | "GRID_Y_END = -1.5\n", 316 | "OUTPUT_DIR = \"/content/drive/My Drive/neowise/circles/circles-vis\"\n", 317 | "grid = np.mgrid[GRID_X_START:GRID_X_END:100j,GRID_X_START:GRID_Y_END:100j]\n", 318 | "grid_2d = grid.reshape(2,-1)\n", 319 | "XX, YY = grid\n", 320 | "def make_plot(X, y, plot_name, file_name=None, XX=None, YY=None, preds=None, dark=False):\n", 321 | " if (dark):\n", 322 | " plt.style.use('dark_background')\n", 323 | " else:\n", 324 | " sns.set_style(\"whitegrid\")\n", 325 | " plt.figure(figsize=(16,12))\n", 326 | " axes = plt.gca()\n", 327 | " axes.set(xlabel=\"$X_1$\", ylabel=\"$X_2$\")\n", 328 | " plt.title(plot_name, fontsize=30)\n", 329 | " plt.subplots_adjust(left=0.20)\n", 330 | " plt.subplots_adjust(right=0.80)\n", 331 | " if(XX is not None and YY is not None and preds is not None):\n", 332 | " plt.contourf(XX, YY, preds.reshape(XX.shape), 25, alpha = 1, cmap=cm.Spectral)\n", 333 | " plt.contour(XX, YY, preds.reshape(XX.shape), levels=[.5], cmap=\"Greys\", vmin=0, vmax=.6)\n", 334 | " plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), s=40, cmap=plt.cm.Spectral, edgecolors='black')\n", 335 | " if(file_name):\n", 336 | " plt.savefig(file_name)\n", 337 | " plt.close()\n", 338 | "import os\n", 339 | "def callback_numpy_plot(index, init_params):\n", 340 | " plot_title = \"Iteration {:05}\".format(index)\n", 341 | " file_name = \"numpy_model_{:05}.png\".format(index//1)\n", 342 | " file_path = os.path.join(OUTPUT_DIR, file_name)\n", 343 | " act = model.forward_prop(np.transpose(grid_2d),train_model=False)\n", 344 | " prediction_probs = act[\"A\" + str(len(model.layer_names))]\n", 345 | " prediction_probs = prediction_probs.reshape(prediction_probs.shape[1], 1)\n", 346 | " make_plot(X_te, y_te, plot_title, file_name=file_path, XX=XX, YY=YY, preds=prediction_probs, dark=True)" 347 | ], 348 | "execution_count": null, 349 | "outputs": [] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "metadata": { 354 | "id": "FTysI3KtJfpG", 355 | "colab_type": "code", 356 | "colab": {} 357 | }, 358 | "source": [ 359 | "model.reset()\n", 360 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 361 | "model.add(\"dense2\",500,250,\"relu\")\n", 362 | "model.add(\"dense3\",250,150,\"tanh\")\n", 363 | "model.add(\"dense4\",150,100,\"tanh\")\n", 364 | "model.add(\"dense5\",100,60,\"tanh\")\n", 365 | "model.add(\"dense6\",60,30,\"relu\")\n", 366 | "model.add(\"dense7\",30,15,\"tanh\")\n", 367 | "model.add(\"dense8\",15,1,\"sigmoid\")\n", 368 | "\n", 369 | "model.fit(X_tr,y_tr,0.005,50,\"Momentum\",\"Binary\",mb=16,alpha_decay=True,print_cost=False,callback=callback_numpy_plot)" 370 | ], 371 | "execution_count": null, 372 | "outputs": [] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "metadata": { 377 | "id": "v6IzmHsrUo_P", 378 | "colab_type": "code", 379 | "colab": {} 380 | }, 381 | "source": [ 382 | "act = model.forward_prop(np.transpose(grid_2d),train_model=True)\n", 383 | "prediction_probs_np = act[\"A\" + str(len(model.layer_names))]\n", 384 | "prediction_probs_np = prediction_probs_np.reshape(prediction_probs_np.shape[1], 1)\n", 385 | "make_plot(X_te, y_te, \"Final Iteration\", file_name=None, XX=XX, YY=YY, preds=prediction_probs_np, dark=True)" 386 | ], 387 | "execution_count": null, 388 | "outputs": [] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "metadata": { 393 | "id": "dD_QvDAIcBX4", 394 | "colab_type": "code", 395 | "colab": {} 396 | }, 397 | "source": [ 398 | "" 399 | ], 400 | "execution_count": null, 401 | "outputs": [] 402 | } 403 | ] 404 | } -------------------------------------------------------------------------------- /Example Notebooks/spirals_nw.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "spirals-nw.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "dsDyEFwiwzx5", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "!pip install neowise" 25 | ], 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "mWwCducexamc", 33 | "colab_type": "code", 34 | "colab": {} 35 | }, 36 | "source": [ 37 | "import neowise as nw\n", 38 | "import numpy as np\n", 39 | "import matplotlib.pyplot as plt\n", 40 | "import math\n", 41 | "import seaborn as sns\n", 42 | "from matplotlib import pyplot\n", 43 | "from matplotlib import cm" 44 | ], 45 | "execution_count": null, 46 | "outputs": [] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "metadata": { 51 | "id": "7vrISIFByNu2", 52 | "colab_type": "code", 53 | "colab": {} 54 | }, 55 | "source": [ 56 | "# Data Preparation --- Two Spirals\n", 57 | "def twospirals(n_points, noise=0.5):\n", 58 | " n = np.sqrt(np.random.rand(n_points,1)) * 780 * (2*np.pi)/360\n", 59 | " d1x = -np.cos(n)*n + np.random.rand(n_points,1) * noise\n", 60 | " d1y = np.sin(n)*n + np.random.rand(n_points,1) * noise\n", 61 | " return (np.vstack((np.hstack((d1x,d1y)),np.hstack((-d1x,-d1y)))), \n", 62 | " np.hstack((np.zeros(n_points),np.ones(n_points))))\n", 63 | "\n", 64 | "N_SAMPLES = 1000\n", 65 | "X_data, y_data_t = twospirals(N_SAMPLES)\n", 66 | "y_data = y_data_t.reshape(N_SAMPLES*2,1)\n", 67 | "mu1 = np.mean(X_data[:,0],axis=0)\n", 68 | "mu2 = np.mean(X_data[:,1],axis=0)\n", 69 | "var1 = np.var(X_data[:,0],axis=0)\n", 70 | "var2 = np.var(X_data[:,1],axis=0)\n", 71 | "X_data[:,0] = (X_data[:,0]-mu1)/var1\n", 72 | "X_data[:,1] = (X_data[:,1]-mu2)/var2\n", 73 | "m = int(X_data.shape[0])\n", 74 | "m_tr = int(math.ceil((90/100)*m))\n", 75 | "m_cv = int(math.ceil((5/100)*m))\n", 76 | "m_te = m - (m_tr + m_cv)\n", 77 | "X_tr = np.zeros((m_tr,X_data.shape[1]))\n", 78 | "y_tr_t = np.zeros((m_tr,1))\n", 79 | "X_cv = np.zeros((m_cv,X_data.shape[1]))\n", 80 | "y_cv_t = np.zeros((m_cv,1))\n", 81 | "X_te = np.zeros((m_te,X_data.shape[1]))\n", 82 | "y_te_t = np.zeros((m_te,1))\n", 83 | "perm = np.random.permutation(m)\n", 84 | "p = perm.reshape(m,1)\n", 85 | "for i in range(0,m_tr):\n", 86 | " X_tr[i] = X_data[p[i]]\n", 87 | " y_tr_t[i] = y_data[p[i]]\n", 88 | "for i in range(0,m_cv):\n", 89 | " X_cv[i] = X_data[p[i+m_tr]]\n", 90 | " y_cv_t[i] = y_data[p[i+m_tr]]\n", 91 | "for i in range(0,m_te):\n", 92 | " X_te[i] = X_data[p[i+m_tr+m_cv]]\n", 93 | " y_te_t[i] = y_data[p[i+m_tr+m_cv]]\n", 94 | "y_tr = y_tr_t.T\n", 95 | "y_cv = y_cv_t.T\n", 96 | "y_te = y_te_t.T\n", 97 | "plt.title('Training Set')\n", 98 | "plt.plot(X_data[y_data_t==0,0], X_data[y_data_t==0,1], '.', label='class 1')\n", 99 | "plt.plot(X_data[y_data_t==1,0], X_data[y_data_t==1,1], '.', label='class 2')\n", 100 | "plt.legend()\n", 101 | "plt.show()" 102 | ], 103 | "execution_count": null, 104 | "outputs": [] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "metadata": { 109 | "id": "lErPYl9Gz12u", 110 | "colab_type": "code", 111 | "colab": {} 112 | }, 113 | "source": [ 114 | "model = nw.Model(X_tr,y_tr,X_te,y_te,X_cv,y_cv)" 115 | ], 116 | "execution_count": null, 117 | "outputs": [] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "metadata": { 122 | "id": "0njl6usZ0U0q", 123 | "colab_type": "code", 124 | "colab": {} 125 | }, 126 | "source": [ 127 | "model.reset()\n", 128 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 129 | "model.add(\"dense2\",500,250,\"relu\")\n", 130 | "model.add(\"dense3\",250,150,\"sine\")\n", 131 | "model.add(\"dense4\",150,100,\"sine\")\n", 132 | "model.add(\"dense5\",100,60,\"sine\")\n", 133 | "model.add(\"dense6\",60,30,\"relu\")\n", 134 | "model.add(\"dense7\",30,15,\"tanh\")\n", 135 | "model.add(\"dense8\",15,1,\"sigmoid\")" 136 | ], 137 | "execution_count": null, 138 | "outputs": [] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "metadata": { 143 | "id": "z-wW1ENR-QRK", 144 | "colab_type": "code", 145 | "colab": {} 146 | }, 147 | "source": [ 148 | "model.summary()" 149 | ], 150 | "execution_count": null, 151 | "outputs": [] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "metadata": { 156 | "id": "tOII05RI-QoQ", 157 | "colab_type": "code", 158 | "colab": {} 159 | }, 160 | "source": [ 161 | "model.fit(X_tr,y_tr,0.005,15,\"Adam\",\"Binary\",mb=32,alpha_decay=True)" 162 | ], 163 | "execution_count": null, 164 | "outputs": [] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "metadata": { 169 | "id": "T9Gt48ZBA0Ok", 170 | "colab_type": "code", 171 | "colab": {} 172 | }, 173 | "source": [ 174 | "model.test(X_te,y_te,\"Binary\")" 175 | ], 176 | "execution_count": null, 177 | "outputs": [] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "metadata": { 182 | "id": "oJ0NyG95BGIZ", 183 | "colab_type": "code", 184 | "colab": {} 185 | }, 186 | "source": [ 187 | "model.plot(\"Cost\")" 188 | ], 189 | "execution_count": null, 190 | "outputs": [] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "metadata": { 195 | "id": "Qy0_uXhUBOxj", 196 | "colab_type": "code", 197 | "colab": {} 198 | }, 199 | "source": [ 200 | "model.plot(\"Accuracy\")" 201 | ], 202 | "execution_count": null, 203 | "outputs": [] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": { 208 | "id": "gFZhEPIDUtJw", 209 | "colab_type": "text" 210 | }, 211 | "source": [ 212 | "## Replace contents of argument \"direc\" with the directory you want to save the images!\n", 213 | "\n", 214 | "## For saving and loading the model, pass the directory in which you want to save the file as the argument and append it by the name of the file with .h5 extension!" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "metadata": { 220 | "id": "ynlL7BWJBgr2", 221 | "colab_type": "code", 222 | "colab": {} 223 | }, 224 | "source": [ 225 | "model.plot(\"Cost\",animate=True,direc='/content/drive/My Drive/neowise/spirals/spirals-costs/')" 226 | ], 227 | "execution_count": null, 228 | "outputs": [] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "metadata": { 233 | "id": "zSUiISWuBqit", 234 | "colab_type": "code", 235 | "colab": {} 236 | }, 237 | "source": [ 238 | "model.plot(\"Accuracy\",animate=True,direc='/content/drive/My Drive/neowise/spirals/spirals-accu/')" 239 | ], 240 | "execution_count": null, 241 | "outputs": [] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "metadata": { 246 | "id": "77Qn4WDaCulk", 247 | "colab_type": "code", 248 | "colab": {} 249 | }, 250 | "source": [ 251 | "model.save_model('/content/drive/My Drive/neowise/spirals/spirals.h5')" 252 | ], 253 | "execution_count": null, 254 | "outputs": [] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "metadata": { 259 | "id": "813VAqf9DT2w", 260 | "colab_type": "code", 261 | "colab": {} 262 | }, 263 | "source": [ 264 | "model1 = nw.Model(X_tr,y_tr,X_te,y_te,X_cv,y_cv)" 265 | ], 266 | "execution_count": null, 267 | "outputs": [] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "metadata": { 272 | "id": "3GWphRk7EHiD", 273 | "colab_type": "code", 274 | "colab": {} 275 | }, 276 | "source": [ 277 | "model1.load_model('/content/drive/My Drive/neowise/spirals/spirals.h5')" 278 | ], 279 | "execution_count": null, 280 | "outputs": [] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "metadata": { 285 | "id": "3g3MAet3HSxx", 286 | "colab_type": "code", 287 | "colab": {} 288 | }, 289 | "source": [ 290 | "model1.summary()" 291 | ], 292 | "execution_count": null, 293 | "outputs": [] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "metadata": { 298 | "id": "PmtMzSNxH9MA", 299 | "colab_type": "code", 300 | "colab": {} 301 | }, 302 | "source": [ 303 | "model1.test(X_te,y_te,\"Binary\")" 304 | ], 305 | "execution_count": null, 306 | "outputs": [] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "metadata": { 311 | "id": "xuiZtBn-I51i", 312 | "colab_type": "code", 313 | "colab": {} 314 | }, 315 | "source": [ 316 | "sns.set_style(\"whitegrid\")\n", 317 | "GRID_X_START = 0.5\n", 318 | "GRID_X_END = -0.5\n", 319 | "GRID_Y_START = 0.5\n", 320 | "GRID_Y_END = -0.5\n", 321 | "OUTPUT_DIR = \"/content/drive/My Drive/neowise/spirals/spirals-vis\"\n", 322 | "grid = np.mgrid[GRID_X_START:GRID_X_END:100j,GRID_X_START:GRID_Y_END:100j]\n", 323 | "grid_2d = grid.reshape(2,-1)\n", 324 | "XX, YY = grid\n", 325 | "def make_plot(X, y, plot_name, file_name=None, XX=None, YY=None, preds=None, dark=False):\n", 326 | " if (dark):\n", 327 | " plt.style.use('dark_background')\n", 328 | " else:\n", 329 | " sns.set_style(\"whitegrid\")\n", 330 | " plt.figure(figsize=(16,12))\n", 331 | " axes = plt.gca()\n", 332 | " axes.set(xlabel=\"$X_1$\", ylabel=\"$X_2$\")\n", 333 | " plt.title(plot_name, fontsize=30)\n", 334 | " plt.subplots_adjust(left=0.20)\n", 335 | " plt.subplots_adjust(right=0.80)\n", 336 | " if(XX is not None and YY is not None and preds is not None):\n", 337 | " plt.contourf(XX, YY, preds.reshape(XX.shape), 25, alpha = 1, cmap=cm.Spectral)\n", 338 | " plt.contour(XX, YY, preds.reshape(XX.shape), levels=[.5], cmap=\"Greys\", vmin=0, vmax=.6)\n", 339 | " plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), s=40, cmap=plt.cm.Spectral, edgecolors='black')\n", 340 | " if(file_name):\n", 341 | " plt.savefig(file_name)\n", 342 | " plt.close()\n", 343 | "import os\n", 344 | "def callback_numpy_plot(index, init_params):\n", 345 | " plot_title = \"Iteration {:05}\".format(index)\n", 346 | " file_name = \"numpy_model_{:05}.png\".format(index//1)\n", 347 | " file_path = os.path.join(OUTPUT_DIR, file_name)\n", 348 | " act = model.forward_prop(np.transpose(grid_2d),train_model=False)\n", 349 | " prediction_probs = act[\"A\" + str(len(model.layer_names))]\n", 350 | " prediction_probs = prediction_probs.reshape(prediction_probs.shape[1], 1)\n", 351 | " make_plot(X_te, y_te, plot_title, file_name=file_path, XX=XX, YY=YY, preds=prediction_probs, dark=True)" 352 | ], 353 | "execution_count": null, 354 | "outputs": [] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "metadata": { 359 | "id": "NEfq8tDmJenj", 360 | "colab_type": "code", 361 | "colab": {} 362 | }, 363 | "source": [ 364 | "model.reset()\n", 365 | "model.add(\"dense1\",X_tr.shape[1],500,\"relu\")\n", 366 | "model.add(\"dense2\",500,250,\"relu\")\n", 367 | "model.add(\"dense3\",250,150,\"sine\")\n", 368 | "model.add(\"dense4\",150,100,\"sine\")\n", 369 | "model.add(\"dense5\",100,60,\"sine\")\n", 370 | "model.add(\"dense6\",60,30,\"relu\")\n", 371 | "model.add(\"dense7\",30,15,\"tanh\")\n", 372 | "model.add(\"dense8\",15,1,\"sigmoid\")\n", 373 | "\n", 374 | "model.fit(X_tr,y_tr,0.005,15,\"Adam\",\"Binary\",mb=32,alpha_decay=True,print_cost=False,callback=callback_numpy_plot)" 375 | ], 376 | "execution_count": null, 377 | "outputs": [] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "metadata": { 382 | "id": "rujhQigrR7uF", 383 | "colab_type": "code", 384 | "colab": {} 385 | }, 386 | "source": [ 387 | "act = model.forward_prop(np.transpose(grid_2d),train_model=True)\n", 388 | "prediction_probs_np = act[\"A\" + str(len(model.layer_names))]\n", 389 | "prediction_probs_np = prediction_probs_np.reshape(prediction_probs_np.shape[1], 1)\n", 390 | "make_plot(X_te, y_te, \"Final Iteration\", file_name=None, XX=XX, YY=YY, preds=prediction_probs_np, dark=True)" 391 | ], 392 | "execution_count": null, 393 | "outputs": [] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "metadata": { 398 | "id": "4yEKK-cycFGZ", 399 | "colab_type": "code", 400 | "colab": {} 401 | }, 402 | "source": [ 403 | "" 404 | ], 405 | "execution_count": null, 406 | "outputs": [] 407 | } 408 | ] 409 | } -------------------------------------------------------------------------------- /neowise/build/lib/neowise/neural_net.py: -------------------------------------------------------------------------------- 1 | from neowise.activations import * 2 | from neowise.cost_function import * 3 | from neowise.functional import * 4 | from neowise.layers import * 5 | from neowise.optimizers import * 6 | from neowise.plots import * 7 | from tqdm import tqdm 8 | import sys 9 | import time 10 | import hdfdict 11 | from prettytable import PrettyTable 12 | 13 | 14 | class Model: 15 | """ 16 | Model 17 | Puts together all the components of the network and builds a Neural Network 18 | 19 | Arguments: 20 | X_tr: Training data to train the network (nd-array) 21 | y_tr: Output labels of the Training data (nd-array) 22 | X_te: Testing data for the network (nd-array) 23 | y_te: Output labels of the Testing data (nd-array) 24 | X_cv: Cross Validation data for the network (nd-array) 25 | y_cv: Output labels for the Cross Validation data (nd-array) 26 | """ 27 | def __init__(self, X_tr, y_tr, X_te, y_te, X_cv, y_cv): 28 | self.X_tr, self.y_tr = X_tr, y_tr 29 | self.X_te, self.y_te = X_te, y_te 30 | self.X_cv, self.y_cv = X_cv, y_cv 31 | self.layer_names = [] 32 | self.layer_names_arr = [] 33 | self.activations_cache = None 34 | self.params = None 35 | self.arch = {} 36 | self.accu_tr_arr = None 37 | self.cost_tr_arr = None 38 | self.accu_cv_arr = None 39 | self.cost_cv_arr = None 40 | self.lr = None 41 | self.epochs = None 42 | 43 | def add(self, layer_name, num_inputs, num_outputs, act_fn, dropout=1): 44 | """ 45 | Add 46 | Creates a layer object and stores it in a list (self.layer_names) 47 | :param layer_name: Name of the layer being added 48 | :param num_inputs: Number of inputs to the layer 49 | :param num_outputs: Number of units in the layer 50 | :param act_fn: Activation function to be applied for the layer 51 | :param dropout: Dropout value for the layer 52 | """ 53 | self.layer_names_arr.append(layer_name) 54 | self.arch[str(layer_name)] = [layer_name.encode('utf8'), num_inputs, num_outputs, act_fn.encode('utf8'), 55 | dropout] 56 | layer_name = Dense(num_inputs, num_outputs, act_fn, dropout) 57 | Dense.initialize_params(layer_name) 58 | self.layer_names.append(layer_name) 59 | 60 | def reset(self): 61 | """ 62 | Reset 63 | Resets the self.layer_names list 64 | """ 65 | self.layer_names = [] 66 | self.arch = {} 67 | self.layer_names_arr = [] 68 | return self.layer_names, self.arch, self.layer_names_arr 69 | 70 | def params_dict(self, print_params): 71 | """ 72 | Parameters Dictionary 73 | Creates a dictionary of all the parameters (weights and bias) of the network 74 | :param print_params: Boolean value if True, prints the parameters 75 | :return: self.params 76 | """ 77 | self.params = {} 78 | hee = 1 79 | for layer in self.layer_names: 80 | self.params["W" + str(hee)], self.params["b" + str(hee)] = Dense.get_params(layer) 81 | hee += 1 82 | if print_params is True: 83 | print(self.params) 84 | return self.params 85 | else: 86 | return self.params 87 | 88 | def forward_prop(self, X, train_model=True): 89 | """ 90 | Forward Propagation 91 | Propagates the data through the network 92 | :param X: Data to be propagated 93 | :param train_model: Boolean if True, Dropout values will be unchanged, else all Dropout values = 1 94 | :return: activations_cache, a list containing all the activations of all the layers of the network 95 | """ 96 | self.activations_cache = {} 97 | self.activations_cache = {"A0": X.T} 98 | temp_A = X.T 99 | p = 1 100 | for layer in self.layer_names: 101 | _, temp_A = Dense.forw_prop(layer, temp_A, train_model) 102 | self.activations_cache["A" + str(p)] = temp_A 103 | p += 1 104 | return self.activations_cache 105 | 106 | def backward_prop(self, y, prob_type, activations_cache, lamb, reg): 107 | """ 108 | Backward Propagation 109 | Calculates the derivative of the Cost Function w.r.t the weights, bias, activations of all the layers of 110 | the network 111 | :param y: Output labels of the data (nd-array) 112 | :param prob_type: Type of problem ["Binary": For Binary Classification,"Multi": For Multi Class Classification] 113 | (str) 114 | :param activations_cache: Activations cache list put together doing nw.neural_net.forward_prop (list) 115 | :param lamb: Regularization parameter "lamda" (float) 116 | :param reg: Type of Regularization ["L1": For L1 regularization, "L2": For L2 Regularization] (str) 117 | """ 118 | prob_type_dict = {"Binary": [BinaryCrossEntropy, PrecisionRecall, Predict, Evaluate], 119 | "Multi": [CrossEntropy, PrecisionRecallMulti, PredictMulti, EvaluateMulti]} 120 | _, temp_dA = prob_type_dict[prob_type][0](y, activations_cache["A" + str(len(self.layer_names))], 121 | self.layer_names, lamb, reg)() 122 | l = 1 123 | for layer in reversed(self.layer_names): 124 | _, layer.dW, layer.db, temp_dA = Dense.back_prop(layer, temp_dA, self.activations_cache[ 125 | "A" + str(len(self.layer_names) - l)]) 126 | l += 1 127 | 128 | def fit(self, X, y, alpha, num_iter, optim, prob_type, mb, reg=None, lamb=None, alpha_decay=False, print_cost=True, 129 | callback=None): 130 | """ 131 | Fit 132 | Trains the model 133 | :param X: Training data (nd-array) 134 | :param y: Training data output labels (nd-array) 135 | :param alpha: Learning Rate (float) 136 | :param num_iter: Number of Iterations through the data (int) 137 | :param optim: Optimizer for training ["GD":Gradient Descent,"Momentum":Momentum,"RMSprop":RMSprop,"Adam":Adam] (str) 138 | :param prob_type: Type of problem ["Binary":Binary Classification,"Multi":Multi-Class Classification] (str) 139 | :param mb: Mini-Batch size (int) 140 | :param reg: Regularization type ["L1":L1 Regularization,"L2":L2 Regularization] (str) 141 | :param lamb: Regularization parameter (float) 142 | :param alpha_decay: Decrease the learning rate through the number of iterations (bool) 143 | :param print_cost: Whether to print cost or not (bool) 144 | :param callback: Callback for boundary visualisation for binary classification 145 | """ 146 | self.lr = alpha 147 | if (num_iter > 100) and (num_iter <= 1000): 148 | freq = 10 149 | elif num_iter > 1000: 150 | freq = 50 151 | elif num_iter <= 100: 152 | freq = 1 153 | params = self.params_dict(print_params=False) 154 | V_dict = {} 155 | S_dict = {} 156 | mini_batches, num = CreateMiniBatches(X, y, mb)() 157 | self.accu_tr_arr = [] 158 | self.cost_tr_arr = [] 159 | self.accu_cv_arr = [] 160 | self.cost_cv_arr = [] 161 | for k in range(1, len(self.layer_names) + 1): 162 | V_dict["Vdw" + str(k)] = np.zeros(params["W" + str(k)].shape) 163 | V_dict["Vdb" + str(k)] = np.zeros(params["b" + str(k)].shape) 164 | S_dict["Sdw" + str(k)] = np.zeros(params["W" + str(k)].shape) 165 | S_dict["Sdb" + str(k)] = np.zeros(params["b" + str(k)].shape) 166 | optim_dict = {"GD": [GradientDescent, None, None, 0], 167 | "Momentum": [Momentum, V_dict, None, 0], 168 | "RMSprop": [RMSProp, None, S_dict, 0], 169 | "Adam": [Adam, V_dict, S_dict, 0]} 170 | prob_type_dict = {"Binary": [BinaryCrossEntropy, PrecisionRecall, Predict, Evaluate], 171 | "Multi": [CrossEntropy, PrecisionRecallMulti, PredictMulti, EvaluateMulti]} 172 | 173 | for i in range(1, num_iter + 1): 174 | params_plot = self.params_dict(print_params=False) 175 | if alpha_decay is True: 176 | alpha = (np.power(0.95, i)) * self.lr 177 | for vee in tqdm(range(0, num + 1), file=sys.stdout): 178 | activations_dict = self.forward_prop(mini_batches["MB_X" + str(vee)]) 179 | self.backward_prop(mini_batches["MB_Y" + str(vee)], prob_type, activations_dict, lamb, reg) 180 | optim_dict[optim][0](alpha, self.layer_names, optim_dict[optim][1], optim_dict[optim][2], 181 | optim_dict[optim][3] + i)() 182 | act_tr = self.forward_prop(X) 183 | cost_tr, _ = prob_type_dict[prob_type][0](y, act_tr["A" + str(len(self.layer_names))], self.layer_names, 184 | lamb, reg)() 185 | preds = prob_type_dict[prob_type][2](act_tr["A" + str(len(self.layer_names))])() 186 | accu_tr = prob_type_dict[prob_type][3](y, preds)() 187 | self.accu_tr_arr.append(accu_tr) 188 | self.cost_tr_arr.append(cost_tr) 189 | if self.X_cv is not None: 190 | act_cv = self.forward_prop(self.X_cv) 191 | cost_cv, _ = prob_type_dict[prob_type][0](self.y_cv, act_cv["A" + str(len(self.layer_names))], 192 | self.layer_names, lamb, reg=None)() 193 | preds_cv = prob_type_dict[prob_type][2](act_cv["A" + str(len(self.layer_names))])() 194 | accu_cv = prob_type_dict[prob_type][3](self.y_cv, preds_cv)() 195 | self.accu_cv_arr.append(accu_cv) 196 | self.cost_cv_arr.append(cost_cv) 197 | if (i % 1 == 0) and print_cost == True: 198 | if self.X_cv is None: 199 | print("Iteration " + str(i) + " " + "train_cost: " + str( 200 | np.round(cost_tr, 6)) + " --- " + "train_acc: " + str(np.round(accu_tr, 3))) 201 | else: 202 | print("Iteration " + str(i) + " " + "train_cost: " + str( 203 | np.round(cost_tr, 6)) + " --- " + "train_acc: " + str( 204 | np.round(accu_tr, 3)) + " --- " + "val_cost: " + str( 205 | np.round(cost_cv, 6)) + " --- " + "val_accu: " + str(np.round(accu_cv, 3))) 206 | if i % 1 == 0: 207 | if (callback is not None): 208 | callback(i, params_plot) 209 | 210 | def save_model(self, fname): 211 | """ 212 | Save Model 213 | Save the parameters and the architecture of the model for reuse once trained through hdfdict 214 | :param fname: Directory where the model should be saved with filename with .h5 extension 215 | """ 216 | params = self.params_dict(print_params=False) 217 | archi = self.arch 218 | model_dict = {"Parameters": params, "Architecture": archi} 219 | hdfdict.dump(model_dict, fname) 220 | print("Model saved!") 221 | 222 | def load_model(self, fname): 223 | """ 224 | Load Model 225 | Loads the parameters and the architecture of the saved models 226 | :param fname: Directory from where the model saved be opened 227 | """ 228 | print("Model loading....") 229 | model_dict = dict(hdfdict.load(fname)) 230 | params_dict = model_dict["Parameters"] 231 | arch_dict = model_dict["Architecture"] 232 | self.reset() 233 | for key in arch_dict: 234 | self.add(arch_dict[key][0].decode('utf8'), int(arch_dict[key][1]), int(arch_dict[key][2]), 235 | arch_dict[key][3].decode('utf8'), int(arch_dict[key][4])) 236 | dee = 1 237 | for layer in self.layer_names: 238 | layer.weights = params_dict["W" + str(dee)] 239 | layer.bias = params_dict["b" + str(dee)] 240 | dee += 1 241 | print("Model loaded!") 242 | 243 | def plot(self, type_func, animate=False, direc=None, freq=1): 244 | """ 245 | Plot 246 | Plots the graphs of cost functions and accuracy on training and cross val with number of iterations on the X axis 247 | :param type_func: Type Function to be plotted ["Cost": Cost,"Accuracy": Accuracy] (str) 248 | :param animate: Boolean whether to animate the graph or not (bool) 249 | :param direc: Directory where the images should be stored 250 | :param freq: Update frequency of plot animation 251 | """ 252 | itera = np.arange(1, len(self.cost_tr_arr) + 1) 253 | plot_dict = {"Cost": [PlotCostStatic, AnimatePlotMulti, self.cost_tr_arr, self.cost_cv_arr, 'Costs', 254 | 'Train-Cross Val Costs Curve', 'upper right', ['c', '#9ef705'], AnimatePlot, "Costs", 255 | "Cost Function Curve"], 256 | "Accuracy": [PlotTrCvStatic, AnimatePlotMulti, self.accu_tr_arr, self.accu_cv_arr, 'Accuracy', 257 | 'Train-Cross Val Accuracy Curve', 'lower right', ['m', 'r'], AnimatePlot, "Accuracy", 258 | "Accuracy Curve"]} 259 | if animate is False: 260 | plot_dict[type_func][0](self.cost_tr_arr, self.cost_cv_arr, self.accu_tr_arr, self.accu_cv_arr)() 261 | else: 262 | if len(self.cost_cv_arr) != 0: 263 | plot_dict[type_func][1]([itera, itera], [plot_dict[type_func][2], plot_dict[type_func][3]], 264 | 'Number of Iterations', plot_dict[type_func][4], plot_dict[type_func][5], 265 | ['Train', 'Cross Val'], plot_dict[type_func][6], plot_dict[type_func][7], direc, 266 | freq)() 267 | else: 268 | plot_dict[type_func][8](itera, plot_dict[type_func][2], "Number of Iterations", plot_dict[type_func][9], 269 | plot_dict[type_func][10], "Train", plot_dict[type_func][6], 270 | plot_dict[type_func][7][0], direc, freq)() 271 | print("Go to your directory to find the images! Feed them to a GIF creator to animate them!") 272 | 273 | def summary(self): 274 | """ 275 | Summary 276 | Returns an ASCII Table generated by prettytable that contains the architecture of the network 277 | """ 278 | tab = PrettyTable() 279 | tab.field_names = ["Layer Number", "Layer Name", "Inputs", "Outputs", "Activation", "Dropout", 280 | "Number of Parameters"] 281 | yee = 1 282 | total_params = 0 283 | for layer in self.layer_names: 284 | total_params += (layer.weights.shape[0] * layer.weights.shape[1]) + len(layer.bias) 285 | tab.add_row([str(yee), self.layer_names_arr[yee - 1], layer.weights.shape[1], layer.weights.shape[0], 286 | layer.activation_fn, layer.dropout, 287 | str((layer.weights.shape[0] * layer.weights.shape[1]) + len(layer.bias))]) 288 | yee += 1 289 | print(tab) 290 | print("Total number of trainable Parameters: " + str(total_params)) 291 | 292 | def test(self, X, y, prob_type, training=False, print_values=True): 293 | """ 294 | Test 295 | Test the trained model on the test data and return the accuracy, precision, recall and F1 score on test data 296 | :param X: Data (test) 297 | :param y: Output labels of data (test) 298 | :param prob_type: Type of problem ["Binary":Binary Classification,"Multi":Multi-Class Classification] (str) 299 | :param training: Boolean if training is True or False for nw.neural_net.forward_prop (default=False) 300 | :param print_values: Boolean whether to print values or not (default=True) 301 | """ 302 | prob_type_dict = {"Binary": [BinaryCrossEntropy, PrecisionRecall, Predict, Evaluate], 303 | "Multi": [CrossEntropy, PrecisionRecallMulti, PredictMulti, EvaluateMulti]} 304 | act_te = self.forward_prop(X, training) 305 | predictions_te = prob_type_dict[prob_type][2](act_te["A" + str(len(self.layer_names))])() 306 | accu_te = prob_type_dict[prob_type][3](y, predictions_te)() 307 | prec_te, rec_te, f1_te = prob_type_dict[prob_type][1](predictions_te, y)() 308 | tab = PrettyTable() 309 | if prob_type == "Multi": 310 | tab.field_names = ["Class", "Precision", "Recall", "F1"] 311 | for hee in range(0, y.shape[0]): 312 | tab.add_row([hee, prec_te["class" + str(hee)], rec_te["class" + str(hee)], f1_te["class" + str(hee)]]) 313 | if print_values is True: 314 | print(tab) 315 | print("Test Accuracy: " + str(accu_te)) 316 | if prob_type == "Binary": 317 | tab.field_names = ["Precision", "Recall", "F1"] 318 | tab.add_row([str(prec_te), str(rec_te), str(f1_te)]) 319 | if print_values is True: 320 | print(tab) 321 | print("Test Accuracy: " + str(accu_te)) 322 | if print_values is not True: 323 | return accu_te, prec_te, rec_te, f1_te 324 | -------------------------------------------------------------------------------- /neowise/neowise/neural_net.py: -------------------------------------------------------------------------------- 1 | from neowise.activations import * 2 | from neowise.cost_function import * 3 | from neowise.functional import * 4 | from neowise.layers import * 5 | from neowise.optimizers import * 6 | from neowise.plots import * 7 | from tqdm import tqdm 8 | import sys 9 | import time 10 | import hdfdict 11 | from prettytable import PrettyTable 12 | 13 | 14 | class Model: 15 | """ 16 | Model 17 | Puts together all the components of the network and builds a Neural Network 18 | 19 | Arguments: 20 | X_tr: Training data to train the network (nd-array) 21 | y_tr: Output labels of the Training data (nd-array) 22 | X_te: Testing data for the network (nd-array) 23 | y_te: Output labels of the Testing data (nd-array) 24 | X_cv: Cross Validation data for the network (nd-array) 25 | y_cv: Output labels for the Cross Validation data (nd-array) 26 | """ 27 | def __init__(self, X_tr, y_tr, X_te, y_te, X_cv, y_cv): 28 | self.X_tr, self.y_tr = X_tr, y_tr 29 | self.X_te, self.y_te = X_te, y_te 30 | self.X_cv, self.y_cv = X_cv, y_cv 31 | self.layer_names = [] 32 | self.layer_names_arr = [] 33 | self.activations_cache = None 34 | self.params = None 35 | self.arch = {} 36 | self.accu_tr_arr = None 37 | self.cost_tr_arr = None 38 | self.accu_cv_arr = None 39 | self.cost_cv_arr = None 40 | self.lr = None 41 | self.epochs = None 42 | 43 | def add(self, layer_name, num_inputs, num_outputs, act_fn, dropout=1): 44 | """ 45 | Add 46 | Creates a layer object and stores it in a list (self.layer_names) 47 | :param layer_name: Name of the layer being added 48 | :param num_inputs: Number of inputs to the layer 49 | :param num_outputs: Number of units in the layer 50 | :param act_fn: Activation function to be applied for the layer 51 | :param dropout: Dropout value for the layer 52 | """ 53 | self.layer_names_arr.append(layer_name) 54 | self.arch[str(layer_name)] = [layer_name.encode('utf8'), num_inputs, num_outputs, act_fn.encode('utf8'), 55 | dropout] 56 | layer_name = Dense(num_inputs, num_outputs, act_fn, dropout) 57 | Dense.initialize_params(layer_name) 58 | self.layer_names.append(layer_name) 59 | 60 | def reset(self): 61 | """ 62 | Reset 63 | Resets the self.layer_names list 64 | """ 65 | self.layer_names = [] 66 | self.arch = {} 67 | self.layer_names_arr = [] 68 | return self.layer_names, self.arch, self.layer_names_arr 69 | 70 | def params_dict(self, print_params): 71 | """ 72 | Parameters Dictionary 73 | Creates a dictionary of all the parameters (weights and bias) of the network 74 | :param print_params: Boolean value if True, prints the parameters 75 | :return: self.params 76 | """ 77 | self.params = {} 78 | hee = 1 79 | for layer in self.layer_names: 80 | self.params["W" + str(hee)], self.params["b" + str(hee)] = Dense.get_params(layer) 81 | hee += 1 82 | if print_params is True: 83 | print(self.params) 84 | return self.params 85 | else: 86 | return self.params 87 | 88 | def forward_prop(self, X, train_model=True): 89 | """ 90 | Forward Propagation 91 | Propagates the data through the network 92 | :param X: Data to be propagated 93 | :param train_model: Boolean if True, Dropout values will be unchanged, else all Dropout values = 1 94 | :return: activations_cache, a list containing all the activations of all the layers of the network 95 | """ 96 | self.activations_cache = {} 97 | self.activations_cache = {"A0": X.T} 98 | temp_A = X.T 99 | p = 1 100 | for layer in self.layer_names: 101 | _, temp_A = Dense.forw_prop(layer, temp_A, train_model) 102 | self.activations_cache["A" + str(p)] = temp_A 103 | p += 1 104 | return self.activations_cache 105 | 106 | def backward_prop(self, y, prob_type, activations_cache, lamb, reg): 107 | """ 108 | Backward Propagation 109 | Calculates the derivative of the Cost Function w.r.t the weights, bias, activations of all the layers of 110 | the network 111 | :param y: Output labels of the data (nd-array) 112 | :param prob_type: Type of problem ["Binary": For Binary Classification,"Multi": For Multi Class Classification] 113 | (str) 114 | :param activations_cache: Activations cache list put together doing nw.neural_net.forward_prop (list) 115 | :param lamb: Regularization parameter "lamda" (float) 116 | :param reg: Type of Regularization ["L1": For L1 regularization, "L2": For L2 Regularization] (str) 117 | """ 118 | prob_type_dict = {"Binary": [BinaryCrossEntropy, PrecisionRecall, Predict, Evaluate], 119 | "Multi": [CrossEntropy, PrecisionRecallMulti, PredictMulti, EvaluateMulti]} 120 | _, temp_dA = prob_type_dict[prob_type][0](y, activations_cache["A" + str(len(self.layer_names))], 121 | self.layer_names, lamb, reg)() 122 | l = 1 123 | for layer in reversed(self.layer_names): 124 | _, layer.dW, layer.db, temp_dA = Dense.back_prop(layer, temp_dA, self.activations_cache[ 125 | "A" + str(len(self.layer_names) - l)]) 126 | l += 1 127 | 128 | def fit(self, X, y, alpha, num_iter, optim, prob_type, mb, reg=None, lamb=None, alpha_decay=False, print_cost=True, 129 | callback=None): 130 | """ 131 | Fit 132 | Trains the model 133 | :param X: Training data (nd-array) 134 | :param y: Training data output labels (nd-array) 135 | :param alpha: Learning Rate (float) 136 | :param num_iter: Number of Iterations through the data (int) 137 | :param optim: Optimizer for training ["GD":Gradient Descent,"Momentum":Momentum,"RMSprop":RMSprop,"Adam":Adam] (str) 138 | :param prob_type: Type of problem ["Binary":Binary Classification,"Multi":Multi-Class Classification] (str) 139 | :param mb: Mini-Batch size (int) 140 | :param reg: Regularization type ["L1":L1 Regularization,"L2":L2 Regularization] (str) 141 | :param lamb: Regularization parameter (float) 142 | :param alpha_decay: Decrease the learning rate through the number of iterations (bool) 143 | :param print_cost: Whether to print cost or not (bool) 144 | :param callback: Callback for boundary visualisation for binary classification 145 | """ 146 | self.lr = alpha 147 | if (num_iter > 100) and (num_iter <= 1000): 148 | freq = 10 149 | elif num_iter > 1000: 150 | freq = 50 151 | elif num_iter <= 100: 152 | freq = 1 153 | params = self.params_dict(print_params=False) 154 | V_dict = {} 155 | S_dict = {} 156 | mini_batches, num = CreateMiniBatches(X, y, mb)() 157 | self.accu_tr_arr = [] 158 | self.cost_tr_arr = [] 159 | self.accu_cv_arr = [] 160 | self.cost_cv_arr = [] 161 | for k in range(1, len(self.layer_names) + 1): 162 | V_dict["Vdw" + str(k)] = np.zeros(params["W" + str(k)].shape) 163 | V_dict["Vdb" + str(k)] = np.zeros(params["b" + str(k)].shape) 164 | S_dict["Sdw" + str(k)] = np.zeros(params["W" + str(k)].shape) 165 | S_dict["Sdb" + str(k)] = np.zeros(params["b" + str(k)].shape) 166 | optim_dict = {"GD": [GradientDescent, None, None, 0], 167 | "Momentum": [Momentum, V_dict, None, 0], 168 | "RMSprop": [RMSProp, None, S_dict, 0], 169 | "Adam": [Adam, V_dict, S_dict, 0]} 170 | prob_type_dict = {"Binary": [BinaryCrossEntropy, PrecisionRecall, Predict, Evaluate], 171 | "Multi": [CrossEntropy, PrecisionRecallMulti, PredictMulti, EvaluateMulti]} 172 | 173 | for i in range(1, num_iter + 1): 174 | params_plot = self.params_dict(print_params=False) 175 | if alpha_decay is True: 176 | alpha = (np.power(0.95, i)) * self.lr 177 | for vee in tqdm(range(0, num + 1), file=sys.stdout): 178 | activations_dict = self.forward_prop(mini_batches["MB_X" + str(vee)]) 179 | self.backward_prop(mini_batches["MB_Y" + str(vee)], prob_type, activations_dict, lamb, reg) 180 | optim_dict[optim][0](alpha, self.layer_names, optim_dict[optim][1], optim_dict[optim][2], 181 | optim_dict[optim][3] + i)() 182 | act_tr = self.forward_prop(X) 183 | cost_tr, _ = prob_type_dict[prob_type][0](y, act_tr["A" + str(len(self.layer_names))], self.layer_names, 184 | lamb, reg)() 185 | preds = prob_type_dict[prob_type][2](act_tr["A" + str(len(self.layer_names))])() 186 | accu_tr = prob_type_dict[prob_type][3](y, preds)() 187 | self.accu_tr_arr.append(accu_tr) 188 | self.cost_tr_arr.append(cost_tr) 189 | if self.X_cv is not None: 190 | act_cv = self.forward_prop(self.X_cv,train_model=False) 191 | cost_cv, _ = prob_type_dict[prob_type][0](self.y_cv, act_cv["A" + str(len(self.layer_names))], 192 | self.layer_names, lamb, reg=None)() 193 | preds_cv = prob_type_dict[prob_type][2](act_cv["A" + str(len(self.layer_names))])() 194 | accu_cv = prob_type_dict[prob_type][3](self.y_cv, preds_cv)() 195 | self.accu_cv_arr.append(accu_cv) 196 | self.cost_cv_arr.append(cost_cv) 197 | if (i % 1 == 0) and print_cost == True: 198 | if self.X_cv is None: 199 | print("Iteration " + str(i) + " " + "train_cost: " + str( 200 | np.round(cost_tr, 6)) + " --- " + "train_acc: " + str(np.round(accu_tr, 3))) 201 | else: 202 | print("Iteration " + str(i) + " " + "train_cost: " + str( 203 | np.round(cost_tr, 6)) + " --- " + "train_acc: " + str( 204 | np.round(accu_tr, 3)) + " --- " + "val_cost: " + str( 205 | np.round(cost_cv, 6)) + " --- " + "val_accu: " + str(np.round(accu_cv, 3))) 206 | if i % 1 == 0: 207 | if (callback is not None): 208 | callback(i, params_plot) 209 | 210 | def save_model(self, fname): 211 | """ 212 | Save Model 213 | Save the parameters and the architecture of the model for reuse once trained through hdfdict 214 | :param fname: Directory where the model should be saved with filename with .h5 extension 215 | """ 216 | params = self.params_dict(print_params=False) 217 | archi = self.arch 218 | model_dict = {"Parameters": params, "Architecture": archi} 219 | hdfdict.dump(model_dict, fname) 220 | print("Model saved!") 221 | 222 | def load_model(self, fname): 223 | """ 224 | Load Model 225 | Loads the parameters and the architecture of the saved models 226 | :param fname: Directory from where the model saved be opened 227 | """ 228 | print("Model loading....") 229 | model_dict = dict(hdfdict.load(fname)) 230 | params_dict = model_dict["Parameters"] 231 | arch_dict = model_dict["Architecture"] 232 | self.reset() 233 | for key in arch_dict: 234 | self.add(arch_dict[key][0].decode('utf8'), int(arch_dict[key][1]), int(arch_dict[key][2]), 235 | arch_dict[key][3].decode('utf8'), int(arch_dict[key][4])) 236 | dee = 1 237 | for layer in self.layer_names: 238 | layer.weights = params_dict["W" + str(dee)] 239 | layer.bias = params_dict["b" + str(dee)] 240 | dee += 1 241 | print("Model loaded!") 242 | 243 | def plot(self, type_func, animate=False, direc=None, freq=1): 244 | """ 245 | Plot 246 | Plots the graphs of cost functions and accuracy on training and cross val with number of iterations on the X axis 247 | :param type_func: Type Function to be plotted ["Cost": Cost,"Accuracy": Accuracy] (str) 248 | :param animate: Boolean whether to animate the graph or not (bool) 249 | :param direc: Directory where the images should be stored 250 | :param freq: Update frequency of plot animation 251 | """ 252 | itera = np.arange(1, len(self.cost_tr_arr) + 1) 253 | plot_dict = {"Cost": [PlotCostStatic, AnimatePlotMulti, self.cost_tr_arr, self.cost_cv_arr, 'Costs', 254 | 'Train-Cross Val Costs Curve', 'upper right', ['c', '#9ef705'], AnimatePlot, "Costs", 255 | "Cost Function Curve"], 256 | "Accuracy": [PlotTrCvStatic, AnimatePlotMulti, self.accu_tr_arr, self.accu_cv_arr, 'Accuracy', 257 | 'Train-Cross Val Accuracy Curve', 'lower right', ['m', 'r'], AnimatePlot, "Accuracy", 258 | "Accuracy Curve"]} 259 | if animate is False: 260 | plot_dict[type_func][0](self.cost_tr_arr, self.cost_cv_arr, self.accu_tr_arr, self.accu_cv_arr)() 261 | else: 262 | if len(self.cost_cv_arr) != 0: 263 | plot_dict[type_func][1]([itera, itera], [plot_dict[type_func][2], plot_dict[type_func][3]], 264 | 'Number of Iterations', plot_dict[type_func][4], plot_dict[type_func][5], 265 | ['Train', 'Cross Val'], plot_dict[type_func][6], plot_dict[type_func][7], direc, 266 | freq)() 267 | else: 268 | plot_dict[type_func][8](itera, plot_dict[type_func][2], "Number of Iterations", plot_dict[type_func][9], 269 | plot_dict[type_func][10], "Train", plot_dict[type_func][6], 270 | plot_dict[type_func][7][0], direc, freq)() 271 | print("Go to your directory to find the images! Feed them to a GIF creator to animate them!") 272 | 273 | def summary(self): 274 | """ 275 | Summary 276 | Returns an ASCII Table generated by prettytable that contains the architecture of the network 277 | """ 278 | tab = PrettyTable() 279 | tab.field_names = ["Layer Number", "Layer Name", "Inputs", "Outputs", "Activation", "Dropout", 280 | "Number of Parameters"] 281 | yee = 1 282 | total_params = 0 283 | for layer in self.layer_names: 284 | total_params += (layer.weights.shape[0] * layer.weights.shape[1]) + len(layer.bias) 285 | tab.add_row([str(yee), self.layer_names_arr[yee - 1], layer.weights.shape[1], layer.weights.shape[0], 286 | layer.activation_fn, layer.dropout, 287 | str((layer.weights.shape[0] * layer.weights.shape[1]) + len(layer.bias))]) 288 | yee += 1 289 | print(tab) 290 | print("Total number of trainable Parameters: " + str(total_params)) 291 | 292 | def test(self, X, y, prob_type, training=False, print_values=True): 293 | """ 294 | Test 295 | Test the trained model on the test data and return the accuracy, precision, recall and F1 score on test data 296 | :param X: Data (test) 297 | :param y: Output labels of data (test) 298 | :param prob_type: Type of problem ["Binary":Binary Classification,"Multi":Multi-Class Classification] (str) 299 | :param training: Boolean if training is True or False for nw.neural_net.forward_prop (default=False) 300 | :param print_values: Boolean whether to print values or not (default=True) 301 | """ 302 | prob_type_dict = {"Binary": [BinaryCrossEntropy, PrecisionRecall, Predict, Evaluate], 303 | "Multi": [CrossEntropy, PrecisionRecallMulti, PredictMulti, EvaluateMulti]} 304 | act_te = self.forward_prop(X, training) 305 | predictions_te = prob_type_dict[prob_type][2](act_te["A" + str(len(self.layer_names))])() 306 | accu_te = prob_type_dict[prob_type][3](y, predictions_te)() 307 | prec_te, rec_te, f1_te = prob_type_dict[prob_type][1](predictions_te, y)() 308 | tab = PrettyTable() 309 | if prob_type == "Multi": 310 | tab.field_names = ["Class", "Precision", "Recall", "F1"] 311 | for hee in range(0, y.shape[0]): 312 | tab.add_row([hee, prec_te["class" + str(hee)], rec_te["class" + str(hee)], f1_te["class" + str(hee)]]) 313 | if print_values is True: 314 | print(tab) 315 | print("Test Accuracy: " + str(accu_te)) 316 | if prob_type == "Binary": 317 | tab.field_names = ["Precision", "Recall", "F1"] 318 | tab.add_row([str(prec_te), str(rec_te), str(f1_te)]) 319 | if print_values is True: 320 | print(tab) 321 | print("Test Accuracy: " + str(accu_te)) 322 | if print_values is not True: 323 | return accu_te, prec_te, rec_te, f1_te 324 | --------------------------------------------------------------------------------