Qnet 2000

Source Code for Network Recall

The source file QNETSOLV.C can be used to incorporate trained networks into your own C or C++ applications. Only three functions need to be called to perform network recall:

1)NETDEF *init_net( char *NetFileName );

2)void net_output( NETDEF *net, float *inputs, float *outputs );

3)void free_net( NETDEF *net );

The routine, init_net, is used to initialize and allocate the neural network defined in the Qnet network definition file passed through the argument list. The routine returns a pointer to the C structure that defines the network. The user does not need to allocate or access members of this structure, simply define a pointer to NETDEF and init_net allocates and initializes the needed information. The routine net_output computes the network output. Arguments passed are the pointer to NETDEF, a pointer to a float array that contains 1 sequence of input patterns and a pointer to a float array that will contain the network outputs upon return from net_output. The order of values in the input array must be the same order that was used to train the network. At this point, you may write, save, or do whatever with the outputs. If you need to call net_output multiple times, simply loop through the desired number of calls, changing the “inputs” array before each call. The free_net routine frees all memory allocated for the network and should be called when all input patterns have been processed through the network.

A template of how a trained network is incorporated into sample C code is shown below.

#include <stdio.h>

#include <stdlib.h>

#include "qnetsolv.h"

//number of input nodes for network

#define NINPUTS 10

//number of output nodes for network

#define NOUTPUTS 2

main()

{

float inputs[NINPUTS], outputs[NOUTPUTS];

NETDEF *network;

// INITIALIZE NETWORK

network = init_net("C:\\QNET\\TRAINED.NET");

// If multiple input cases for recall, add loop here!

// Read/set input values into float inputs[] array. The number

// of inputs must be equal to the number of input nodes

// and they must be in same order that was used for

// network training.

// COMPUTE OUTPUTS

net_output( network, inputs, outputs );

// Write or use outputs. The number of output values is

// equal to the number of output nodes. The order is the

// same as the target data used for training.

// End Loop for multiple inputs here!

// FREE NETWORK STORAGE

free_net(network);

}