Main ffnet class¶
- class ffnet.ffnet(conec, lazy_derivative=True)[source]¶
Feed-forward neural network main class.
Parameters : - conec : list of tuples
List of network connections
- lazy_derivative : bool
If True all data necessary for derivatives calculation (see ffnet.derivative method) are generated only on demand.
Returns : - net
Feed forward network object
Raises : - TypeError
If conec is not directed acyclic graph
Instance attributes: - conec : array
Topologically sorted network connections
- weights : array
Weights in order of topologically sorted connections
- renormalize : bool
If True normalization ranges will be recreated from training data at next training call.
Default is True
Examples : >>> from ffnet import mlgraph, ffnet >>> conec = mlgraph((2,2,1)) >>> net = ffnet(conec)
See also: - call(inp)[source]¶
Calculates network answer to given input.
Parameters : - inp : array
2D array of input patterns (or 1D for single pattern)
Returns : - ans : array
1D or 2D array of calculated network outputs
Raises : - TypeError
If inp is invalid
- derivative(inp)[source]¶
Returns partial derivatives of the network’s output vs its input.
For each input pattern an array of the form:
| o1/i1, o1/i2, ..., o1/in | | o2/i1, o2/i2, ..., o2/in | | ... | | om/i1, om/i2, ..., om/in |
is returned.
Parameters : - inp : array
2D array of input patterns (or 1D for single pattern)
Returns : - ans : array
1D or 2D array of calculated network outputs
Examples : >>> from ffnet import mlgraph, ffnet >>> conec = mlgraph((3,3,2)) >>> net = ffnet(conec); net.weights[:] = 1. >>> net.derivative([0., 0., 0.]) array([[ 0.02233658, 0.02233658, 0.02233658], [ 0.02233658, 0.02233658, 0.02233658]])
- randomweights()[source]¶
Randomize network weights due to Bottou proposition.
If n is a number of incoming connections to the node, weights of these connections are chosen randomly from range (-2.38/sqrt(n), 2.38/sqrt(n))
- sqerror(input, target)[source]¶
Calculates sum of squared errors at network output.
Error is calculated for normalized input and target arrays.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
Returns : - err : float
0.5*(sum of squared errors at network outputs)
Note
This function might be slow in frequent use, because data normalization is performed at each call. Usually there’s no need to use this function, unless you need to adopt your own training strategy.
- sqgrad(input, target)[source]¶
Returns gradient of network error vs. network weights.
Error is calculated for normalized input and target arrays.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
Returns : - grad : 1-D array
Array of the same length as net.weights containing gradient values.
Note
This function might be slow in frequent use, because data normalization is performed at each call. Usually there’s no need to use this function, unless you need to adopt your own training strategy.
- test(input, target, iprint=1, filename=None)[source]¶
Calculates output and parameters of regression.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- iprint : {0, 1, 2}, optional
Verbosity level: 0 – print nothing, 1 – print regression parameters for each output node (default), 2 – print additionaly general network info and all targets vs. outputs
- filename : str
Path to the file where printed messages are redirected Default is None
Returns : - out : tuple
(output, regress) tuple where: output is an array of network answers on input patterns and regress contains regression parameters for each output node. These parameters are: slope, intercept, r-value, p-value, stderr-of-slope, stderr-of-estimate.
Examples : >>> from ffnet import mlgraph, ffnet >>> from numpy.random import rand >>> conec = mlgraph((3,3,2)) >>> net = ffnet(conec) >>> input = rand(50,3); target = rand(50,2) >>> output, regress = net.test(input, target) Testing results for 50 testing cases: OUTPUT 1 (node nr 8): Regression line parameters: slope = -0.000649 intercept = 0.741282 r-value = -0.021853 p-value = 0.880267 slope stderr = 0.004287 estim. stderr = 0.009146 . OUTPUT 2 (node nr 7): Regression line parameters: slope = 0.005536 intercept = 0.198818 r-value = 0.285037 p-value = 0.044816 slope stderr = 0.002687 estim. stderr = 0.005853
Exemplary plot:
from ffnet import mlgraph, ffnet from numpy.random import rand from numpy import linspace import pylab # Create and train net on random data conec = mlgraph((3,10,2)) net = ffnet(conec) input = rand(50,3); target = rand(50,2) net.train_tnc(input, target, maxfun = 400) output, regress = net.test(input, target, iprint = 0) # Plot results for first output pylab.plot(target.T[0], output.T[0], 'o', label='targets vs. outputs') slope = regress[0][0]; intercept = regress[0][1] x = linspace(0,1) y = slope * x + intercept pylab.plot(x, y, linewidth = 2, label = 'regression line') pylab.legend() pylab.show()
- train_bfgs(input, target, **kwargs)[source]¶
Train network with constrained version of BFGS algorithm.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- maxfun : int
Maximum number of function evaluations (default is 15000)
- bounds : list, optional
(min, max) pairs for each connection weight, defining the bounds on that weight. Use None for one of min or max when there is no bound in that direction. By default all bounds ar set to (-100, 100)
- disp : int, optional
If 0, then no output (default). If positive number then convergence messages are dispalyed.
See also
scipy.optimize.fmin_l_bfgs_b optimizer is used in this method. Look at its documentation for possible other useful parameters.
- train_cg(input, target, **kwargs)[source]¶
Train network with conjugate gradient algorithm.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- maxiter : integer, optional
Maximum number of iterations (default is 10000)
- disp : bool
If True convergence method is displayed (default)
See also
scipy.optimize.fmin_cg optimizer is used in this method. Look at its documentation for possible other useful parameters.
- train_genetic(input, target, **kwargs)[source]¶
Global weights optimization with genetic algorithm.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- lower : float, optional
Lower bound of weights values (default is -25.)
- upper : float, optional
Upper bound of weights values (default is 25.)
- individuals : integer, optional
Number of individuals in a population (default is 20)
- generations : integer, optional
Number of generations over which solution is to evolve (default is 500)
- verbosity : {0, 1, 2}, optional
Printed output 0/1/2=None/Minimal/Verbose (default is 0)
See also
See description of pikaia.pikaia optimization function for other parameters.
- train_momentum(input, target, eta=0.2, momentum=0.8, maxiter=10000, disp=0)[source]¶
Simple backpropagation training with momentum.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- eta : float, optional
Learning rate
- momentum : float, optional
Momentum coefficient
- maxiter : integer, optional
Maximum number of iterations
- disp : bool
If True convergence method is displayed
- train_rprop(input, target, a=1.2, b=0.5, mimin=1e-06, mimax=50.0, xmi=0.1, maxiter=10000, disp=0)[source]¶
Rprop training algorithm.
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- a : float, optional
Training step increasing parameter
- b : float, optional
Training step decreasing parameter
- mimin : float, optional
Minimum training step
- mimax : float, optional
Maximum training step
- xmi : array (or float), optional
Array containing initial training steps for weights. If xmi is a scalar then its value is set for all weights
- maxiter : integer, optional
Maximum number of iterations
- disp : bool
If True convergence method is displayed. Default is False
Returns : - xmi : array
Computed array of training steps to be used in eventual further training calls.
- train_tnc(input, target, nproc=1, **kwargs)[source]¶
Parameters : - input : 2-D array
Array of input patterns
- target : 2-D array
Array of network targets
- nproc : int or ‘ncpu’, optional
Number of processes spawned for training. If nproc=’ncpu’ nproc will be set to number of avilable processors
- maxfun : int
Maximum number of function evaluation. If None, maxfun is set to max(100, 10*len(weights)). Defaults to None.
- bounds : list, optional
(min, max) pairs for each connection weight, defining the bounds on that weight. Use None for one of min or max when there is no bound in that direction. By default all bounds ar set to (-100, 100)
- messages : int, optional
If 0, then no output (default). If positive number then convergence messages are dispalyed.
Note
On Windows using ncpu > 1 might be memory hungry, because each process have to load its own instance of network and training data. This is not the case on Linux platforms.
See also
scipy.optimize.fmin_tnc optimizer is used in this method. Look at its documentation for possible other useful parameters.
