Page 25 - MSDN Magazine, July 2018
P. 25

#include <stdio.h>
#include <stdlib.h>
#include <math.h> // Has tanh()
typedef struct {
int ni, nh, no;
float *h_nodes, *o_nodes; // No i_nodes float **ih_wts, **ho_wts;
float *h_biases, *o_biases;
} nn_t;
The program defines the following functions: construct(): initialize the struct
free(): deallocate memory when done set_weights(): assign values to weights and biases softmax(): the softmax function
predict(): implements the NN IO mechanism show_weights(): a display helper
The main advantage of using a custom C/C++ IO function is conceptual simplicity.
The key lines of code in the demo program main function look like:
nn_t net; // Neural net struct
construct(&net, 4, 5, 3); // Instantiate the NN
float wts[43] = { // specify the weights and biases 0.2680, -0.3782, -0.3828, 0.1143, 0.1269,
.. .
-0.0466, 0.4528, -0.4062 };
set_weights(&net, wts); // Copy values into NN float inpts[4] = { 6.1, 3.1, 5.1, 1.1 }; // Inputs int shownodes = 0; // Don’t show
float* probs = predict(net, inpts, shownodes);
The point is that if you know exactly how a simple neural network ML model works, the IO process isn’t magic. You can implement basic IO quite easily.
The main advantage of using a custom C/C++ IO function is conceptual simplicity. Also, because you’re coding at a very low level (really just one level of abstraction
above assembly language), the gener-
Figure 3 Simulation of Custom C/C++ IO Code on an IoT Device
activation is very easy to implement—taking only about one day to one week of development effort, depending on many factors, of course. A deep neural network with several hidden layers is somewhat easy to deal with—maybe a week or two of effort. But implementing the IO functionality of a convolutional neural net- work (CNN) or a long, short-term memory (LSTM) recurrent
iris nn.py (Python Code File)
(produces)
iris_cntk.model (Binary File)
ELL tool cntk_import.py
iris_cntk.ell (JSON File)
ELL tool wrap.py
.\host\build (C++ Files)
cmake.exe
iris_cntk (Python Module)
(used in)
use_iris_ell_model.py (Python)
ated executable code will typically be very small and run very fast. Addition- ally, because you have full control over your IO code, you can use all kinds of tricks to speed up performance or reduce memory footprint. For exam- ple, program test.c uses type float but, depending on the problem scenario, you might be able to use a custom 16-bit fixed-point data type.
The main disadvantage of using a custom C/C++ IO approach is that the technique becomes increasingly diffi- cult as the complexity of the trained ML model increases. For example, an IO function for a single hidden layer neural network with tanh and softmax msdnmagazine.com
CNTK Model
Darknet Model
TensorFlow Model ONNX Model
.ell File
dev machine
IoT device
July 2018 19
Prediction Model for Minimal PC
Prediction Model for Raspberry Pi
Prediction Model for Cortex M4
Prediction Model for iPhone
Figure 4 The ELL Workflow Process, High-Level and Granular


































































































   23   24   25   26   27