Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Implementation of Artificial Neural Networks using NumPy

NotificationsYou must be signed in to change notification settings

ahmedfgad/NumPyANN

Repository files navigation

NumPyANN is a Python project for building artificial neural networks using NumPy.

NumPyANN is part ofPyGAD which is an open-source Python 3 library for implementing the genetic algorithm and optimizing machine learning algorithms. Both regression and classification neural networks are supported starting from PyGAD 2.7.0.

Check documentation of theNeuralGenetic project in the PyGAD's documentation:https://pygad.readthedocs.io/en/latest/nn.html

The library is under active development and more features are added regularly. If you want a feature to be supported, please check theContact Us section to send a request.

Donation

Tutorial Project

IMPORTANT If you are coming for the code of the tutorial titledArtificial Neural Network Implementation using NumPy and Classification of the Fruits360 Image Dataset, then it has been moved to theTutorialProject directory on 10 May 2020.

Installation

To installPyGAD, simply use pip to download and install the library fromPyPI (Python Package Index). The library is at PyPI at this pagehttps://pypi.org/project/pygad.

Install PyGAD with the following command:

pipinstallpygad

To get started with PyGAD, please read the documentation atRead The Docshttps://pygad.readthedocs.io.

PyGAD Source Code

The source code of the PyGAD' modules is found in the following GitHub projects:

The documentation of PyGAD is available atRead The Docshttps://pygad.readthedocs.io.

PyGAD Documentation

The documentation of the PyGAD library is available atRead The Docs at this link:https://pygad.readthedocs.io. It discusses the modules supported by PyGAD, all its classes, methods, attribute, and functions. For each module, a number of examples are given.

If there is an issue using PyGAD, feel free to post at issue in thisGitHub repositoryhttps://github.com/ahmedfgad/GeneticAlgorithmPython or by sending an e-mail toahmed.f.gad@gmail.com.

If you built a project that uses PyGAD, then please drop an e-mail toahmed.f.gad@gmail.com with the following information so that your project is included in the documentation.

  • Project title
  • Brief description
  • Preferably, a link that directs the readers to your project

Please check theContact Us section for more contact details.

Life Cycle of PyGAD

The next figure lists the different stages in the lifecycle of an instance of thepygad.GA class. Note that PyGAD stops when either all generations are completed or when the function passed to theon_generation parameter returns the stringstop.

PyGAD Lifecycle

The next code implements all the callback functions to trace the execution of the genetic algorithm. Each callback function prints its name.

importpygadimportnumpyfunction_inputs= [4,-2,3.5,5,-11,-4.7]desired_output=44deffitness_func(ga_instance,solution,solution_idx):output=numpy.sum(solution*function_inputs)fitness=1.0/ (numpy.abs(output-desired_output)+0.000001)returnfitnessfitness_function=fitness_funcdefon_start(ga_instance):print("on_start()")defon_fitness(ga_instance,population_fitness):print("on_fitness()")defon_parents(ga_instance,selected_parents):print("on_parents()")defon_crossover(ga_instance,offspring_crossover):print("on_crossover()")defon_mutation(ga_instance,offspring_mutation):print("on_mutation()")defon_generation(ga_instance):print("on_generation()")defon_stop(ga_instance,last_population_fitness):print("on_stop()")ga_instance=pygad.GA(num_generations=3,num_parents_mating=5,fitness_func=fitness_function,sol_per_pop=10,num_genes=len(function_inputs),on_start=on_start,on_fitness=on_fitness,on_parents=on_parents,on_crossover=on_crossover,on_mutation=on_mutation,on_generation=on_generation,on_stop=on_stop)ga_instance.run()

Based on the used 3 generations as assigned to thenum_generations argument, here is the output.

on_start()on_fitness()on_parents()on_crossover()on_mutation()on_generation()on_fitness()on_parents()on_crossover()on_mutation()on_generation()on_fitness()on_parents()on_crossover()on_mutation()on_generation()on_stop()

Example

Check thePyGAD's documentation for information about the implementation of this example.

importnumpyimportpygad.nn# Preparing the NumPy array of the inputs.data_inputs=numpy.array([[1,1],                           [1,0],                           [0,1],                           [0,0]])# Preparing the NumPy array of the outputs.data_outputs=numpy.array([0,1,1,0])# The number of inputs (i.e. feature vector length) per samplenum_inputs=data_inputs.shape[1]# Number of outputs per samplenum_outputs=2HL1_neurons=2# Building the network architecture.input_layer=pygad.nn.InputLayer(num_inputs)hidden_layer1=pygad.nn.DenseLayer(num_neurons=HL1_neurons,previous_layer=input_layer,activation_function="relu")output_layer=pygad.nn.DenseLayer(num_neurons=num_outputs,previous_layer=hidden_layer1,activation_function="softmax")# Training the network.pygad.nn.train(num_epochs=10,last_layer=output_layer,data_inputs=data_inputs,data_outputs=data_outputs,learning_rate=0.01)# Using the trained network for predictions.predictions=pygad.nn.predict(last_layer=output_layer,data_inputs=data_inputs)# Calculating some statisticsnum_wrong=numpy.where(predictions!=data_outputs)[0]num_correct=data_outputs.size-num_wrong.sizeaccuracy=100* (num_correct/data_outputs.size)print("Number of correct classifications : {num_correct}.".format(num_correct=num_correct))print("Number of wrong classifications : {num_wrong}.".format(num_wrong=num_wrong.size))print("Classification accuracy : {accuracy}.".format(accuracy=accuracy))

For More Information

There are different resources that can be used to get started with the genetic algorithm and building it in Python.

Tutorial: Implementing Genetic Algorithm in Python

To start with coding the genetic algorithm, you can check the tutorial titledGenetic Algorithm Implementation in Python available at these links:

This tutorial is prepared based on a previous version of the project but it still a good resource to start with coding the genetic algorithm.

Genetic Algorithm Implementation in Python

Tutorial: Introduction to Genetic Algorithm

Get started with the genetic algorithm by reading the tutorial titledIntroduction to Optimization with Genetic Algorithm which is available at these links:

Introduction to Genetic Algorithm

Tutorial: Build Neural Networks in Python

Read about building neural networks in Python through the tutorial titledArtificial Neural Network Implementation using NumPy and Classification of the Fruits360 Image Dataset available at these links:

Building Neural Networks Python

Tutorial: Optimize Neural Networks with Genetic Algorithm

Read about training neural networks using the genetic algorithm through the tutorial titledArtificial Neural Networks Optimization using Genetic Algorithm with Python available at these links:

Training Neural Networks using Genetic Algorithm Python

Tutorial: Building CNN in Python

To start with coding the genetic algorithm, you can check the tutorial titledBuilding Convolutional Neural Network using NumPy from Scratch available at these links:

This tutorial) is prepared based on a previous version of the project but it still a good resource to start with coding CNNs.

Building CNN in Python

Tutorial: Derivation of CNN from FCNN

Get started with the genetic algorithm by reading the tutorial titledDerivation of Convolutional Neural Network from Fully Connected Network Step-By-Step which is available at these links:

Derivation of CNN from FCNN

Book: Practical Computer Vision Applications Using Deep Learning with CNNs

You can also check my book cited asAhmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7 which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.

Find the book at these links:

Fig04

Citing PyGAD - Bibtex Formatted Citation

If you used PyGAD, please consider adding a citation to the following paper about PyGAD:

@misc{gad2021pygad,      title={PyGAD: An Intuitive Genetic Algorithm Python Library},       author={Ahmed Fawzy Gad},      year={2021},      eprint={2106.06158},      archivePrefix={arXiv},      primaryClass={cs.NE}}

Contact Us


[8]ページ先頭

©2009-2025 Movatter.jp