HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Implementation of Single Layer Perceptron Learning Algorithm in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
layerimplementationlearningalgorithmsingleperceptron

Problem

I have implemented a working version of perceptron learning algorithm in C. Right now, it only works on single layer perceptrons and only takes two inputs.
I plan on making it work with more than two inputs, but want to make sure I'm doing everything right first.

Here is the tutorial I used: https://www.spicelogic.com/Blog/Perceptron-Artificial-Neural-Networks-10

The training data is from a data file called "training_data.txt".
In the data file:

  • first number in each line is first input



  • second number in each line is second input



  • third number in each line is the class.



The input is meant to be used by a camera on a vehicle to detect if an object is a pedestrian or another vehicle.

In the data file:

  • first number in each line is first input -- height to width ratio of


object

  • second number in each line is second input -- how reflective


the object is

  • third number in each line is the class -- 1 is a car, 2


is a person

Here are my files:

main.c:

```
/*/
/ This is a program that implements the preceptron learning /
/ algorithm -- single layer. It gets the training set from /
/ the file training_data.txt (change the macro to use a /
/ different file). Right now it only works with two inputs. /
/*/

/****/
/ How the data file works: /
/ ^Each line is a new set /
/ ^the first number is input1 /
/ ^the second number is input2 /
/ ^the third number is the class /
/ means the end of data /
/****/

#include
#include
/ for rand() /
#include
#include
/ for sleep /
#include
/ for type bool /

#include "functions.h"
/ all function prototypes are here /

/ _______________ /
/ /
/ MACROS /
/ _______________ /
#define THRESHOLD .5
#define DATA_FILE "training_data.txt"
#define LEARNING_RATE

Solution

Portability

With the exception of the call to sleep() this program compiles on the Windows 10 operating system using Visual Studio 2019, to improve the portability you might want to write your own implementation of sleep for non Linux systems. Before including unistd.h check to see if the code is compiling on Linux.
Include Guards

The header file functions.h is missing an include guard. Include guards prevent header files from being included multiple times. This is important because it can prevent compilation errors and prevent multiple definitions of constants. The most portable form of include guards are:

#ifndef CONSTANT
#define CONSTANT
// necessary definitions
#endif


Some C compilers also support #pragma once.
Write Self Document Code

One of the largest problems with successful software is that it needs to be maintained. A successful program will receive many features requests and some of those requests will be implemented. The maintenance may not be performed by the original developer. As part of this maintenance, comments and documentation will need to be maintained. Comments are important in code to explain why an algorithm is written the way it is, and generally that is the only reason to write comments. It is better to write self-documenting code as much as possible to reduce the amount of comments necessary.

Comments such as:

/* _______________ */
/*                 */
/*     MACROS      */
/* _______________ */


/* ___________________ */
            /*                     */
            /*     CALCULATION     */
            /* ___________________ */

            /* sum the weighted inputs */


and

/* ___________________ */
    /*                     */
    /*     FILE I/O        */
    /* ___________________ */

    /* open training_data.txt for reading */


waste space in the source file and will require unnecessary maintenance. The main() function is almost 200 lines of code and comments, which is generally more than 3 screens in most editors or IDEs, a general best practice in programming is that no function should be larger than a single screen because of reading comprehension, it is very difficult to understand functions larger than a single screen. Being larger than a single screen increases the risks of bugs in the code and decreases the maintainability of the code. I was able to reduce the size of main() by almost 100 lines by deleting unnecessary comments.
Code Flexibility

The program would be more flexible or usable if the user passed the name of the file in on the command line rather than having the name of the file hard coded. It might also be better to input the threshold and bias in the command line as well.
Declare the Variables as Needed

In the original version of C back in the 1970s and 1980s variables had to be declared at the top of the function. That is no longer the case, and a recommended programming practice to declare the variable as needed. In C the language doesn't provide a default initialization of the variable so variables should be initialized as part of the declaration. For readability and maintainability each variable should be declared and initialized on its own line.

Code Snippets

#ifndef CONSTANT
#define CONSTANT
// necessary definitions
#endif
/* _______________ */
/*                 */
/*     MACROS      */
/* _______________ */
/* ___________________ */
            /*                     */
            /*     CALCULATION     */
            /* ___________________ */

            /* sum the weighted inputs */
/* ___________________ */
    /*                     */
    /*     FILE I/O        */
    /* ___________________ */

    /* open training_data.txt for reading */

Context

StackExchange Code Review Q#160361, answer score: 2

Revisions (0)

No revisions yet.