Implementation of a Library for Artificial Neural Networks in C

Similar documents
Back propagation Algorithm:

Lecture 2 Notes. Outline. Neural Networks. The Big Idea. Architecture. Instructors: Parth Shah, Riju Pahwa

Supervised Learning in Neural Networks (Part 2)

CS6220: DATA MINING TECHNIQUES

A *69>H>N6 #DJGC6A DG C<>C::G>C<,8>:C8:H /DA 'D 2:6G, ()-"&"3 -"(' ( +-" " " % '.+ % ' -0(+$,

Assignment # 5. Farrukh Jabeen Due Date: November 2, Neural Networks: Backpropation

Neural Nets. CSCI 5582, Fall 2007

Neural Networks. CE-725: Statistical Pattern Recognition Sharif University of Technology Spring Soleymani

CHAPTER VI BACK PROPAGATION ALGORITHM

International Journal of Advanced Research in Computer Science and Software Engineering

Machine Learning 13. week

Neural Network Neurons

Artificial Neural Networks Lecture Notes Part 5. Stephen Lucci, PhD. Part 5

An Algorithm For Training Multilayer Perceptron (MLP) For Image Reconstruction Using Neural Network Without Overfitting.

Image Compression: An Artificial Neural Network Approach

Multilayer Feed-forward networks

11/14/2010 Intelligent Systems and Soft Computing 1

CS 4510/9010 Applied Machine Learning. Neural Nets. Paula Matuszek Fall copyright Paula Matuszek 2016

Character Recognition Using Convolutional Neural Networks

Climate Precipitation Prediction by Neural Network

Week 3: Perceptron and Multi-layer Perceptron

Lecture 20: Neural Networks for NLP. Zubin Pahuja

Neural Network Classifier for Isolated Character Recognition

World Journal of Engineering Research and Technology WJERT

A neural network that classifies glass either as window or non-window depending on the glass chemistry.

Channel Performance Improvement through FF and RBF Neural Network based Equalization

INVESTIGATING DATA MINING BY ARTIFICIAL NEURAL NETWORK: A CASE OF REAL ESTATE PROPERTY EVALUATION

For Monday. Read chapter 18, sections Homework:

Simulation of Back Propagation Neural Network for Iris Flower Classification

CMPT 882 Week 3 Summary

Performance Evaluation of Artificial Neural Networks for Spatial Data Analysis

Backpropagation in Neural Nets, and an Introduction to Vision. CSCI 5582, Fall 2007

Opening the Black Box Data Driven Visualizaion of Neural N

Edge Detection for Dental X-ray Image Segmentation using Neural Network approach

Neural Networks Laboratory EE 329 A

Learning. Learning agents Inductive learning. Neural Networks. Different Learning Scenarios Evaluation

An Integer Recurrent Artificial Neural Network for Classifying Feature Vectors

HANDWRITTEN GURMUKHI CHARACTER RECOGNITION USING WAVELET TRANSFORMS

An Intelligent Technique for Image Compression

Simulation of Zhang Suen Algorithm using Feed- Forward Neural Networks

Unsupervised learning in Vision

Improving Trajectory Tracking Performance of Robotic Manipulator Using Neural Online Torque Compensator

MATLAB representation of neural network Outline Neural network with single-layer of neurons. Neural network with multiple-layer of neurons.

CHAPTER 7 MASS LOSS PREDICTION USING ARTIFICIAL NEURAL NETWORK (ANN)

4.12 Generalization. In back-propagation learning, as many training examples as possible are typically used.

CLASSIFICATION WITH RADIAL BASIS AND PROBABILISTIC NEURAL NETWORKS

1. Introduction. 2. Motivation and Problem Definition. Volume 8 Issue 2, February Susmita Mohapatra

CS 4510/9010 Applied Machine Learning

This leads to our algorithm which is outlined in Section III, along with a tabular summary of it's performance on several benchmarks. The last section

SEMANTIC COMPUTING. Lecture 8: Introduction to Deep Learning. TU Dresden, 7 December Dagmar Gromann International Center For Computational Logic

Asst. Prof. Bhagwat Kakde

Assignment 2. Classification and Regression using Linear Networks, Multilayer Perceptron Networks, and Radial Basis Functions

Artificial neural networks are the paradigm of connectionist systems (connectionism vs. symbolism)

Artificial Neural Network based Curve Prediction

WHAT TYPE OF NEURAL NETWORK IS IDEAL FOR PREDICTIONS OF SOLAR FLARES?

Lecture 17: Neural Networks and Deep Learning. Instructor: Saravanan Thirumuruganathan

Constructively Learning a Near-Minimal Neural Network Architecture

Notes on Multilayer, Feedforward Neural Networks

Review on Methods of Selecting Number of Hidden Nodes in Artificial Neural Network

6. NEURAL NETWORK BASED PATH PLANNING ALGORITHM 6.1 INTRODUCTION

More on Learning. Neural Nets Support Vectors Machines Unsupervised Learning (Clustering) K-Means Expectation-Maximization

Gesture Recognition using Neural Networks

Visual object classification by sparse convolutional neural networks

CS231A Course Project Final Report Sign Language Recognition with Unsupervised Feature Learning

Neural Networks. Robot Image Credit: Viktoriya Sukhanova 123RF.com

DEEP LEARNING REVIEW. Yann LeCun, Yoshua Bengio & Geoffrey Hinton Nature Presented by Divya Chitimalla

Neural Networks (Overview) Prof. Richard Zanibbi

FITTING PIECEWISE LINEAR FUNCTIONS USING PARTICLE SWARM OPTIMIZATION

USING IMAGES PATTERN RECOGNITION AND NEURAL NETWORKS FOR COATING QUALITY ASSESSMENT Image processing for quality assessment

COMBINING NEURAL NETWORKS FOR SKIN DETECTION

CSC 578 Neural Networks and Deep Learning

Machine Learning Classifiers and Boosting

LECTURE NOTES Professor Anita Wasilewska NEURAL NETWORKS

A Novel Technique for Optimizing the Hidden Layer Architecture in Artificial Neural Networks N. M. Wagarachchi 1, A. S.

Radial Basis Function Neural Network Classifier

ESTIMATION OF SUBSURFACE QANATS DEPTH BY MULTI LAYER PERCEPTRON NEURAL NETWORK VIA MICROGRAVITY DATA

Data Mining. Neural Networks

COMP 551 Applied Machine Learning Lecture 14: Neural Networks

Instantaneously trained neural networks with complex inputs

In this assignment, we investigated the use of neural networks for supervised classification

Perceptrons and Backpropagation. Fabio Zachert Cognitive Modelling WiSe 2014/15

IEEE TRANSACTIONS ON NEURAL NETWORKS, VOL. 10, NO. 6, NOVEMBER Inverting Feedforward Neural Networks Using Linear and Nonlinear Programming

Artificial Intelligence Introduction Handwriting Recognition Kadir Eren Unal ( ), Jakob Heyder ( )

Global Journal of Engineering Science and Research Management

Fingerprint Feature Extraction Based Discrete Cosine Transformation (DCT)

Introduction to Multilayer Perceptrons

Neuron Selectivity as a Biologically Plausible Alternative to Backpropagation

IMPROVEMENTS TO THE BACKPROPAGATION ALGORITHM

II. ARTIFICIAL NEURAL NETWORK

Applying Neural Network Architecture for Inverse Kinematics Problem in Robotics

A faster model selection criterion for OP-ELM and OP-KNN: Hannan-Quinn criterion

CSI5387: Data Mining Project

Detecting Spam with Artificial Neural Networks

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CS 4510/9010 Applied Machine Learning. Deep Learning. Paula Matuszek Fall copyright Paula Matuszek 2016

CHAPTER 8 COMPOUND CHARACTER RECOGNITION USING VARIOUS MODELS

Exercise: Training Simple MLP by Backpropagation. Using Netlab.

Multi-Layered Perceptrons (MLPs)

Ensemble methods in machine learning. Example. Neural networks. Neural networks

Improving the Performance of Adaptive Secured Backup Ad-hoc Routing protocol by Artificial Neural Networks in Wireless Ad-hoc Networks

Transcription:

Implementation of a Library for Artificial Neural Networks in C Jack Breese TJHSST Computer Systems Lab 2007-2008 June 10, 2008 1 Abstract In modern computing, there are several approaches to pattern recognition and object classification. As computational power has increased, artificial neural networks have become ever more popular and prevalent in this regard. Over the course of the year, I implemented a general-purpose library for artificial neural networks in the C programming language. It includes a variety of functions and data structures for use in creating, working with, and training simple perceptron-type neural networks using a backpropagation heuristic. Though it is not fully capable of training a neural network in its current iteration, this library would serve as a useful starting point for further exploration in the field of machine learning. 2 Background 2.1 Defining Neural Networks An artificial neural network is a computational model which emulates the biological structure of the brain. It is a system of interconnected virtual neurons. In a perceptron, the type of network which is being implemented in this project, the network is organized into a hierarchy of layers. The first layer, or input layer, has one neuron for each input value. Each of these neurons is connected to every neuron in the next layer, the hidden layer. The neurons in this hidden layer are, in turn, connected to each of the neurons in the final, or output layer. The vast computational utility of these artificial neural networks stems from the adaptability of the system. After initialization, the network can be trained. As these networks are capable of modifying their connections, adapting their responses based on their training, they become increasingly more accurate as they are trained. This modeling of biological networks has been used in an ever-increasing number of fields. 1

2.2 Uses of Neural Networks Neural networks have found use in a large number of computational disciplines. In the field of object and pattern recognition, neural networks are commonly used to perform such tasks as optical character recognition and face recognition. Pattern analysis is another field in which neural networks excel. Bayesian neural networks, for example, have become quite commonplace in filtering for e-mail spam, growing more effective every time a message is marked as such. In statistics, neural networks can be used to approximate functions and to interpolate missing data. 2.3 Neural Network Structure Figure 1 is a diagram of the basic layout of a two-layer perceptron neural network. In a task such as face recognition, a neuron is created in the input layer for each discrete pixel value in the input image. The hidden layer is a layer of neurons used only in calculation. Each neuron adds up the values in the preceding layer, multiplied by the weight of each connection in the weight matrix. This process is then repeated for the output layer, where, after being processed by the sigmoid function, the output will be a value between 0 and 1, with 1 corresponding to 100% certainty that the input image matches the condition for that specific output neuron 2.4 Mathematics and Training of Neural Networks Perceptron neural networks are broken up into a hierarchy of layers. The first layer, or the input layer, is relatively simple. It contains one neuron for each input value the network is to have. Each of these neurons is then connected to every neuron of the second layer. This second, or hidden, layer allows for the majority of the computational power of these networks. Each neuron is connected to every neuron in the previous layer. These connections are given a weight. The hidden layer neurons take the sum of the values of the connected neurons, multiplied by their respective weights. This sum is then fed into the activation function, in most cases the sigmoid function f(x) = 1 1+e x. The value of the neuron is then the result of this function. These values are passed along another series of weighted connections to the network s output layer. The output layer contains one neuron for each condition the network is checking for. These neurons function like those of the hidden layer, and the result of the sigmoid function yields the probability that this condition has been satisfied. sgm(x) = 1 1 + e x (1) V alue = sgm( (NeuronV alue) (ConnectionW eight)) (2) In training neural networks, a large set of input data is assembled. The network is initialized with random weights at first, and the data is then fed into the network. As each data is tested, the result is checked. The square of the 2

Figure 1: A sample neural network. difference between the expected and actual result is calculated, and this data is used to adjust the weights of each connection accordingly. The accuracy of neural networks is mostly a function of the size of their training set rather than their complexity, as shown by a 1964 study. A minimum number of neurons, connections, and layers is required for a perceptron to begin modeling accurately. In this study, the number of neurons in the hidden layer of a perceptron was reduced by 7 8, yet the network still achieved 80% accuracy after being subjected to the same training. It is interesting to note that, in the implementation of neural networks, the output complexity can have a direct impact on the required training time. In a 2001 study, it was found that, in neural networks for optical character recognition, an array of 26 networks of one output each could achieve the same accuracy as a single network with 26 outputs, but with vastly reduced training time. The implications of this study suggest that it may be advantageous to break the pattern classification task into smaller subunits in order to decrease the training time required. 3 Project Development This project is coded entirely in C. It has been packaged as a set of header files so that other programs can make use of its methods and data structures. A full method listing is available in appendix A. It includes methods to initialize double-layer perceptron networks of arbitrary size, limited only by the quantity of allocatable memory. It can also calculate the sigmoid activation function and propagate values through the network. In additon, methods exist to adjust 3

Figure 2: The sigmoid function. weights in training based on a classical backpropagation algorithm, as well as read in pgm formatted image data to the network. The library also includes several data structures for keeping track of the network and training information, as well as methods to save and load neural network weights and its own file format. 3.1 Data Structures In order to implement a neural network effectively, my program makes extensive use of the C programming language s structs. As shown in the code example, there is a neuron struct, which holds the value of each neuron, and a pointer to an array of connection structs. Each connection struct holds a float value for its weight, and a pointer to the neuron from which it originates. typedef struct _connection { float weight; struct _neuron * from; } connection; typedef struct _neuron { float d; connection * cons; }neuron; neuron* mkneuron(int c) { neuron* n = malloc(sizeof(neuron)); n->d = 0; connection * a = malloc(c*sizeof(connection)); n->cons = a; return n; } 4

The library also includes structures which contain PGM formatted image data and attributes for use in training, as well as a structure which keeps track of all available training data. The structures of my network are summarized in appendix A. 3.2 Neural Network Initialization The use of structs in my program greatly simplifies the network initialization process. The initnet method creates an array of pointers to neuron structs of the specified size, and then returns it. After the memory is allocated and the structure set up, the program then calls a separate method in order to populate the network with random weights and begin propagating image data through it. A pseudocode implementation of this initialization follows. Function initnet: For each layer in the net: Allocate enough memory for each neuron in the layer For each neuron in the layer: Connect to each neuron in the previous layer, if any. 3.3 Network Algorithms For each connection to the previous layer: Set a random weight. The following are pseudocode versions of other algorithms used in my library, in order to better illustrate the functions of the network. Calculating Neuron Values Training For each neuron in the previous layer: Sum += neuron_weight*neuron_value neuron_value = activation_function(sum) For each neuron in the network: For each connection to the neuron: weight = random_value() Until desired accuracy is reached: 5

For each example in the training data: actual_out = run_network(example) exp_out = calculate_expected(example) error = exp_out actual out For each neuron in the network: calculate_delta_weights(error) adjust_weights(neuron) 3.4 File Format In order to accurately save a network, my program uses its own file format. It saves the sizes of each of the layers of the network, and then iterates over the weights of each of the connections of every neuron, one layer at a time. By foregoing markers in the output file, and instead relying on the loading method iterating in the same way, it was possible to drastically cut the required file size and to greatly reduce the time spent on file I/O operations. 3.5 Running Behavior Currently, my program initializes an array of pointers to an input layer, an array of pointers to a hidden layer, and an array of pointers to an output layer. It then iterates over these arrays, creating the connections between these neurons and assigning them randomly generated or user-specified weights. After initializing these connections, it then feeds in testing data to the input layer (at this point, all zeros or ones), and iterates over each neuron in the network, summing the values of the weighted connection and calculating the value of the sigmoid function to pass on to the next layer. It then saves the network to a specified file, so that it can be reinitialized in its current state in the future. Once saved, it then reads in image data, feeds it into the network, and propagates it through. It then waits for user input, in the form of a list of filenames for use in training. 3.6 Results of Testing: Problems and Solutions In the development of my neural network framework, I encountered significant delays due to unexpected segmentation faults. Initially, as I went to calculate the value of my output, my program would add the weighted connections of the first few neurons, and then crash with a segmentation fault. Further analysis revealed that my program was not successfully iterating through the array of connection structs. It was only iterating through the first few, and then receiving a seemingly random memory address. Further analysis of my code revealed that this was a simple error on my part, as I had not correctly allocated the memory for this array. After rewriting a single line of code to correctly allocate a region of memory for this array, my program began to function as expected. //Incorrect Code. neuron* mkneuron(int c) { 6

} neuron* n = malloc(sizeof(neuron)); n->d = 0; a = *connection[c]; n->cons = a; return n; //Correct Code. neuron* mkneuron(int c) { neuron* n = malloc(sizeof(neuron)); n->d = 0; connection * a = malloc(c*sizeof(connection)); n->cons = a; return n; } 3.7 Training Issues Though my library has been successfully implemented, I was unable to create a testing program which trained it to recognize faces. I created and sanatized a training data set based on one released by the Carnegie Mellon University CS department, but encountered problems with null termination, and was unable to create an array of filenames which correctly corresponded with expected network output values. The library is, however, functional in every other regard. 4 Results This library currently serves as a full implementation of all methods necessary for the creation, management, and training of arbitrarily sized two-layer perceptron neural networks. Though it includes the methods and data structures needed in this regard, is has not yet been used to successfully train a neural network, due to the limitations of the C programming language mentioned earlier. Though actually training the network is an exercise left up to the end-user, this library still serves as an excellent starting point for future exploration in the field of neural networks and machine learning. It has been packaged as a set of C header files and released under a permissive open-source license, so that it may prove useful to others working in a similar field. 7

Appendix A Methods neuron* mkneuron(int c) This method allocates memory for a neuron. C is the number of neurons in the previous layer that will connect to it. Neuron* initlayer(int size, int plsize) This method instantiates a layer of size size, with a weight matrix stored for the layer before it, of size plsize. void calcvalue(neuron* n, int s) This method calculates the value for a neuron based on the weight matrix it stores and the values of the previous layer. It should only be called by the calcnet method, never on its own. void calcnet(neuron* i, neuron* h, neuron* o) This method propagates the calculations through the entire neural network, calling the calcvalue method on each neuron. void readtrain(char* filename, traininfo * tin, char* zval) This method reads training data from a structured file, filename, and reads it into tin. Zval is used in calculating the expected outputs for a given training data, and is the type of input file that should produce an output of zero. void readpgm(char* filename, pimg* pgm) This method reads in any valid pgm image, filename, to a pimg struct (which must first be allocated.) It is used in reading in image data to train and test the neural network. void backprop(neuron* i, neuron* h, neuron* o, double r, double e) This method performs backpropagation in order to adjust the weights on the neural network during the training process. R is the rate at which training should be performed, and e is the error from what is expected. 8

void saveweights(neuron* i, neuron* h, neuron* o) This method saves the weight matrices of each layer in the neural network, so that it can be loaded and used again after training. double act(double x) This method is the sigmoid activation function pictured in the report. Data Structures connection This struct holds a double weight, and a neuron pointer. Each neuron holds an array of connections pointing to the layer preceding it, in order to store the weight matrix. Neuron This struct holds a double, d, which stores the neurons value,a double err, which stores the calculated error for training use, and an array of connections. pimg This struct stores a valid pgm formatted image file, including all attributes, as well as raw ASCII formatted image data. TrainInfo This struct stores an array of training image files, as well as an array of doubles which holds the expected values for each input file. 9

References [1] Philippe Crochat and Daniel Franklin. Back-propagation neural network tutorial, April 2003. [2] Herman H Goldstine. Computers and perception. Proceedings of the American Philosophical Society, 108:282 290, 1964. [3] Amit Gupta and Monica S Lam. Estimating missing values using neural networks. The Journal of the Operational Research Society, 47:229 238, February 1996. [4] J. J Hopfield. Learning algorithms and probability distributions in feedforward and feed-back networks. Proceedings of the National Academy of Sciences of the United States of America, 84:8429 8433, December 1987. [5] John J Hopfield and David W Tank. Computing with neural circuits: A model. Science, 233:625 633, 1986. [6] B. D Ripley. Neural networks and related methods for classification. Journal of the Royal Statistical Society. Series B (Methodological), 56:409 456, 1994. [7] Matee Serearuno and Tony Holden. A comparison in training time of the single and multiple-output mlp neural networks. SAC, 0:32 35, January 2001. [8] Brad Warner and Manavendra Misra. Understanding neural networks as statistical tools. The American Statistician, 50:284 293, November 1996. 10