Encog for C
Encog for C is an Encog project that brings some of the Encog functionality to the C/C++ programming languages. Some of the features supported by Encog for C include:
- Resilient Propagation Training
- Particle Swarm Optimization Training (with CUDA GPU support)
- Nelder Mead Training
- Feedforward Neural Networks
- Support for EGB Files and EG Files
Encog for C does not currently support non-neural network learning or the Encog Analyst.
Contents |
Installing Encog for C
Before you can use Encog for C/C++ you must install it on your platform. Because the C programming language produces platform specific binaries, we distribute Encog for C in source-code format. At some point we will probably release binaries for some of the popular platforms. Complete installation instructions are provided for Encog for C on most major platforms. For more information refer to the article Installing Encog for C.
Command Line
Encog for C provides a command line utility. You can use this command line utility to access most of the features of Encog for C, without the need to actually program in the C programming language. The command line tool is named encog-cmd. If you run encog-cmd you will see basic instructions for use, as you can see here.
heaton:encog-c jheaton$ ./encog * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Usage: encog xor - perform a simple XOR train encog benchmark - perform a benchmark encog train [eg file] [egb file] - train encog egb2csv [egb file] [csv file] - convert egb to csv encog csv2egb [csv file] [egb file] - convert csv to egb encog cuda - Test to see if CUDA(GPU) is working encog randomize [eg file] - Randomize the neural network in this file encog error [eg file] [egb file] - Calculate the error for this network with the training file Options: /input:## The number of inputs, default=10. /ideal:## The number of ideals, default=1. /records:## The number of records, default=10000. /iterations:## The number of iterations, default=100. /threads:## The number of threads, default=0 (match core count). /train:type (where type is PSO, NM or RPROP), default=pso. /gpu: enable/disable, default=enable. /particles:## Particle Swarm Optimization(PSO): particle count, default=30. /inertia:## PSO: The inertia, default: 0.4. /c1:## PSO: C1, default: 2.0. /c2:## PSO: C2, default: 2.0. /maxpos:## PSO: Maximum position, default=-1 (no max pos). /maxvel:## PSO: Maximum velocity, default=2. /step:## Nelder Mead (NM): The step value, default:10.0. /konverge:## NM: The konverge value, default:100. /reqmin:## NM: The required minimum, default:1.0e-16f. Encog Finished. Run time 00:00:00.0009 heaton:encog-c jheaton$
The command line tool supports several commands. The command is given as the first argument to the command line tool. The following commands are supported.
- benchmark
- csv2egb
- cuda
- egb2csv
- error
- randomize
- train
- xor
The above output is from the Encog for C running on a Mac, however you should see very similar output from Windows or Linux.
| Option | Default | Description |
|---|---|---|
| /c1:## | 2.0 | The PSO learning rate for the particle to converge to its own best. |
| /c2:## | 2.0 | The PSO learning rate for the particle to converge to the overall best particle. |
| /gpu: enable/disable | enable | Determines if the GPU is to be used or not. You must be using a version of Encog compiled for use with CUDA. |
| /ideal:## | 1 | The number of ideals to use in a benchmark or csv2egb command. |
| /input:## | 10 | The number of inputs to use in a benchmark or csv2egb command. |
| /inertia:## | 0.4 | The PSO inertia. |
| /iterations:## | 100 | The number of iterations to use in a benchmark. |
| /konverge:## | 100 | The Nelder Mead konverge value, default:100. |
| /maxpos:## | -1 (no max pos) | PSO: Maximum position, default=-1 (no max pos). |
| /maxvel:## | 2 | The PSO maximum velocity. |
| /particles:## | 30 | The PSO particle count. |
| /records:## | 10000 | The number of records for a benchmark command. |
| /reqmin:## | 1.0e-16f | The Nelder Mead required minimum. |
| /step:## | 10.0 | The Nelder Mead step value, default:10.0. |
| /threads:## | 0 (match CPU core count) | The number of threads, specify 0 to match the core count. |
| /train:type (where type is PSO, NM or RPROP) | PSO | The type of training to use, either PSO, NM or RPROP. |
The following sections will describe how each of the commands work. Some of the commands make use of the option flags previously listed in the table. The following sections will use data files from the Workbench Classification Example. Encog for C does not currently contain built in support for Normalization, so the normalized data generated from the Encog Workbench can be used. This way Encog for C can be used to perform the lengthy training operations. Future versions of Encog for C will likely include normalization support.
The benchmark Command
The benchmark command is used to perform a basic training operation and see how long it takes to execute. The benchmark command can be used to compare the effects of different option settings. The following is an example of performing a benchmark.
heaton:encogtest jheaton$ ./encog-cmd benchmark * * Encog C/C++ 1.0(64 bit, CUDA) Command Line v0.1 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Input Count: 10 Ideal Count: 1 Records: 10000 Iterations: 100 Performing benchmark...please wait Benchmark time(seconds): 4.2392 Benchmark time includes only training time. Encog Finished. Run time 00:00:04.2720 heaton:encogtest jheaton$
The csv2egb Command
Encog training data is usually stored in an EGB File. The data for the EGB file typically comes from a CSV file. The EGB file is binary, and allows Encog to quickly load training data. Additionally, the EGB file stores the number if input and output columns. The csv2egb command allows you to convert a CSV file to EGB. It is assumed that the CSV will have the input columns come first, followed by the ideal output columns. When you perform the conversion, you must specify the input and ideal column counts using the /input and /ideal option specifiers. The following shows an example of using the egb2csv command.
heaton:encogtest jheaton$ ./encog csv2egb iris_norm.csv iris.egb /input:4 /ideal:2 * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Converting CSV to EGB Input Count: 4 Ideal Count: 2 Record Count: 113 Source File: iris_norm.csv Target File: iris.egb Conversion done. Encog Finished. Run time 00:00:00.0007 heaton:encogtest jheaton$
The above command takes the iris_norm.csv file and converts it to the EGB File iris.egb. There are 4 input columns, and 2 ideal output columns.
The cuda Command
The cuda command provides basic stats on your GPU and also tests to see if Encog can access your GPU. The following is an example of the cuda command.
heaton:encogtest jheaton$ ./encog cuda * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Device 0: GeForce GT 650M CUDA Driver Version / Runtime Version 5.0 / 4.2 CUDA Capability Major/Minor version number: 3.0 Total amount of global memory: 1024 MBytes (1073414144 bytes) ( 2) Multiprocessors x (192) CUDA Cores/MP: 384 CUDA Cores GPU Clock Speed: 0.90 GHz Total amount of constant memory: 65536 bytes Total amount of shared memory per block: 49152 bytes Total number of registers available per block: 65536 Warp size: 32 Maximum number of threads per block: 1024 Maximum sizes of each dimension of a block: 1024 x 1024 x 64 Maximum sizes of each dimension of a grid: 2147483647 x 65535 x 65535 Maximum memory pitch: 2147483647 bytes Texture alignment: 512 bytes Performing CUDA test. Vector Addition CUDA Vector Add Test was successful. Encog Finished. Run time 00:00:00.1615 heaton:encogtest jheaton$
If Encog were not able to access the GPU, an error would have been displayed. Also, not all versions of Encog are compiled to include CUDA. Make sure you are using a CUDA binary if you are trying to use CUDA commands.
The egb2csv Command
Encog training data is usually stored in an EGB File. This data can be converted back to a CSV File. The egb2csv command can be used to do this.
heaton:encogtest jheaton$ ./encog egb2csv iris.egb test.csv * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Converting EGB to CSV Input Count: 4 Ideal Count: 2 Record Count: 113 Source File: iris.egb Target File: test.csv Conversion done. Encog Finished. Run time 00:00:00.0011 heaton:encogtest jheaton$
The above command will convert the EGB File iris.egb to the CSV File test.csv.
The error Command
The error command calculates the error between the actual outputs of a neural network and the ideal outputs specified in an EGB File. The following shows an example of using the error command.
heaton:encogtest jheaton$ ./encog error iris_train.eg iris.egb * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Evaluate Error Input Count: 4 Ideal Count: 2 Record Count: 112 SSE Error: 0.99% Encog Finished. Run time 00:00:00.0361 heaton:encogtest jheaton$
The above command used the neural network in EG File iris_train.eg and validated it against iris.egb. The resulting sum of squares error is shown.
The randomize Command
Neural networks can be randomized to new starting weights. This will be a totally random network that will need to be trained. The randomize command can be used to do this.
heaton:encogtest jheaton$ ./encog randomize iris_train.eg * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Network randomized and saved. Encog Finished. Run time 00:00:00.0020 heaton:encogtest jheaton$
The above command was used to randomize the neural network in the EG File iris_train.eg.
The train Command
Before a neural network can be used it must be trained. The train command is used to train a neural network. The command below trains a neural network using RPROP. Additionally, you can use any of the following.
- rprop - Resilient Propagation
- pso - Particle Swarm Optimization
- nm - Nelder Mead
The following command shows a neural network training.
heaton:encogtest jheaton$ ./encog train iris_train.eg iris_train.egb /train:rprop * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Training Input Count: 4 Ideal Count: 2 Record Count: 112 Beginning training (ENCOG_TRAIN_RPROP). Iteration #1, Error: 83.3155%, press [Enter] to quit and save. Iteration #29, Error: 3.0457%, press [Enter] to quit and save. Iteration #76, Error: 2.3124%, press [Enter] to quit and save. Iteration #123, Error: 2.1975%, press [Enter] to quit and save. Iteration #174, Error: 2.0893%, press [Enter] to quit and save. Iteration #220, Error: 2.0181%, press [Enter] to quit and save. Iteration #268, Error: 1.9613%, press [Enter] to quit and save. Iteration #315, Error: 1.8969%, press [Enter] to quit and save. Iteration #362, Error: 1.8496%, press [Enter] to quit and save. Iteration #410, Error: 1.8047%, press [Enter] to quit and save. Iteration #458, Error: 1.7439%, press [Enter] to quit and save. Iteration #505, Error: 1.6237%, press [Enter] to quit and save. Iteration #553, Error: 1.5741%, press [Enter] to quit and save. Iteration #601, Error: 1.5254%, press [Enter] to quit and save. Iteration #648, Error: 1.4925%, press [Enter] to quit and save. Iteration #697, Error: 1.4655%, press [Enter] to quit and save. Iteration #747, Error: 1.4291%, press [Enter] to quit and save. Iteration #797, Error: 1.3943%, press [Enter] to quit and save. Iteration #846, Error: 1.3477%, press [Enter] to quit and save. Iteration #895, Error: 1.2523%, press [Enter] to quit and save. Iteration #944, Error: 1.1863%, press [Enter] to quit and save. Iteration #992, Error: 1.0642%, press [Enter] to quit and save. Iteration #1040, Error: 1.0169%, press [Enter] to quit and save. Iteration #1067, Error: 0.9992%, press [Enter] to quit and save. Training complete. Encog Finished. Run time 00:00:22.1626 heaton:encogtest jheaton$
The xor Command
The xor command shows a simple XOR network trained. The following is an example of this.
heaton:encogtest jheaton$ ./encog xor * * Encog C/C++ (64 bit, CUDA) Command Line v1.0 * * Processor/Core Count: 8 Basic Data Type: double (64 bits) GPU: enabled Beginning training (ENCOG_TRAIN_PSO). Iteration #1, Error: 24.9807%, press [Enter] to quit and save. Iteration #30, Error: 0.7353%, press [Enter] to quit and save. Training complete. Results: [0 0] = 0.0777629 (ideal=0 ) [1 0] = 0.87588387 (ideal=1 ) [0 1] = 0.93529811 (ideal=1 ) [1 1] = 0.06143687 (ideal=0 ) Error: 0.0073 Encog Finished. Run time 00:00:00.0549 heaton:encogtest jheaton$
Encog for C API
char line[MAX_STR];
int i; REAL *input,*ideal; REAL output[1]; float error;
Encog API Initialization
To make use of the Encog API, you must include the encog.h header file. this is done with the following line of code.
#include "encog.h"Before you can issue any commands you must initialize the Encog for C API. This is done with the following line of code.
EncogInit();
Loading Training Data
There are several ways to load data in the Encog for C API. Probably the easiest is simply to load an external EGB file.
ENCOG_DATA *data; data = EncogDataEGBLoad(egbFile);
If there is a small amount of data, you may wish to embed it into the source fine. This works well for examples. The following code does this.
/* Load the data for XOR */ ENCOG_DATA *data; data = EncogDataCreate(2, 1, 4); EncogErrorCheck(); EncogDataAdd(data,"0,0, 0"); EncogDataAdd(data,"1,0, 1"); EncogDataAdd(data,"0,1, 1"); EncogDataAdd(data,"1,1, 0");
</syntaxhighlight>
Create a Neural Network
To create a neural network use the following code.
/* Create a 3 layer neural network, with sigmoid transfer functions and bias */ ENCOG_NEURAL_NETWORK *net; net = EncogNetworkFactory("basic", "2:B->SIGMOID->2:B->SIGMOID->1", 0,0); EncogErrorCheck();
For more information on the Factory Code above, refer to the Feedforward Neural Network article.
Train a Neural Network
Neural network training takes a randomized neural network and teaches the neural network to provide better output for the provided input. Encog for C suppors three different training methods:
- rprop - Resilient Propagation
- pso - Particle Swarm Optimization
- nm - Nelder Mead
For example, the following command would specify Resilient Propagation training.
EncogHashPut(encogContext.config,PARAM_TRAIN,"RPROP");
Next, you must create a trainer. This is done with the following code.
ENCOG_OBJECT *trainer; trainer = EncogTrainNew(net,data); EncogErrorCheck();
The call to EncogErrorCheck checks to see if the last call to the Encog API caused an error. If an error is reported, the error will be displayed, and the program terminated.
ENCOG_TRAINING_REPORT *report; report = EncogTrainReport(trainer); EncogErrorCheck();
We can also specify how the training progress will be reported, and what the maximum error (before stopping) is.
/* Begin training, report progress. */ report->maxError = 0.00f; report->maxIterations = 500; report->updateSeconds = 1; report->maxError = (float)0.01;
Now that all is setup, we can begin training.
EncogTrainRun(trainer,net);
The above call will not return until the training complets.
Determine Error
The following code shows how to calculate the error.
/* Obtain the SSE error, display it */ error = EncogErrorSSE(net, data); *line = 0; EncogStrCatStr(line,"Error: ",MAX_STR); EncogStrCatDouble(line,(double)error,4,MAX_STR); puts(line);
Query Neural Network
The following code shows how to query the neural network.
/* Display results */ /* Display the results from the neural network, see if it learned anything */ printf("\nResults:\n"); for(i=0; i<4; i++) { input = EncogDataGetInput(data,i); ideal = EncogDataGetIdeal(data,i); EncogNetworkCompute(net,input,output); *line = 0; EncogStrCatStr(line,"[",MAX_STR); EncogStrCatDouble(line,input[0],8,MAX_STR); EncogStrCatStr(line," ",MAX_STR); EncogStrCatDouble(line,input[1],8,MAX_STR); EncogStrCatStr(line,"] = ",MAX_STR); EncogStrCatDouble(line,output[0],8,MAX_STR); EncogStrCatStr(line," (ideal=", MAX_STR); EncogStrCatDouble(line,ideal[0],8,MAX_STR); EncogStrCatStr(line," )", MAX_STR); puts(line); }
=Delete Neural Network
It is important to delete unused Encog objects. The following code deletes the neural network that was previously created.
/* Delete the neural network */ EncogObjectFree(net); EncogErrorCheck(); }
Trouble Shooting
dyld: Library not loaded: @rpath/libcudart.dylib Referenced from: ./encog Reason: image not found Trace/BPT trap Add these lines to your .bash_profile: export PATH=/usr/local/cuda/bin:$PATH export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH
encog-core/cuda_eval.cu(252) : CUDA Runtime API error 35: CUDA driver version is insufficient for CUDA runtime version.