TORCS  1.3.9
The Open Racing Car Simulator
ANN.cpp File Reference
#include <cstring>
#include <learning/ANN.h>
#include <learning/string_utils.h>
#include <learning/Distribution.h>
Include dependency graph for ANN.cpp:

Go to the source code of this file.

Functions

ANNNewANN (int n_inputs, int n_outputs)
 Create a new ANN. More...
 
int DeleteANN (ANN *ann)
 Delete a neural network. More...
 
int ANN_AddHiddenLayer (ANN *ann, int n_nodes)
 Add a hidden layer with n_nodes. More...
 
int ANN_AddRBFHiddenLayer (ANN *ann, int n_nodes)
 Add an RBF layer with n_nodes. More...
 
LayerANN_AddLayer (ANN *ann, int n_inputs, int n_outputs, real *x)
 Low-level code to add a weighted sum layer. More...
 
LayerANN_AddRBFLayer (ANN *ann, int n_inputs, int n_outputs, real *x)
 Low-level code to add an RBF layer. More...
 
void ANN_FreeLayer (void *l)
 Free this layer - low level. More...
 
void ANN_FreeLayer (Layer *l)
 Free this layer - low level. More...
 
int ANN_Init (ANN *ann)
 Initialise neural network. More...
 
void ANN_Reset (ANN *ann)
 Resets the eligbility traces and batch updates. More...
 
real ANN_Input (ANN *ann, real *x)
 Give an input vector to the neural network. More...
 
real ANN_StochasticInput (ANN *ann, real *x)
 Stochastically generate an output, depending on parameter distributions. More...
 
void ANN_CalculateLayerOutputs (Layer *current_layer, bool stochastic)
 Calculate layer outputs. More...
 
void ANN_RBFCalculateLayerOutputs (Layer *current_layer, bool stochastic)
 Calculate layer outputs. More...
 
real ANN_Train (ANN *ann, real *x, real *t)
 Perform mean square error training, where the aim is to minimise the cost function \(\sum_i |f(x_i)-t_i|^2\), where \(x_i\) is input data, \(f(\cdot)\) is the mapping performed by the neural network, \(t_i\) is the desired output and \(i\) denotes the example index. More...
 
real ANN_Delta_Train (ANN *ann, real *delta, real TD)
 Minimise a custom cost function. More...
 
real ANN_Backpropagate (LISTITEM *p, real *d, bool use_eligibility, real TD)
 d are the derivatives at the outputs. More...
 
real ANN_RBFBackpropagate (LISTITEM *p, real *d, bool use_eligibility, real TD)
 Backpropagation for an RBF layer. More...
 
void ANN_LayerBatchAdapt (Layer *l)
 Perform batch adaptation. More...
 
real ANN_Test (ANN *ann, real *x, real *t)
 Given an input and test pattern, return the MSE between the network's output and the test pattern. More...
 
realANN_GetOutput (ANN *ann)
 Get the output for the current input. More...
 
real ANN_GetError (ANN *ann)
 Get the error for the current input/output pair. More...
 
realANN_GetErrorVector (ANN *ann)
 Return the error vector for pattern. More...
 
void ANN_SetLearningRate (ANN *ann, real a)
 Set the learning rate to a. More...
 
void ANN_SetLambda (ANN *ann, real lambda)
 Set lambda, eligibility decay. More...
 
void ANN_SetZeta (ANN *ann, real zeta)
 Set zeta, parameter variance smoothing. More...
 
void ANN_SetBatchMode (ANN *ann, bool batch)
 Set batch updates. More...
 
void ANN_BatchAdapt (ANN *ann)
 Adapt the parameters after a series of patterns has been seen. More...
 
real ANN_ShowWeights (ANN *ann)
 Dump the weights on stdout. More...
 
real ANN_LayerShowWeights (Layer *l)
 Dump the weights of a particular layer on stdout. More...
 
real ANN_ShowInputs (ANN *ann)
 Dump inputs to all layers on stdout. More...
 
real ANN_LayerShowInputs (Layer *l)
 Dump inputs to a particular layer on stdout. More...
 
void ANN_ShowOutputs (ANN *ann)
 Dump outputs to stdout. More...
 
void ANN_SetOutputsToLinear (ANN *ann)
 Set outputs to linear. More...
 
void ANN_SetOutputsToTanH (ANN *ann)
 Set outputs to hyperbolic tangent. More...
 
real Exp (real x)
 Exponential hook. More...
 
real Exp_d (real x)
 Exponential derivative hook. More...
 
real htan (real x)
 Hyperbolic tangent hook. More...
 
real htan_d (real x)
 Hyperbolic tangent derivative hook. More...
 
real dtan (real x)
 Discrete htan hook. More...
 
real dtan_d (real x)
 Discrete htan derivative hook. More...
 
real linear (real x)
 linear hook More...
 
real linear_d (real x)
 linear derivative hook More...
 
static bool CheckMatchingToken (const char *tag, StringBuffer *buf, FILE *f)
 Check that tags match. More...
 
static void WriteToken (const char *tag, FILE *f)
 Write a token. More...
 
ANNLoadANN (char *filename)
 Load an ANN from a filename. More...
 
int SaveANN (ANN *ann, char *filename)
 Save the ANN to a filename. More...
 
ANNLoadANN (FILE *f)
 Load the ANN from a C file handle. More...
 
int SaveANN (ANN *ann, FILE *f)
 Save the ANN to a C file handle. More...
 

Function Documentation

◆ ANN_AddHiddenLayer()

int ANN_AddHiddenLayer ( ANN ann,
int  n_nodes 
)

Add a hidden layer with n_nodes.

Definition at line 111 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_AddLayer()

Layer* ANN_AddLayer ( ANN ann,
int  n_inputs,
int  n_outputs,
real x 
)

Low-level code to add a weighted sum layer.

Definition at line 152 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_AddRBFHiddenLayer()

int ANN_AddRBFHiddenLayer ( ANN ann,
int  n_nodes 
)

Add an RBF layer with n_nodes.

Definition at line 131 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_AddRBFLayer()

Layer* ANN_AddRBFLayer ( ANN ann,
int  n_inputs,
int  n_outputs,
real x 
)

Low-level code to add an RBF layer.

Definition at line 240 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_Backpropagate()

real ANN_Backpropagate ( LISTITEM p,
real d,
bool  use_eligibility,
real  TD 
)

d are the derivatives at the outputs.

Definition at line 610 of file ANN.cpp.

◆ ANN_BatchAdapt()

void ANN_BatchAdapt ( ANN ann)

Adapt the parameters after a series of patterns has been seen.

Definition at line 923 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_CalculateLayerOutputs()

void ANN_CalculateLayerOutputs ( Layer current_layer,
bool  stochastic 
)

Calculate layer outputs.

Definition at line 449 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_Delta_Train()

real ANN_Delta_Train ( ANN ann,
real delta,
real  TD 
)

Minimise a custom cost function.

The argument delta is a the derivative of the cost function with respect to the neural network outputs. In this case you must call ANN_Input() yourself. As an example, to use MSE cost with ANN_Delta_Train(), you should call ANN_Input() with your data, call ANN_GetOutput() to take a look at the outputs and then call ANN_DeltaTrain() with a real vector delta equal to the target vector minus the ANN output vector. The argument TD is useful if you want to separate the calculation of the derivative of the output with respect to the parameters from the derivative the cost function with respect to the output. This is done in reinforcement learning with eligibility traces for example, where the cost function depends upon previous outputs.

Definition at line 584 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_FreeLayer() [1/2]

void ANN_FreeLayer ( void *  l)

Free this layer - low level.

Definition at line 313 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_FreeLayer() [2/2]

void ANN_FreeLayer ( Layer l)

Free this layer - low level.

Definition at line 322 of file ANN.cpp.

◆ ANN_GetError()

real ANN_GetError ( ANN ann)

Get the error for the current input/output pair.

Definition at line 833 of file ANN.cpp.

◆ ANN_GetErrorVector()

real* ANN_GetErrorVector ( ANN ann)

Return the error vector for pattern.

Definition at line 847 of file ANN.cpp.

◆ ANN_GetOutput()

real* ANN_GetOutput ( ANN ann)

Get the output for the current input.

Definition at line 824 of file ANN.cpp.

◆ ANN_Init()

int ANN_Init ( ANN ann)

Initialise neural network.

Call this function after you have added all the layers. It adds an extra output layer.

Definition at line 346 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_Input()

real ANN_Input ( ANN ann,
real x 
)

Give an input vector to the neural network.

Calculate a new output given the input. If the vector length is incorrect, you will have trouble.

Definition at line 406 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_LayerBatchAdapt()

void ANN_LayerBatchAdapt ( Layer l)

Perform batch adaptation.

Definition at line 773 of file ANN.cpp.

◆ ANN_LayerShowInputs()

real ANN_LayerShowInputs ( Layer l)

Dump inputs to a particular layer on stdout.

Definition at line 996 of file ANN.cpp.

◆ ANN_LayerShowWeights()

real ANN_LayerShowWeights ( Layer l)

Dump the weights of a particular layer on stdout.

Definition at line 958 of file ANN.cpp.

◆ ANN_RBFBackpropagate()

real ANN_RBFBackpropagate ( LISTITEM p,
real d,
bool  use_eligibility,
real  TD 
)

Backpropagation for an RBF layer.

Definition at line 727 of file ANN.cpp.

◆ ANN_RBFCalculateLayerOutputs()

void ANN_RBFCalculateLayerOutputs ( Layer current_layer,
bool  stochastic 
)

Calculate layer outputs.

Definition at line 503 of file ANN.cpp.

◆ ANN_Reset()

void ANN_Reset ( ANN ann)

Resets the eligbility traces and batch updates.

Definition at line 379 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_SetBatchMode()

void ANN_SetBatchMode ( ANN ann,
bool  batch 
)

Set batch updates.

Definition at line 906 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_SetLambda()

void ANN_SetLambda ( ANN ann,
real  lambda 
)

Set lambda, eligibility decay.

Definition at line 872 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_SetLearningRate()

void ANN_SetLearningRate ( ANN ann,
real  a 
)

Set the learning rate to a.

Definition at line 856 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_SetOutputsToLinear()

void ANN_SetOutputsToLinear ( ANN ann)

Set outputs to linear.

Definition at line 1033 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_SetOutputsToTanH()

void ANN_SetOutputsToTanH ( ANN ann)

Set outputs to hyperbolic tangent.

Definition at line 1050 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_SetZeta()

void ANN_SetZeta ( ANN ann,
real  zeta 
)

Set zeta, parameter variance smoothing.

Useful for ANN_StochasticInput()

Definition at line 890 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_ShowInputs()

real ANN_ShowInputs ( ANN ann)

Dump inputs to all layers on stdout.

Definition at line 979 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_ShowOutputs()

void ANN_ShowOutputs ( ANN ann)

Dump outputs to stdout.

Definition at line 1017 of file ANN.cpp.

◆ ANN_ShowWeights()

real ANN_ShowWeights ( ANN ann)

Dump the weights on stdout.

Definition at line 941 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_StochasticInput()

real ANN_StochasticInput ( ANN ann,
real x 
)

Stochastically generate an output, depending on parameter distributions.

This is an option for people that understand what they are doing.

Definition at line 429 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_Test()

real ANN_Test ( ANN ann,
real x,
real t 
)

Given an input and test pattern, return the MSE between the network's output and the test pattern.

Definition at line 802 of file ANN.cpp.

Here is the call graph for this function:

◆ ANN_Train()

real ANN_Train ( ANN ann,
real x,
real t 
)

Perform mean square error training, where the aim is to minimise the cost function \(\sum_i |f(x_i)-t_i|^2\), where \(x_i\) is input data, \(f(\cdot)\) is the mapping performed by the neural network, \(t_i\) is the desired output and \(i\) denotes the example index.

Under mild assumptions, this is equivalent to minimising \(E\{|f(X)-T|^2\}\), the expected value of the squared error.

Definition at line 544 of file ANN.cpp.

Here is the call graph for this function:

◆ CheckMatchingToken()

static bool CheckMatchingToken ( const char *  tag,
StringBuffer buf,
FILE *  f 
)
inlinestatic

Check that tags match.

Definition at line 1148 of file ANN.cpp.

Here is the call graph for this function:

◆ DeleteANN()

int DeleteANN ( ANN ann)

Delete a neural network.

Definition at line 77 of file ANN.cpp.

Here is the call graph for this function:

◆ dtan()

real dtan ( real  x)

Discrete htan hook.

Definition at line 1105 of file ANN.cpp.

◆ dtan_d()

real dtan_d ( real  x)

Discrete htan derivative hook.

Definition at line 1119 of file ANN.cpp.

◆ Exp()

real Exp ( real  x)

Exponential hook.

Definition at line 1068 of file ANN.cpp.

◆ Exp_d()

real Exp_d ( real  x)

Exponential derivative hook.

Definition at line 1077 of file ANN.cpp.

◆ htan()

real htan ( real  x)

Hyperbolic tangent hook.

Definition at line 1086 of file ANN.cpp.

◆ htan_d()

real htan_d ( real  x)

Hyperbolic tangent derivative hook.

Definition at line 1095 of file ANN.cpp.

◆ linear()

real linear ( real  x)

linear hook

Definition at line 1133 of file ANN.cpp.

◆ linear_d()

real linear_d ( real  x)

linear derivative hook

Definition at line 1142 of file ANN.cpp.

◆ LoadANN() [1/2]

ANN* LoadANN ( char *  filename)

Load an ANN from a filename.

Definition at line 1171 of file ANN.cpp.

Here is the call graph for this function:

◆ LoadANN() [2/2]

ANN* LoadANN ( FILE *  f)

Load the ANN from a C file handle.

Definition at line 1194 of file ANN.cpp.

Here is the call graph for this function:

◆ NewANN()

ANN* NewANN ( int  n_inputs,
int  n_outputs 
)

Create a new ANN.

Definition at line 25 of file ANN.cpp.

Here is the call graph for this function:

◆ SaveANN() [1/2]

int SaveANN ( ANN ann,
char *  filename 
)

Save the ANN to a filename.

Definition at line 1182 of file ANN.cpp.

Here is the call graph for this function:

◆ SaveANN() [2/2]

int SaveANN ( ANN ann,
FILE *  f 
)

Save the ANN to a C file handle.

Definition at line 1249 of file ANN.cpp.

Here is the call graph for this function:

◆ WriteToken()

static void WriteToken ( const char *  tag,
FILE *  f 
)
inlinestatic

Write a token.

Definition at line 1165 of file ANN.cpp.