Changeset 17502 for branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 04/09/20 14:01:09 (5 years ago)
- Location:
- branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r17475 r17502 47 47 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 48 48 <Prefer32Bit>false</Prefer32Bit> 49 <LangVersion>7</LangVersion> 49 50 </PropertyGroup> 50 51 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 57 58 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 58 59 <Prefer32Bit>false</Prefer32Bit> 60 <LangVersion>7</LangVersion> 59 61 </PropertyGroup> 60 62 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' "> … … 67 69 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 68 70 <Prefer32Bit>false</Prefer32Bit> 71 <LangVersion>7</LangVersion> 69 72 </PropertyGroup> 70 73 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' "> … … 77 80 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 78 81 <Prefer32Bit>false</Prefer32Bit> 82 <LangVersion>7</LangVersion> 79 83 </PropertyGroup> 80 84 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> … … 87 91 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 88 92 <Prefer32Bit>false</Prefer32Bit> 93 <LangVersion>7</LangVersion> 89 94 </PropertyGroup> 90 95 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> … … 97 102 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 98 103 <Prefer32Bit>false</Prefer32Bit> 104 <LangVersion>7</LangVersion> 99 105 </PropertyGroup> 100 106 <ItemGroup> … … 113 119 </Reference> 114 120 <Reference Include="System.Drawing" /> 121 <Reference Include="System.Numerics" /> 115 122 <Reference Include="System.Xml.Linq"> 116 123 <RequiredTargetFramework>3.5</RequiredTargetFramework> -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/TensorFlowConstantOptimizationEvaluator.cs
r17493 r17502 20 20 #endregion 21 21 22 #define EXPLICIT_SHAPE 22 //#define EXPORT_GRAPH 23 //#define LOG_CONSOLE 24 //#define LOG_FILE 23 25 24 26 using System; 25 27 using System.Collections; 26 28 using System.Collections.Generic; 29 #if LOG_CONSOLE 27 30 using System.Diagnostics; 31 #endif 32 #if LOG_FILE 33 using System.Globalization; 34 using System.IO; 35 #endif 28 36 using System.Linq; 29 37 using System.Threading; … … 46 54 private const string LearningRateName = "LearningRate"; 47 55 48 #region Parameter Properties 56 private static readonly TF_DataType DataType = tf.float32; 57 58 #region Parameter Properties 49 59 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter { 50 60 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumIterationsName]; } … … 53 63 get { return (IFixedValueParameter<DoubleValue>)Parameters[LearningRateName]; } 54 64 } 55 56 57 65 #endregion 66 67 #region Properties 58 68 public int ConstantOptimizationIterations { 59 69 get { return ConstantOptimizationIterationsParameter.Value.Value; } … … 62 72 get { return LearningRateParameter.Value.Value; } 63 73 } 64 74 #endregion 65 75 66 76 public TensorFlowConstantOptimizationEvaluator() 67 77 : base() { 68 78 Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree(0 indicates other or default stopping criterion).", new IntValue(10))); 69 Parameters.Add(new FixedValueParameter<DoubleValue>(LearningRateName, "", new DoubleValue(0.0 1)));79 Parameters.Add(new FixedValueParameter<DoubleValue>(LearningRateName, "", new DoubleValue(0.001))); 70 80 } 71 81 … … 87 97 ApplyLinearScalingParameter.ActualValue.Value, UpdateVariableWeights, 88 98 ConstantOptimizationIterations, LearningRate, 89 cancellationToken, counter); 90 } 91 92 public static ISymbolicExpressionTree OptimizeTree( 93 ISymbolicExpressionTree tree, 99 cancellationToken); 100 } 101 102 public static ISymbolicExpressionTree OptimizeTree(ISymbolicExpressionTree tree, 94 103 IRegressionProblemData problemData, IEnumerable<int> rows, 95 104 bool applyLinearScaling, bool updateVariableWeights, int maxIterations, double learningRate, 96 CancellationToken cancellationToken = default(CancellationToken), EvaluationsCounter counter= null) {105 CancellationToken cancellationToken = default(CancellationToken), IProgress<double> progress = null) { 97 106 98 107 int numRows = rows.Count(); … … 114 123 return (ISymbolicExpressionTree)tree.Clone(); 115 124 116 #if EXPLICIT_SHAPE 117 var target = tf.placeholder(tf.float64, new TensorShape(numRows, 1), name: problemData.TargetVariable); 118 #endif 119 // mse 120 var costs = tf.reduce_sum(tf.square(target - prediction)) / (2.0 * numRows); 121 var optimizer = tf.train.GradientDescentOptimizer((float)learningRate).minimize(costs); 125 var target = tf.placeholder(DataType, new TensorShape(numRows), name: problemData.TargetVariable); 126 // MSE 127 var cost = tf.reduce_mean(tf.square(prediction - target)); 128 129 var optimizer = tf.train.AdamOptimizer((float)learningRate); 130 //var optimizer = tf.train.GradientDescentOptimizer((float)learningRate); 131 var optimizationOperation = optimizer.minimize(cost); 132 133 #if EXPORT_GRAPH 134 //https://github.com/SciSharp/TensorFlow.NET/wiki/Debugging 135 tf.train.export_meta_graph(@"C:\temp\TFboard\graph.meta", as_text: false, 136 clear_devices: true, clear_extraneous_savers: false, strip_default_attrs: true); 137 #endif 122 138 123 139 // features as feed items … … 127 143 var variableName = kvp.Value; 128 144 if (problemData.Dataset.VariableHasType<double>(variableName)) { 129 var data = problemData.Dataset.GetDoubleValues(variableName, rows).ToArray(); 130 //if (vectorLength.HasValue) { 131 // var vectorData = new double[numRows][]; 132 // for (int i = 0; i < numRows; i++) 133 // vectorData[i] = Enumerable.Repeat(data[i], vectorLength.Value).ToArray(); 134 // variablesFeed.Add(variable, np.array(vectorData)); 135 //} else 145 var data = problemData.Dataset.GetDoubleValues(variableName, rows).Select(x => (float)x).ToArray(); 136 146 variablesFeed.Add(variable, np.array(data, copy: false).reshape(numRows, 1)); 137 //} else if (problemData.Dataset.VariableHasType<string>(variableName)) {138 // variablesFeed.Add(variable, problemData.Dataset.GetStringValues(variableName, rows));139 147 } else if (problemData.Dataset.VariableHasType<DoubleVector>(variableName)) { 140 var data = problemData.Dataset.GetDoubleVectorValues(variableName, rows).Select(x => x. ToArray()).ToArray();148 var data = problemData.Dataset.GetDoubleVectorValues(variableName, rows).Select(x => x.Select(y => (float)y).ToArray()).ToArray(); 141 149 variablesFeed.Add(variable, np.array(data)); 142 150 } else 143 151 throw new NotSupportedException($"Type of the variable is not supported: {variableName}"); 144 152 } 145 var targetData = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).ToArray(); 146 variablesFeed.Add(target, np.array(targetData, copy: false).reshape(numRows, 1)); 153 var targetData = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).Select(x => (float)x).ToArray(); 154 variablesFeed.Add(target, np.array(targetData, copy: false)); 155 147 156 148 157 List<NDArray> constants; 149 158 using (var session = tf.Session()) { 159 160 #if LOG_FILE 161 var directoryName = $"C:\\temp\\TFboard\\logdir\\manual_{DateTime.Now.ToString("yyyyMMddHHmmss")}_{maxIterations}_{learningRate.ToString(CultureInfo.InvariantCulture)}"; 162 Directory.CreateDirectory(directoryName); 163 var costsWriter = new StreamWriter(File.Create(Path.Combine(directoryName, "Costs.csv"))); 164 var weightsWriter = new StreamWriter(File.Create(Path.Combine(directoryName, "Weights.csv"))); 165 var gradientsWriter = new StreamWriter(File.Create(Path.Combine(directoryName, "Gradients.csv"))); 166 #endif 167 168 #if LOG_CONSOLE || LOG_FILE 169 var gradients = optimizer.compute_gradients(cost); 170 #endif 171 150 172 session.run(tf.global_variables_initializer()); 151 173 152 // https://github.com/SciSharp/TensorFlow.NET/wiki/Debugging 153 tf.train.export_meta_graph(@"C:\temp\TFboard\graph.meta", as_text: false); 174 progress?.Report(session.run(cost, variablesFeed)[0].GetValue<float>(0)); 175 176 177 #if LOG_CONSOLE 178 Trace.WriteLine("Costs:"); 179 Trace.WriteLine($"MSE: {session.run(cost, variablesFeed)[0].ToString(true)}"); 154 180 155 181 Trace.WriteLine("Weights:"); 156 foreach (var v in variables) 182 foreach (var v in variables) { 157 183 Trace.WriteLine($"{v.name}: {session.run(v).ToString(true)}"); 184 } 185 186 Trace.WriteLine("Gradients:"); 187 foreach (var t in gradients) { 188 Trace.WriteLine($"{t.Item2.name}: {session.run(t.Item1, variablesFeed)[0].ToString(true)}"); 189 } 190 #endif 191 192 #if LOG_FILE 193 costsWriter.WriteLine("MSE"); 194 costsWriter.WriteLine(session.run(cost, variablesFeed)[0].GetValue<float>(0).ToString(CultureInfo.InvariantCulture)); 195 196 weightsWriter.WriteLine(string.Join(";", variables.Select(v => v.name))); 197 weightsWriter.WriteLine(string.Join(";", variables.Select(v => session.run(v).GetValue<float>(0, 0).ToString(CultureInfo.InvariantCulture)))); 198 199 gradientsWriter.WriteLine(string.Join(";", gradients.Select(t => t.Item2.name))); 200 gradientsWriter.WriteLine(string.Join(";", gradients.Select(t => session.run(t.Item1, variablesFeed)[0].GetValue<float>(0, 0).ToString(CultureInfo.InvariantCulture)))); 201 #endif 158 202 159 203 for (int i = 0; i < maxIterations; i++) { 160 161 //optimizer.minimize(costs); 162 session.run(optimizer, variablesFeed); 204 if (cancellationToken.IsCancellationRequested) 205 break; 206 207 session.run(optimizationOperation, variablesFeed); 208 209 progress?.Report(session.run(cost, variablesFeed)[0].GetValue<float>(0)); 210 211 #if LOG_CONSOLE 212 Trace.WriteLine("Costs:"); 213 Trace.WriteLine($"MSE: {session.run(cost, variablesFeed)[0].ToString(true)}"); 163 214 164 215 Trace.WriteLine("Weights:"); 165 foreach (var v in variables) 216 foreach (var v in variables) { 166 217 Trace.WriteLine($"{v.name}: {session.run(v).ToString(true)}"); 167 } 168 218 } 219 220 Trace.WriteLine("Gradients:"); 221 foreach (var t in gradients) { 222 Trace.WriteLine($"{t.Item2.name}: {session.run(t.Item1, variablesFeed)[0].ToString(true)}"); 223 } 224 #endif 225 226 #if LOG_FILE 227 costsWriter.WriteLine(session.run(cost, variablesFeed)[0].GetValue<float>(0).ToString(CultureInfo.InvariantCulture)); 228 weightsWriter.WriteLine(string.Join(";", variables.Select(v => session.run(v).GetValue<float>(0, 0).ToString(CultureInfo.InvariantCulture)))); 229 gradientsWriter.WriteLine(string.Join(";", gradients.Select(t => session.run(t.Item1, variablesFeed)[0].GetValue<float>(0, 0).ToString(CultureInfo.InvariantCulture)))); 230 #endif 231 } 232 233 #if LOG_FILE 234 costsWriter.Close(); 235 weightsWriter.Close(); 236 gradientsWriter.Close(); 237 #endif 169 238 constants = variables.Select(v => session.run(v)).ToList(); 170 239 } … … 182 251 foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) { 183 252 if (node is ConstantTreeNode constantTreeNode) 184 constantTreeNode.Value = constants[i++].Get Double(0, 0);253 constantTreeNode.Value = constants[i++].GetValue<float>(0, 0); 185 254 else if (node is VariableTreeNodeBase variableTreeNodeBase && updateVariableWeights) 186 variableTreeNodeBase.Weight = constants[i++].Get Double(0, 0);255 variableTreeNodeBase.Weight = constants[i++].GetValue<float>(0, 0); 187 256 else if (node is FactorVariableTreeNode factorVarTreeNode && updateVariableWeights) { 188 257 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 189 factorVarTreeNode.Weights[j] = constants[i++].Get Double(0, 0);258 factorVarTreeNode.Weights[j] = constants[i++].GetValue<float>(0, 0); 190 259 } 191 260 }
Note: See TracChangeset
for help on using the changeset viewer.