- Timestamp:
- 04/09/20 14:01:09 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToTensorConverter.cs
r17493 r17502 29 29 using Tensorflow; 30 30 using static Tensorflow.Binding; 31 using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;32 31 33 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 34 33 public class TreeToTensorConverter { 35 34 36 #region helper class 37 public class DataForVariable { 38 public readonly string variableName; 39 public readonly string variableValue; // for factor vars 40 41 public DataForVariable(string varName, string varValue) { 42 this.variableName = varName; 43 this.variableValue = varValue; 44 } 45 46 public override bool Equals(object obj) { 47 var other = obj as DataForVariable; 48 if (other == null) return false; 49 return other.variableName.Equals(this.variableName) && 50 other.variableValue.Equals(this.variableValue); 51 } 52 53 public override int GetHashCode() { 54 return variableName.GetHashCode() ^ variableValue.GetHashCode(); 55 } 56 } 57 #endregion 35 private static readonly TF_DataType DataType = tf.float32; 58 36 59 37 public static bool TryConvert(ISymbolicExpressionTree tree, int numRows, Dictionary<string, int> variableLengths, 60 38 bool makeVariableWeightsVariable, bool addLinearScalingTerms, 61 out Tensor graph, out Dictionary<Tensor, string> parameters, out List<Tensor> variables 62 /*, out double[] initialConstants*/) { 39 out Tensor graph, out Dictionary<Tensor, string> parameters, out List<Tensor> variables) { 63 40 64 41 try { … … 66 43 graph = converter.ConvertNode(tree.Root.GetSubtree(0)); 67 44 68 //var parametersEntries = converter.parameters.ToList(); // guarantee same order for keys and values 69 parameters = converter.parameters; // parametersEntries.Select(kvp => kvp.Value).ToList(); 45 parameters = converter.parameters; 70 46 variables = converter.variables; 71 //initialConstants = converter.initialConstants.ToArray();72 47 return true; 73 48 } catch (NotSupportedException) { … … 75 50 parameters = null; 76 51 variables = null; 77 //initialConstants = null;78 52 return false; 79 53 } … … 85 59 private readonly bool addLinearScalingTerms; 86 60 87 //private readonly List<double> initialConstants = new List<double>();88 61 private readonly Dictionary<Tensor, string> parameters = new Dictionary<Tensor, string>(); 89 62 private readonly List<Tensor> variables = new List<Tensor>(); … … 97 70 98 71 99 100 72 private Tensor ConvertNode(ISymbolicExpressionTreeNode node) { 101 73 if (node.Symbol is Constant) { 102 var value = ((ConstantTreeNode)node).Value; 103 //initialConstants.Add(value); 104 #if EXPLICIT_SHAPE 105 //var var = (RefVariable)tf.VariableV1(value, name: $"c_{variables.Count}", dtype: tf.float64, shape: new[] { 1, 1 }); 74 var value = (float)((ConstantTreeNode)node).Value; 106 75 var value_arr = np.array(value).reshape(1, 1); 107 var var = tf.Variable(value_arr, name: $"c_{variables.Count}", dtype: tf.float64); 108 #endif 109 //var var = tf.Variable(value, name: $"c_{variables.Count}", dtype: tf.float64/*, shape: new[] { 1, 1 }*/); 76 var var = tf.Variable(value_arr, name: $"c_{variables.Count}", dtype: DataType); 110 77 variables.Add(var); 111 78 return var; … … 118 85 //var varValue = factorVarNode != null ? factorVarNode.VariableValue : string.Empty; 119 86 //var par = FindOrCreateParameter(parameters, varNode.VariableName, varValue); 120 #if EXPLICIT_SHAPE 121 var par = tf.placeholder(tf.float64, new TensorShape(numRows, variableLengths[varNode.VariableName]), name: varNode.VariableName); 122 #endif 87 var par = tf.placeholder(DataType, new TensorShape(numRows, variableLengths[varNode.VariableName]), name: varNode.VariableName); 123 88 parameters.Add(par, varNode.VariableName); 124 89 125 90 if (makeVariableWeightsVariable) { 126 //initialConstants.Add(varNode.Weight); 127 #if EXPLICIT_SHAPE 128 //var w = (RefVariable)tf.VariableV1(varNode.Weight, name: $"w_{varNode.VariableName}_{variables.Count}", dtype: tf.float64, shape: new[] { 1, 1 }); 129 var w_arr = np.array(varNode.Weight).reshape(1, 1); 130 var w = tf.Variable(w_arr, name: $"w_{varNode.VariableName}", dtype: tf.float64); 131 #endif 132 //var w = tf.Variable(varNode.Weight, name: $"w_{varNode.VariableName}_{variables.Count}", dtype: tf.float64/*, shape: new[] { 1, 1 }*/); 91 var w_arr = np.array((float)varNode.Weight).reshape(1, 1); 92 var w = tf.Variable(w_arr, name: $"w_{varNode.VariableName}", dtype: DataType); 133 93 variables.Add(w); 134 94 return w * par; … … 143 103 // foreach (var variableValue in factorVarNode.Symbol.GetVariableValues(factorVarNode.VariableName)) { 144 104 // //var par = FindOrCreateParameter(parameters, factorVarNode.VariableName, variableValue); 145 // var par = tf.placeholder( tf.float64, new TensorShape(numRows, 1), name: factorVarNode.VariableName);105 // var par = tf.placeholder(DataType, new TensorShape(numRows, 1), name: factorVarNode.VariableName); 146 106 // parameters.Add(par, factorVarNode.VariableName); 147 107 148 108 // var value = factorVarNode.GetValue(variableValue); 149 109 // //initialConstants.Add(value); 150 // var wVar = (RefVariable)tf.VariableV1(value, name: $"f_{factorVarNode.VariableName}_{variables.Count}", dtype: tf.float64, shape: new[] { 1, 1 });110 // var wVar = (RefVariable)tf.VariableV1(value, name: $"f_{factorVarNode.VariableName}_{variables.Count}", dtype: DataType, shape: new[] { 1, 1 }); 151 111 // //var wVar = tf.Variable(value, name: $"f_{factorVarNode.VariableName}_{variables.Count}"/*, shape: new[] { 1, 1 }*/); 152 112 // variables.Add(wVar); … … 159 119 160 120 if (node.Symbol is Addition) { 161 var terms = new List<Tensor>(); 162 foreach (var subTree in node.Subtrees) { 163 terms.Add(ConvertNode(subTree)); 164 } 165 121 var terms = node.Subtrees.Select(ConvertNode).ToList(); 166 122 return terms.Aggregate((a, b) => a + b); 167 123 } 168 124 169 125 if (node.Symbol is Subtraction) { 170 var terms = new List<Tensor>(); 171 for (int i = 0; i < node.SubtreeCount; i++) { 172 var t = ConvertNode(node.GetSubtree(i)); 173 if (i > 0) t = -t; 174 terms.Add(t); 175 } 176 126 var terms = node.Subtrees.Select(ConvertNode).ToList(); 177 127 if (terms.Count == 1) return -terms[0]; 178 else return terms.Aggregate((a, b) => a +b);128 return terms.Aggregate((a, b) => a - b); 179 129 } 180 130 181 131 if (node.Symbol is Multiplication) { 182 var terms = new List<Tensor>(); 183 foreach (var subTree in node.Subtrees) { 184 terms.Add(ConvertNode(subTree)); 185 } 186 187 if (terms.Count == 1) return terms[0]; 188 else return terms.Aggregate((a, b) => a * b); 132 var terms = node.Subtrees.Select(ConvertNode).ToList(); 133 return terms.Aggregate((a, b) => a * b); 189 134 } 190 135 191 136 if (node.Symbol is Division) { 192 var terms = new List<Tensor>(); 193 foreach (var subTree in node.Subtrees) { 194 terms.Add(ConvertNode(subTree)); 195 } 196 197 if (terms.Count == 1) return 1.0 / terms[0]; 198 else return terms.Aggregate((a, b) => a * (1.0 / b)); 137 var terms = node.Subtrees.Select(ConvertNode).ToList(); 138 if (terms.Count == 1) return 1.0f / terms[0]; 139 return terms.Aggregate((a, b) => a / b); 199 140 } 200 141 … … 207 148 var x1 = ConvertNode(node.GetSubtree(0)); 208 149 var x2 = ConvertNode(node.GetSubtree(1)); 209 return x1 / tf.pow(1 + x2 * x2, 0.5);150 return x1 / tf.pow(1.0f + x2 * x2, 0.5f); 210 151 } 211 152 … … 233 174 if (node.Symbol is Cube) { 234 175 return math_ops.pow( 235 ConvertNode(node.GetSubtree(0)), 3.0 );176 ConvertNode(node.GetSubtree(0)), 3.0f); 236 177 } 237 178 238 179 if (node.Symbol is CubeRoot) { 239 180 return math_ops.pow( 240 ConvertNode(node.GetSubtree(0)), 1.0 / 3.0);181 ConvertNode(node.GetSubtree(0)), 1.0f / 3.0f); 241 182 // TODO 242 183 // f: x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3), … … 281 222 282 223 if (node.Symbol is StartSymbol) { 224 Tensor prediction; 283 225 if (addLinearScalingTerms) { 284 226 // scaling variables α, β are given at the beginning of the parameter vector 285 #if EXPLICIT_SHAPE 286 //var alpha = (RefVariable)tf.VariableV1(1.0, name: $"alpha_{1.0}", dtype: tf.float64, shape: new[] { 1, 1 }); 287 //var beta = (RefVariable)tf.VariableV1(0.0, name: $"beta_{0.0}", dtype: tf.float64, shape: new[] { 1, 1 }); 288 289 var alpha_arr = np.array(1.0).reshape(1, 1); 290 var alpha = tf.Variable(alpha_arr, name: "alpha", dtype: tf.float64); 291 var beta_arr = np.array(0.0).reshape(1, 1); 292 var beta = tf.Variable(beta_arr, name: "beta", dtype: tf.float64); 293 #endif 294 //var alpha = tf.Variable(1.0, name: $"alpha_{1.0}", dtype: tf.float64/*, shape: new[] { 1, 1 }*/); 295 //var beta = tf.Variable(0.0, name: $"beta_{0.0}", dtype: tf.float64/*, shape: new[] { 1, 1 }*/); 227 var alpha_arr = np.array(1.0f).reshape(1, 1); 228 var alpha = tf.Variable(alpha_arr, name: "alpha", dtype: DataType); 229 var beta_arr = np.array(0.0f).reshape(1, 1); 230 var beta = tf.Variable(beta_arr, name: "beta", dtype: DataType); 296 231 variables.Add(alpha); 297 232 variables.Add(beta); 298 233 var t = ConvertNode(node.GetSubtree(0)); 299 return t * alpha + beta; 300 } else return ConvertNode(node.GetSubtree(0)); 234 prediction = t * alpha + beta; 235 } else { 236 prediction = ConvertNode(node.GetSubtree(0)); 237 } 238 239 return tf.reduce_sum(prediction, axis: new[] { 1 }); 301 240 } 302 241
Note: See TracChangeset
for help on using the changeset viewer.