TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
ANN.h
Go to the documentation of this file.
1/* -*- Mode: C++; -*- */
2// VER: $Id$
3// copyright (c) 2004 by Christos Dimitrakakis <dimitrak@idiap.ch>
4/***************************************************************************
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 ***************************************************************************/
12
13#ifndef ANN_H
14#define ANN_H
15
16#include <assert.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <math.h>
20#include <learning/learn_debug.h>
21#include <learning/string_utils.h>
22#include <learning/List.h>
23#include <learning/real.h>
24
40
55
66
69typedef struct Layer_ {
82 void (*forward) (struct Layer_* current_layer, bool stochastic);
83 real (*backward) (LISTITEM* p, real* d, bool use_eligibility, real TD);
84 real (*f) (real x);
85 real (*f_d) (real x);
87
104
105
106/**************** User-level API ******************/
107/* Object Management Interface */
108extern ANN* NewANN(int n_inputs, int n_outputs);
109extern int DeleteANN(ANN* ann);
110extern ANN* LoadANN(char* filename);
111extern ANN* LoadANN(FILE* f);
112extern int SaveANN(ANN* ann, char* filename);
113extern int SaveANN(ANN* ann, FILE* f);
114
115/* Setup Inteface */
116extern int ANN_AddHiddenLayer(ANN* ann, int n_nodes);
117extern int ANN_AddRBFHiddenLayer (ANN* ann, int n_nodes);
118extern int ANN_Init(ANN* ann);
119extern void ANN_SetOutputsToTanH(ANN* ann);
120extern void ANN_SetOutputsToLinear(ANN* ann);
121extern void ANN_SetLearningRate(ANN* ann, real a);
122extern void ANN_SetLambda(ANN * ann, real lambda);
123extern void ANN_SetZeta(ANN * ann, real lambda);
124extern void ANN_Reset(ANN* ann);
125
126/* Functionality Interface */
127extern real ANN_Input(ANN* ann, real* x);
128extern real ANN_StochasticInput(ANN * ann, real * x);
129extern real ANN_Train(ANN* ann, real* x, real* t);
130extern real ANN_Delta_Train(ANN * ann, real* delta, real TD = 0.0);
131extern void ANN_SetBatchMode(ANN* ann, bool batch);
132extern void ANN_BatchAdapt(ANN* ann);
133extern real ANN_Test(ANN* ann, real* x, real* t);
134extern real* ANN_GetOutput(ANN* ann);
135extern real ANN_GetError(ANN* ann);
136extern real* ANN_GetErrorVector(ANN* ann);
137
138/********* Low-level code **********/
139
140/* Sub-object management functions */
141extern Layer* ANN_AddLayer (ANN* ann, int n_inputs, int n_outputs, real* x);
142extern Layer* ANN_AddRBFLayer (ANN* ann, int n_inputs, int n_outputs, real* x);
143extern void ANN_FreeLayer (void* l);
144extern void ANN_FreeLayer (Layer* l);
145
146/* Calculations */
147extern void ANN_CalculateLayerOutputs (Layer* current_layer, bool stochastic=false);
148extern real ANN_Backpropagate (LISTITEM* p, real* d, bool use_eligibility=false, real TD = 0.0);
149extern void ANN_RBFCalculateLayerOutputs (Layer* current_layer, bool stochastic=false);
150extern real ANN_RBFBackpropagate (LISTITEM* p, real* d, bool use_eligibility=false, real TD = 0.0);
151extern void ANN_LayerBatchAdapt (Layer* l);
152
153/* Output functions and derivatives */
154extern real Exp (real x);
155extern real Exp_d (real x);
156extern real htan (real x);
157extern real htan_d (real x);
158extern real dtan (real x);
159extern real dtan_d (real x);
160extern real linear (real x);
161extern real linear_d (real x);
162
163/* Debugging functions */
165extern real ANN_ShowWeights(ANN* ann);
166extern void ANN_ShowOutputs(ANN* ann);
167extern real ANN_ShowInputs(ANN* ann);
169#endif /* ANN_H */
int ANN_AddHiddenLayer(ANN *ann, int n_nodes)
Add a hidden layer with n_nodes.
Definition ANN.cpp:111
real ANN_LayerShowInputs(Layer *l)
Dump inputs to a particular layer on stdout.
Definition ANN.cpp:996
void ANN_FreeLayer(void *l)
Free this layer - low level.
Definition ANN.cpp:313
void ANN_SetLearningRate(ANN *ann, real a)
Set the learning rate to a.
Definition ANN.cpp:856
struct Layer_ Layer
A collection of connections from one layer to another, plus management functions and data.
real htan(real x)
Hyperbolic tangent hook.
Definition ANN.cpp:1086
real linear_d(real x)
linear derivative hook
Definition ANN.cpp:1142
void ANN_LayerBatchAdapt(Layer *l)
Perform batch adaptation.
Definition ANN.cpp:773
ANN * NewANN(int n_inputs, int n_outputs)
Create a new ANN.
Definition ANN.cpp:25
void ANN_SetOutputsToTanH(ANN *ann)
Set outputs to hyperbolic tangent.
Definition ANN.cpp:1050
real ANN_Input(ANN *ann, real *x)
Give an input vector to the neural network.
Definition ANN.cpp:406
int ANN_Init(ANN *ann)
Initialise neural network.
Definition ANN.cpp:346
real ANN_ShowInputs(ANN *ann)
Dump inputs to all layers on stdout.
Definition ANN.cpp:979
real Exp(real x)
Exponential hook.
Definition ANN.cpp:1068
real ANN_Backpropagate(LISTITEM *p, real *d, bool use_eligibility=false, real TD=0.0)
d are the derivatives at the outputs.
Definition ANN.cpp:610
real dtan(real x)
Discrete htan hook.
Definition ANN.cpp:1105
int ANN_AddRBFHiddenLayer(ANN *ann, int n_nodes)
Add an RBF layer with n_nodes.
Definition ANN.cpp:131
real ANN_RBFBackpropagate(LISTITEM *p, real *d, bool use_eligibility=false, real TD=0.0)
Backpropagation for an RBF layer.
Definition ANN.cpp:727
void ANN_SetLambda(ANN *ann, real lambda)
Set lambda, eligibility decay.
Definition ANN.cpp:872
real Exp_d(real x)
Exponential derivative hook.
Definition ANN.cpp:1077
Layer * ANN_AddRBFLayer(ANN *ann, int n_inputs, int n_outputs, real *x)
Low-level code to add an RBF layer.
Definition ANN.cpp:240
void ANN_ShowOutputs(ANN *ann)
Dump outputs to stdout.
Definition ANN.cpp:1017
real linear(real x)
linear hook
Definition ANN.cpp:1133
void ANN_SetZeta(ANN *ann, real lambda)
Set zeta, parameter variance smoothing.
Definition ANN.cpp:890
real htan_d(real x)
Hyperbolic tangent derivative hook.
Definition ANN.cpp:1095
struct Connection_ Connection
A linear connection between two neural elements.
int SaveANN(ANN *ann, char *filename)
Save the ANN to a filename.
Definition ANN.cpp:1182
void ANN_SetBatchMode(ANN *ann, bool batch)
Set batch updates.
Definition ANN.cpp:906
void ANN_CalculateLayerOutputs(Layer *current_layer, bool stochastic=false)
Calculate layer outputs.
Definition ANN.cpp:449
real dtan_d(real x)
Discrete htan derivative hook.
Definition ANN.cpp:1119
void ANN_Reset(ANN *ann)
Resets the eligbility traces and batch updates.
Definition ANN.cpp:379
Layer * ANN_AddLayer(ANN *ann, int n_inputs, int n_outputs, real *x)
Low-level code to add a weighted sum layer.
Definition ANN.cpp:152
void ANN_BatchAdapt(ANN *ann)
Adapt the parameters after a series of patterns has been seen.
Definition ANN.cpp:923
void ANN_SetOutputsToLinear(ANN *ann)
Set outputs to linear.
Definition ANN.cpp:1033
struct ANN_ ANN
ANN management structure.
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 ANN.cpp:802
int DeleteANN(ANN *ann)
Delete a neural network.
Definition ANN.cpp:77
real ANN_LayerShowWeights(Layer *l)
Dump the weights of a particular layer on stdout.
Definition ANN.cpp:958
real ANN_Delta_Train(ANN *ann, real *delta, real TD=0.0)
Minimise a custom cost function.
Definition ANN.cpp:584
real ANN_StochasticInput(ANN *ann, real *x)
Stochastically generate an output, depending on parameter distributions.
Definition ANN.cpp:429
real ANN_Train(ANN *ann, real *x, real *t)
Perform mean square error training, where the aim is to minimise the cost function ,...
Definition ANN.cpp:544
ANN * LoadANN(char *filename)
Load an ANN from a filename.
Definition ANN.cpp:1171
void ANN_RBFCalculateLayerOutputs(Layer *current_layer, bool stochastic=false)
Calculate layer outputs.
Definition ANN.cpp:503
real ANN_GetError(ANN *ann)
Get the error for the current input/output pair.
Definition ANN.cpp:833
struct RBFConnection_ RBFConnection
An RBF connection between two neural elements.
real * ANN_GetErrorVector(ANN *ann)
Return the error vector for pattern.
Definition ANN.cpp:847
real ANN_ShowWeights(ANN *ann)
Dump the weights on stdout.
Definition ANN.cpp:941
real * ANN_GetOutput(ANN *ann)
Get the output for the current input.
Definition ANN.cpp:824
static Point p[4]
Definition Convex.cpp:54
struct ListItem LISTITEM
A list item.
struct List LIST
A very simple list structure.
float real
Definition real.h:13
ANN management structure.
Definition ANN.h:89
bool eligibility_traces
use eligibility
Definition ANN.h:102
real * y
unit activations
Definition ANN.h:94
bool batch_mode
use batch mode
Definition ANN.h:101
real lambda
eligibility trace decay
Definition ANN.h:98
real zeta
variance update smoothness
Definition ANN.h:99
real * t
targets
Definition ANN.h:95
real * x
unit inputs
Definition ANN.h:93
int n_inputs
number of inputs
Definition ANN.h:90
LIST * c
connection layers
Definition ANN.h:92
real * error
errors
Definition ANN.h:100
int n_outputs
number of outputs
Definition ANN.h:91
real * d
delta vector
Definition ANN.h:96
real a
learning rate
Definition ANN.h:97
A linear connection between two neural elements.
Definition ANN.h:48
real e
eligibility;
Definition ANN.h:52
real dw
Weight-change.
Definition ANN.h:51
int c
connected?
Definition ANN.h:49
real w
weight
Definition ANN.h:50
real v
variance estimate
Definition ANN.h:53
A collection of connections from one layer to another, plus management functions and data.
Definition ANN.h:69
real(* f_d)(real x)
derivative of activation function
Definition ANN.h:85
int n_inputs
number of inputs
Definition ANN.h:70
real lambda
eligibility decay
Definition ANN.h:79
real * z
activation
Definition ANN.h:74
void(* forward)(struct Layer_ *current_layer, bool stochastic)
forward calculation
Definition ANN.h:82
bool batch_mode
do not update weights immediately
Definition ANN.h:81
real * y
outputs
Definition ANN.h:73
real * x
inputs;
Definition ANN.h:72
real zeta
variance update smoothness.
Definition ANN.h:80
int n_outputs
number of outputs
Definition ANN.h:71
real * d
derivatives
Definition ANN.h:75
Connection * c
connections
Definition ANN.h:76
RBFConnection * rbf
rbf connections (if any)
Definition ANN.h:77
real a
learning rate
Definition ANN.h:78
real(* f)(real x)
activation function
Definition ANN.h:84
real(* backward)(LISTITEM *p, real *d, bool use_eligibility, real TD)
partial derivative calculation
Definition ANN.h:83
An RBF connection between two neural elements.
Definition ANN.h:62
real m
mean
Definition ANN.h:64
real w
weight (= )
Definition ANN.h:63