A Neural Network in C Language

The C programming language was one of the first languages I learned. My other first languages before C were COBOL and Fortran (ugh to them), BASIC (loved it), and Pascal (loved it; where I learned pointers).

I don’t write C programs very much these days, but I like the language and so every now and then I code up something for fun. So, one Sunday morning I set out to implement a neural netwok in C. I was surprised that my C skills were still pretty good and I had my demo program up and running in a couple of hours.

It was difficult to choose a design but in the end I used a typedef-struct to hold the key data structures of the hidden and output nodes (no explicit input nodes in my design), the input-hidden and hidden-output weights, and the hidden and output biases.

typedef struct {
  int ni, nh, no;
  float *h_nodes, *o_nodes; 
  float **ih_wts, **ho_wts;
  float *h_biases, *o_biases;
} nn_t;

After that, I had to refresh my memory for C language dynamic memory allocation using malloc() and a few other details, and then it was just a question of writing-testing-debugging.

To verify my code was working correctly, I used Keras to create and train a 4-5-3 neural network for the ubiquitous Iris Dataset problem, fetched the resulting model weights, placed them into my C version, and checked that the output was the same (which it was).

Although beauty is in the eye of the beholder, I find the C language to be very beautiful — simple and logical — and a good example of how design-by-one-person (or two people in the case of C) is often better than design-by-committee.

int main()
{
  printf("\nBegin neural net with C demo \n");

  nn_t net;
  construct(&net, 4, 5, 3);

  float wts[43] = {
    0.7555, -0.0460,  0.3509, -0.3423, -0.5747,
    0.6113, -0.7757,  0.6457, -0.8525, -0.8164,
   -0.3907, -0.1120,  0.0006,  0.5239,  1.0379,
   -0.1949, -0.2606, -0.7172, -0.8620,  1.0875,

    0.0021, -0.0225,  0.0019, -0.0558, -0.1134,

   -0.7609,  0.1546,  0.4329,
   -0.7539,  0.4817,  0.8748,
   -0.7350, -0.6023, -0.6515,
   -0.0345, -0.8043, -0.0536,
   -1.9457,  0.3096,  2.2589,

   -0.0784,  0.1829, -0.1045 };
  
  set_weights(&net, wts);
  printf("\nneural net: \n");
  show(net);

  float inpts[4] = { 6.1, 3.1, 5.1, 1.1 };
  int i;
  printf("\ninput: \n");
  for (i = 0; i < 4; ++i)
    printf("%0.1f ", inpts[i]);

  float* probs = eval(net, inpts);

  printf("\n\noutput: \n");
  for (i = 0; i < 3; ++i)
    printf("%0.4f ", probs[i]);
  printf("\n");
  free(probs);

  free_nn(&net);
  return 0;
}


Sometimes beauty (and not-beauty) are not in the eye of the beholder — they’re painfully obvious.

Advertisements
This entry was posted in Machine Learning. Bookmark the permalink.

One Response to A Neural Network in C Language

  1. PGT-ART says:

    Did it execute much faster in C ?
    (since people usually claim C is fastest).

Comments are closed.