Changeset 2985 for trunk/sources/HeuristicLab.ArtificialNeuralNetworks
- Timestamp:
- 03/09/10 20:26:03 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/HeuristicLab.ArtificialNeuralNetworks-3.2.csproj
r2808 r2985 138 138 <None Include="HeuristicLab.snk" /> 139 139 <None Include="Properties\AssemblyInfo.frame" /> 140 <Compile Include="MultiLayerPerceptronTimeSeries.cs" /> 140 141 <Compile Include="Properties\AssemblyInfo.cs" /> 141 142 </ItemGroup> -
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptron.cs
r2562 r2985 68 68 69 69 clone.inputVariables = new List<string>(inputVariables); 70 clone.minTimeOffset = MinTimeOffset; 71 clone.maxTimeOffset = MaxTimeOffset; 70 72 71 73 double[] ra = null; … … 78 80 public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 79 81 XmlNode node = base.GetXmlNode(name, document, persistedObjects); 80 82 var minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset"); 83 minTimeOffsetAttr.Value = minTimeOffset.ToString(); 84 var maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset"); 85 maxTimeOffsetAttr.Value = maxTimeOffset.ToString(); 86 node.Attributes.Append(minTimeOffsetAttr); 87 node.Attributes.Append(maxTimeOffsetAttr); 81 88 XmlNode networkInformation = document.CreateElement("NetworkInformation"); 82 89 double[] ra = null; … … 94 101 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 95 102 base.Populate(node, restoredObjects); 103 minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value); 104 maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value); 96 105 double[] ra = (from s in node.SelectSingleNode("NetworkInformation").InnerText.Split(';') 97 106 select double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); -
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptronEvaluator.cs
r2562 r2985 52 52 select ((StringData)x).Data; 53 53 string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data; 54 int targetVariableIndex = dataset.GetVariableIndex(targetVariable);54 // int targetVariableIndex = dataset.GetVariableIndex(targetVariable); 55 55 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 56 56 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; … … 61 61 MultiLayerPerceptron model = GetVariableValue<MultiLayerPerceptron>("MultiLayerPerceptron", scope, true); 62 62 63 double[,] values = new double[end - start, 2]; 64 for (int i = 0; i < end - start; i++) { 63 double[] targetVector; 64 double[,] inputMatrix; 65 MultiLayerPerceptronRegressionOperator.PrepareDataset(dataset, targetVariable, inputVariableNames, start, end, minTimeOffset, maxTimeOffset, 66 out inputMatrix, out targetVector); 67 68 double[,] values = new double[targetVector.Length, 2]; 69 for (int i = 0; i < targetVector.Length; i++) { 65 70 double[] output = new double[1]; 66 double[] inputRow = new double[ dataset.Columns - 1];67 for (int c = 1; c < inputRow.Length; c++) {68 inputRow[c - 1] = dataset.GetValue(i + start, c);71 double[] inputRow = new double[inputMatrix.GetLength(1)]; 72 for (int c = 0; c < inputRow.Length; c++) { 73 inputRow[c] = inputMatrix[i, c]; 69 74 } 70 75 alglib.mlpbase.multilayerperceptron p = model.Perceptron; 71 76 alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output); 72 77 model.Perceptron = p; 73 values[i, 0] = dataset.GetValue(start + i, targetVariableIndex);78 values[i, 0] = targetVector[i]; 74 79 values[i, 1] = output[0]; 75 80 } -
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptronRegression.cs
r2570 r2985 123 123 get { 124 124 if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result."); 125 IScope bestModelScope = engine.GlobalScope ;125 IScope bestModelScope = engine.GlobalScope.SubScopes[0]; 126 126 return CreateMlpModel(bestModelScope); 127 127 } -
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptronRegressionOperator.cs
r2562 r2985 36 36 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 37 37 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 38 AddVariableInfo(new VariableInfo("ValidationSamplesStart", "Start of validation set", typeof(IntData), VariableKind.In)); 39 AddVariableInfo(new VariableInfo("ValidationSamplesEnd", "End of validation set", typeof(IntData), VariableKind.In)); 38 40 AddVariableInfo(new VariableInfo("NumberOfHiddenLayerNeurons", "The number of nodes in the hidden layer.", typeof(IntData), VariableKind.In)); 39 41 AddVariableInfo(new VariableInfo("MaxTimeOffset", "(optional) Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In)); … … 48 50 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 49 51 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; 52 53 int valStart = GetVariableValue<IntData>("ValidationSamplesStart", scope, true).Data; 54 int valEnd = GetVariableValue<IntData>("ValidationSamplesEnd", scope, true).Data; 55 56 50 57 IntData maxTimeOffsetData = GetVariableValue<IntData>("MaxTimeOffset", scope, true, false); 51 58 int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data; … … 54 61 int nHiddenNodes = GetVariableValue<IntData>("NumberOfHiddenLayerNeurons", scope, true).Data; 55 62 56 var perceptron = CreateModel(dataset, targetVariable, dataset.VariableNames, start, end, minTimeOffset, maxTimeOffset, nHiddenNodes);63 var perceptron = CreateModel(dataset, targetVariable, dataset.VariableNames, start, end, valStart, valEnd, minTimeOffset, maxTimeOffset, nHiddenNodes); 57 64 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MultiLayerPerceptron"), perceptron)); 58 65 return null; 59 66 } 60 67 61 public static MultiLayerPerceptron CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end, int nHiddenNodes) {62 return CreateModel(dataset, targetVariable, inputVariables, start, end, 0, 0, nHiddenNodes);63 }68 //public static MultiLayerPerceptron CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end, int nHiddenNodes) { 69 // return CreateModel(dataset, targetVariable, inputVariables, start, end, 0, 0, nHiddenNodes); 70 //} 64 71 65 72 public static MultiLayerPerceptron CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, 66 int start, int end, 73 int start, int end, int valStart, int valEnd, 67 74 int minTimeOffset, int maxTimeOffset, int nHiddenNodes) { 68 int targetVariableIndex = dataset.GetVariableIndex(targetVariable); 69 List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariableIndex, inputVariables.Select(x => dataset.GetVariableIndex(x)), start, end); 70 List<int> allowedRows = CalculateAllowedRows(dataset, targetVariableIndex, allowedColumns, start, end, minTimeOffset, maxTimeOffset); 71 72 double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset); 73 double[] targetVector = PrepareTargetVector(dataset, targetVariableIndex, allowedRows); 74 75 var perceptron = TrainPerceptron(inputMatrix, targetVector, nHiddenNodes); 75 double[,] inputMatrix; 76 double[,] validationData; 77 double[] targetVector; 78 double[] validationTargetVector; 79 PrepareDataset(dataset, targetVariable, inputVariables, start, end, minTimeOffset, maxTimeOffset, out inputMatrix, out targetVector); 80 PrepareDataset(dataset, targetVariable, inputVariables, valStart, valEnd, minTimeOffset, maxTimeOffset, out validationData, out validationTargetVector); 81 var perceptron = TrainPerceptron(inputMatrix, targetVector, nHiddenNodes, validationData, validationTargetVector); 76 82 return new MultiLayerPerceptron(perceptron, inputVariables, minTimeOffset, maxTimeOffset); 77 83 } … … 79 85 80 86 81 private static alglib.mlpbase.multilayerperceptron TrainPerceptron(double[,] inputMatrix, double[] targetVector, int nHiddenNodes ) {87 private static alglib.mlpbase.multilayerperceptron TrainPerceptron(double[,] inputMatrix, double[] targetVector, int nHiddenNodes, double[,] validationData, double[] validationTargetVector) { 82 88 int retVal = 0; 83 89 int n = targetVector.Length; 90 int validationN = validationTargetVector.Length; 84 91 int p = inputMatrix.GetLength(1); 85 92 alglib.mlpbase.multilayerperceptron perceptron = new alglib.mlpbase.multilayerperceptron(); … … 93 100 dataset[row, p - 1] = targetVector[row]; 94 101 } 95 alglib.mlptrain.mlptrainlbfgs(ref perceptron, ref dataset, n, 0.001, 2, 0.01, 0, ref retVal, ref report); 96 if (retVal != 2) throw new ArgumentException("Error in training of multi layer perceptron"); 102 double[,] validationDataset = new double[validationN, p]; 103 for (int row = 0; row < validationN; row++) { 104 for (int column = 0; column < p - 1; column++) { 105 validationDataset[row, column] = validationData[row, column]; 106 } 107 validationDataset[row, p - 1] = validationTargetVector[row]; 108 } 109 //alglib.mlptrain.mlptrainlbfgs(ref perceptron, ref dataset, n, 0.001, 10, 0.01, 0, ref retVal, ref report); 110 alglib.mlptrain.mlptraines(ref perceptron, ref dataset, n, ref validationDataset, validationN, 0.001, 10, ref retVal, ref report); 111 if (retVal != 2 && retVal != 6) throw new ArgumentException("Error in training of multi layer perceptron"); 97 112 return perceptron; 113 } 114 115 public static void PrepareDataset(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end, int minTimeOffset, int maxTimeOffset, 116 out double[,] inputMatrix, out double[] targetVector) { 117 int targetVariableIndex = dataset.GetVariableIndex(targetVariable); 118 List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariableIndex, inputVariables.Select(x => dataset.GetVariableIndex(x)), start, end); 119 List<int> allowedRows = CalculateAllowedRows(dataset, targetVariableIndex, allowedColumns, start, end, minTimeOffset, maxTimeOffset); 120 121 inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset); 122 targetVector = PrepareTargetVector(dataset, targetVariableIndex, allowedRows); 123 98 124 } 99 125 … … 139 165 int rowCount = allowedRows.Count; 140 166 int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1); 141 double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange) + 1];167 double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange)]; 142 168 for (int row = 0; row < allowedRows.Count; row++) 143 169 for (int col = 0; col < allowedColumns.Count; col++) { … … 145 171 matrix[row, (col * timeOffsetRange) + (timeOffset - minTimeOffset)] = dataset.GetValue(allowedRows[row] + timeOffset, allowedColumns[col]); 146 172 } 147 //add constant 1.0 in last column148 for (int i = 0; i < rowCount; i++)149 matrix[i, allowedColumns.Count * timeOffsetRange] = 1.0;150 173 return matrix; 151 174 } -
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/Predictor.cs
r2619 r2985 27 27 using System.Xml; 28 28 using HeuristicLab.DataAnalysis; 29 using System.Linq; 29 30 30 31 namespace HeuristicLab.ArtificialNeuralNetworks { … … 44 45 for (int i = 0; i < end - start; i++) { 45 46 double[] output = new double[1]; 46 double[] inputRow = new double[input.Columns - 1]; 47 for (int c = 1; c < inputRow.Length; c++) { 48 inputRow[c - 1] = input.GetValue(i + start, c); 47 double[] inputRow = new double[GetInputVariables().Count()]; 48 int c = 0; 49 foreach (var inputVariable in GetInputVariables()) { 50 int inputVariableIndex = input.GetVariableIndex(inputVariable); 51 inputRow[c++] = input.GetValue(i + start, inputVariableIndex); 49 52 } 50 alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron; 51 alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output); 52 perceptron.Perceptron = p; 53 yield return Math.Max(Math.Min(output[0], UpperPredictionLimit), LowerPredictionLimit); 53 double estimatedValue; 54 try { 55 alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron; 56 alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output); 57 perceptron.Perceptron = p; 58 estimatedValue = output[0]; 59 } 60 catch (ArithmeticException) { 61 estimatedValue = UpperPredictionLimit; 62 } 63 yield return Math.Max(Math.Min(estimatedValue, UpperPredictionLimit), LowerPredictionLimit); 54 64 } 55 65 }
Note: See TracChangeset
for help on using the changeset viewer.