4
4
using System . Linq ;
5
5
using System . Text ;
6
6
using System . Threading . Tasks ;
7
-
8
- namespace GeNeural . Trainers
7
+ namespace GeNeural . Training . Topology
9
8
{
10
- public class TopologyTrainer
9
+ public sealed class TopologyTrainer
11
10
{
12
- public static NeuralNetwork ConfigureTopology ( double [ ] [ ] trainingInputs , double [ ] [ ] trainingDesiredOutputs , double [ ] [ ] testInputs , double [ ] [ ] testDesiredOutputs )
11
+ public static NeuralNetwork ConfigureTopology ( ISupervisedTrainer < NeuralNetwork > trainer , double [ ] [ ] trainingInputs , double [ ] [ ] trainingDesiredOutputs , double [ ] [ ] testInputs , double [ ] [ ] testDesiredOutputs )
13
12
{
14
13
NeuralNetwork untrainedStubNetwork = new NeuralNetwork ( trainingInputs [ 0 ] . Length , new int [ ] { trainingInputs [ 0 ] . Length , trainingDesiredOutputs [ 0 ] . Length } ) ;
15
14
NeuralNetwork trainedStubNetwork = untrainedStubNetwork . DeepClone ( ) ;
16
- Train ( trainedStubNetwork , trainingInputs , trainingDesiredOutputs ) ;
15
+ Train ( trainer , trainedStubNetwork , trainingInputs , trainingDesiredOutputs ) ;
17
16
double stubNetworkError = GetTotalError ( trainedStubNetwork , trainingInputs , trainingDesiredOutputs ) + GetTotalError ( trainedStubNetwork , testInputs , testDesiredOutputs ) ;
18
17
while ( true )
19
18
{
20
19
NeuralNetwork untrainedLayerNetwork = untrainedStubNetwork . DeepClone ( ) ;
21
20
untrainedLayerNetwork . InsertAfterLayer ( untrainedLayerNetwork . LayerCount - 1 ) ;
22
21
NeuralNetwork trainedLayerNetwork = untrainedStubNetwork . DeepClone ( ) ;
23
- Train ( trainedLayerNetwork , trainingInputs , trainingDesiredOutputs ) ;
22
+ Train ( trainer , trainedLayerNetwork , trainingInputs , trainingDesiredOutputs ) ;
24
23
double layerNetworkError = GetTotalError ( trainedLayerNetwork , trainingInputs , trainingDesiredOutputs ) + GetTotalError ( trainedLayerNetwork , testInputs , testDesiredOutputs ) ;
25
24
while ( true )
26
25
{
27
26
NeuralNetwork untrainedNeuronNetwork = untrainedLayerNetwork . DeepClone ( ) ;
28
27
untrainedNeuronNetwork . AddNeuronNonDestructive ( untrainedNeuronNetwork . LayerCount - 2 ) ;
29
28
NeuralNetwork trainedNeuronNetwork = untrainedNeuronNetwork . DeepClone ( ) ;
30
- Train ( trainedNeuronNetwork , trainingInputs , trainingDesiredOutputs ) ;
29
+ Train ( trainer , trainedNeuronNetwork , trainingInputs , trainingDesiredOutputs ) ;
31
30
double neuronNetworkError = GetTotalError ( trainedNeuronNetwork , trainingInputs , trainingDesiredOutputs ) ;
32
31
if ( neuronNetworkError < layerNetworkError )
33
32
{
@@ -69,7 +68,7 @@ public static double GetError(double desiredOutput, double actualOutput)
69
68
double difference = desiredOutput - actualOutput ;
70
69
return difference * difference ;
71
70
}
72
- public static void Train ( NeuralNetwork network , double [ ] [ ] inputs , double [ ] [ ] desiredOutputs )
71
+ public static void Train < T > ( ISupervisedTrainer < T > trainer , T network , double [ ] [ ] inputs , double [ ] [ ] desiredOutputs ) where T : NeuralNetwork
73
72
{
74
73
75
74
for ( int _ = 0 ; _ < 100 ; _ ++ )
@@ -78,7 +77,7 @@ public static void Train(NeuralNetwork network, double[][] inputs, double[][] de
78
77
{
79
78
for ( int i = 0 ; i < 50 ; i ++ )
80
79
{
81
- network . BackPropagate ( inputs [ t ] , desiredOutputs [ t ] ) ;
80
+ trainer . Train ( network , inputs [ t ] , desiredOutputs [ t ] ) ;
82
81
}
83
82
}
84
83
}
0 commit comments