Changeset 17489 for branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/TensorFlowConstantOptimizationEvaluator.cs
- Timestamp:
- 04/01/20 15:49:03 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/TensorFlowConstantOptimizationEvaluator.cs
r17476 r17489 19 19 */ 20 20 #endregion 21 22 #define EXPLICIT_SHAPE 21 23 22 24 using System; … … 94 96 CancellationToken cancellationToken = default(CancellationToken), EvaluationsCounter counter = null) { 95 97 96 var vectorVariables = tree.IterateNodesBreadth()97 .OfType<VariableTreeNodeBase>()98 .Where(node => problemData.Dataset.VariableHasType<DoubleVector>(node.VariableName))99 .Select(node => node.VariableName);100 101 int? vectorLength = null;102 if (vectorVariables.Any()) {103 vectorLength = vectorVariables.Select(var => problemData.Dataset.GetDoubleVectorValues(var, rows)).First().First().Count;104 }105 98 int numRows = rows.Count(); 99 var variableLengths = problemData.AllowedInputVariables.ToDictionary( 100 var => var, 101 var => { 102 if (problemData.Dataset.VariableHasType<double>(var)) return 1; 103 if (problemData.Dataset.VariableHasType<DoubleVector>(var)) return problemData.Dataset.GetDoubleVectorValue(var, 0).Count; 104 throw new NotSupportedException($"Type of variable {var} is not supported."); 105 }); 106 106 107 107 bool success = TreeToTensorConverter.TryConvert(tree, 108 numRows, v ectorLength,108 numRows, variableLengths, 109 109 updateVariableWeights, applyLinearScaling, 110 110 out Tensor prediction, 111 111 out Dictionary<Tensor, string> parameters, out List<Tensor> variables/*, out double[] initialConstants*/); 112 112 113 var target = tf.placeholder(tf.float64, name: problemData.TargetVariable); 114 int samples = rows.Count(); 113 #if EXPLICIT_SHAPE 114 var target = tf.placeholder(tf.float64, new TensorShape(numRows, 1), name: problemData.TargetVariable); 115 #endif 115 116 // mse 116 var costs = tf.reduce_sum(tf.square( prediction - target)) / (2.0 * samples);117 var costs = tf.reduce_sum(tf.square(target - prediction)) / (2.0 * numRows); 117 118 var optimizer = tf.train.GradientDescentOptimizer((float)learningRate).minimize(costs); 118 119 … … 124 125 if (problemData.Dataset.VariableHasType<double>(variableName)) { 125 126 var data = problemData.Dataset.GetDoubleValues(variableName, rows).ToArray(); 126 if (vectorLength.HasValue) {127 var vectorData = new double[numRows][];128 for (int i = 0; i < numRows; i++)129 vectorData[i] = Enumerable.Repeat(data[i], vectorLength.Value).ToArray();130 variablesFeed.Add(variable, np.array(vectorData));131 } else132 variablesFeed.Add(variable, np.array(data, copy: false));127 //if (vectorLength.HasValue) { 128 // var vectorData = new double[numRows][]; 129 // for (int i = 0; i < numRows; i++) 130 // vectorData[i] = Enumerable.Repeat(data[i], vectorLength.Value).ToArray(); 131 // variablesFeed.Add(variable, np.array(vectorData)); 132 //} else 133 variablesFeed.Add(variable, np.array(data, copy: false).reshape(numRows, 1)); 133 134 //} else if (problemData.Dataset.VariableHasType<string>(variableName)) { 134 135 // variablesFeed.Add(variable, problemData.Dataset.GetStringValues(variableName, rows)); … … 140 141 } 141 142 var targetData = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).ToArray(); 142 variablesFeed.Add(target, np.array(targetData, copy: false)); 143 143 variablesFeed.Add(target, np.array(targetData, copy: false).reshape(numRows, 1)); 144 144 145 145 using (var session = tf.Session()) { 146 146 session.run(tf.global_variables_initializer()); 147 148 // https://github.com/SciSharp/TensorFlow.NET/wiki/Debugging 149 tf.train.export_meta_graph(@"C:\temp\TFboard\graph.meta", as_text: false); 147 150 148 151 Trace.WriteLine("Weights:");
Note: See TracChangeset
for help on using the changeset viewer.