Skip to content

Instantly share code, notes, and snippets.



Created Feb 18, 2020
What would you like to do?
The C Neural Network Toolkit - Include File
(c) 1993,1994 Ian Clarke
#define train teach
struct node
float error,input,output,correct_output,threshold,change_in_threshold;
short type;
const signed short input=-1;
const signed short hidden=0;
const signed short output=1;
struct connection
short start,end;
float weight,change_in_weight;
/* Procedure prototypes */
void run_net(struct node *nodes,struct connection *conns,int n,int c);
float af(float value);
void reset_net(struct node *nodes,struct connection *conns,int n,int c);
void teach_net(struct node *nodes,struct connection *conns,int n,int c
,float learning_rate);
float derror(float,float);
void scramble_net(struct node *nodes,struct connection *conns,int n,int c);
void make_changes(struct node *nodes,struct connection *conns,int n,int c);
/* This procedure causes the energy to flow from the bottom of
the net to the top*/
void run_net(struct node nodes[],struct connection conns[],int n,int c)
int x;
for (x=0;x<n;x++)
for (x=0;x<c;x++)
if ((nodes[conns[x].start].output==0)&(nodes[conns[x].start].type!=input))
/* This is the function used to determine the response of a node to input*/
float af(float input)
float output;
output=(float) (1/(1+exp(-input)));
return output;
/* Reset 'change_in_weight' and 'change_in_threshold' */
void reset_net(struct node nodes[],struct connection conns[],int n,int c)
int counter;
for (counter=0;counter<n;counter++)
for (counter=0;counter<=c-1;counter++)
/* Teach net */
void teach_net(struct node nodes[],struct connection conns[],int n,int c,float lr)
int x;
if (nodes[conns[x].end].type==output)
float derror(float error,float output)
float otpt;
return otpt;
void make_changes(struct node nodes[],struct connection conns[],int n,int c)
int x;
void scramble_net(struct node nodes[],struct connection conns[],int n,int c)
int p;
nodes[p].threshold=((int) drand48()*4)-2;
conns[p].weight=((int) drand48()*4)-2;
The C Neural Network Tool-Kit
By Ian Clarke
NOTE: This is a hastily prepared guide to the use of the C Neural
Network toolkit, I expect it to be full of errors so please email me
when you find them.
****************Intended use of the C Neural Network Toolkit*******************
The C Neural Network Tool-Kit (CNNTK) is an assortment of functions,
designed to allow any C programmer to use the power of Neural Networks in
his or her programs. The program will include detailed documentation to
allow the user to learn about neural networks, they will not even need to
know what a neural network is before they read the instruction files!
***************************Technical Specifications****************************
CNNTK is fully ANSI compatible, allowing it to be used under any operating
system with a ANSI C compiler. The type of network modelled is a
multi-layer feed-forward net, using the generalised delta, or backward
error propogation learning algorithm. The program would heavily benifit
from a maths co-processor if available(and if the compiler used can detect
and make use of it).
CNNTK was developed using Lattice C V5.52 on an Atari ST/Falcon computer.
Also available is a program 'The Neural Network Construction Kit', by the
same author, but currently available for the Atari ST/TT/Falcon range of
computers only.
*******************************Internal Design*********************************
The Neural Network is contained within 2 arrays.
The first array is of 'node' structures. The node structure contains
the following elements:
*float output
This element contains the output of the node in question, this will
contain the output of output nodes, and should be set to the input of
input nodes
*short type
This contains the type of the node, use the constants input, hidden or
output to set this to the correct value
*float correct_output
This should contain the correct output of a node, ie. what the node
should output. This should be set before teach_net is called.
float error
This is for the learning algorithm and need not be touched by the user
float threshold
This is the nodes threshold, and need not be touched by the user
float input
This is the input to a node, DON'T USE THIS TO SET THE INPUT OF INPUT
The second array is of 'connection' structures and contains
information concerning the connections.
*short start
This is the node at which the connection starts
*short end
This is the node at which the connection ends
short weight
This is the weight of the connection and need not be touched by the user
short change_in_weight
This is used by the training algorithm and need not be touched by the
The high level functions (in contact with the users program) are:
*NODE is a pointer to an array of node structures
*CONNECTION is a pointer to an array of connection structures
NODES is the amount of nodes
CONNECTIONS is the amount of connections
This function will run through one cycle of the neural network and set the
output of the output nodes( NODE[x].output ) according to the output of the
input nodes (also NODE [x].output )
This function will adjust the weights of the connections and thresholds
of the nodes to bring the actual output( NODE[x].output ) of the output
nodes, closer to the desired output( NODE[x].correct_output )
This resets connection weights and node thresholds to random values
This is called after one training cycle to implement the changes in
weights and thresholds which have been calculated during the training
After make_changes has been called, this must be called to reset the
change_in_weight and change_in_threshold values
What follows is the general structure of a CNNTK program
1)#include "cnntk1.c" >Include C Neural Network Toolkit File
Define node and connection arrays (normally global)
2)Set up connections between nodes (by setting .start and .end values
in connection structures
3)Set types of nodes (by setting .type component of node structures to
hidden, input or output (these are defined in cnntk1.c)
4)Call scramble_net
Training Cycle>>
5)Set .output values of input nodes to first/next item of input data
6)Set .correct_output values of output nodes
7)Call run_net
8)Call teach_net
9)If not last item of input data return to 5)
10) Call make_changes to implement changes
11) If not last training cycle return to stage 5)
To run the net>
1) Set .output values of input nodes to first/next item of input data
2) Call run_net
3) Output of net will be in .output of output nodes
4) If not last item of input data return to stage 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment