Changeset 18239
- Timestamp:
- 03/22/22 13:28:56 (3 years ago)
- Location:
- branches/3040_VectorBasedGP
- Files:
-
- 1 deleted
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.ExtLibs.sln
r17930 r18239 85 85 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.MathNet.Numerics", "HeuristicLab.MathNet.Numerics\HeuristicLab.MathNet.Numerics.csproj", "{71AE8AE6-F1A5-445C-AD31-AFFC83CE2E28}" 86 86 EndProject 87 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlowNetILMerge", "HeuristicLab.ExtLibs\TensorFlowNet\TensorFlowNetILMerge.csproj", "{99BF44AE-436E-4B30-9435-E2BA98EE6FB3}"88 EndProject89 87 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.TensorFlowNet", "HeuristicLab.ExtLibs\HeuristicLab.TensorFlowNet\HeuristicLab.TensorFlowNet.csproj", "{C2E26343-88C7-4288-938B-EEE7C2ABBDB6}" 90 ProjectSection(ProjectDependencies) = postProject91 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3} = {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}92 EndProjectSection93 EndProject94 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiffSharpILMerge", "HeuristicLab.ExtLibs\DiffSharp\DiffSharpILMerge.csproj", "{0C9D4608-1B14-404E-9932-F60E67B625BA}"95 EndProject96 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.DiffSharp", "HeuristicLab.ExtLibs\HeuristicLab.DiffSharp\HeuristicLab.DiffSharp.csproj", "{99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}"97 ProjectSection(ProjectDependencies) = postProject98 {0C9D4608-1B14-404E-9932-F60E67B625BA} = {0C9D4608-1B14-404E-9932-F60E67B625BA}99 EndProjectSection100 88 EndProject 101 89 Global … … 561 549 {71AE8AE6-F1A5-445C-AD31-AFFC83CE2E28}.Release|x86.ActiveCfg = Release|Any CPU 562 550 {71AE8AE6-F1A5-445C-AD31-AFFC83CE2E28}.Release|x86.Build.0 = Release|Any CPU 563 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU564 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU565 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x64.ActiveCfg = Debug|x64566 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x64.Build.0 = Debug|x64567 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x86.ActiveCfg = Debug|Any CPU568 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x86.Build.0 = Debug|Any CPU569 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU570 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|Any CPU.Build.0 = Release|Any CPU571 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x64.ActiveCfg = Release|x64572 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x64.Build.0 = Release|x64573 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x86.ActiveCfg = Release|Any CPU574 {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x86.Build.0 = Release|Any CPU575 551 {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 576 552 {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Debug|Any CPU.Build.0 = Debug|Any CPU … … 585 561 {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Release|x86.ActiveCfg = Release|Any CPU 586 562 {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Release|x86.Build.0 = Release|Any CPU 587 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|Any CPU.ActiveCfg = Debug|x64588 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|x64.ActiveCfg = Debug|x64589 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|x64.Build.0 = Debug|x64590 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|x86.ActiveCfg = Debug|x64591 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|Any CPU.ActiveCfg = Release|x64592 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|x64.ActiveCfg = Release|x64593 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|x64.Build.0 = Release|x64594 {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|x86.ActiveCfg = Release|x64595 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|Any CPU.ActiveCfg = Debug|x64596 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|x64.ActiveCfg = Debug|x64597 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|x64.Build.0 = Debug|x64598 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|x86.ActiveCfg = Debug|x64599 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|Any CPU.ActiveCfg = Release|x64600 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|x64.ActiveCfg = Release|x64601 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|x64.Build.0 = Release|x64602 {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|x86.ActiveCfg = Release|x64603 563 EndGlobalSection 604 564 GlobalSection(SolutionProperties) = preSolution -
branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.Attic/1.0.0/HeuristicLab.Attic.csproj
r17935 r18239 54 54 </PropertyGroup> 55 55 <ItemGroup> 56 <Reference Include="HEAL.Attic, Version=1.7.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 57 <SpecificVersion>False</SpecificVersion> 58 <HintPath>..\..\..\bin\HEAL.Attic.dll</HintPath> 59 </Reference> 56 60 <Reference Include="System" /> 57 61 <Reference Include="System.Core" /> … … 78 82 </ItemGroup> 79 83 <ItemGroup> 80 <PackageReference Include="HEAL.Attic">81 <Version>1.5.0</Version>82 </PackageReference>83 </ItemGroup>84 <ItemGroup>85 84 <ProjectReference Include="..\..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj"> 86 85 <Project>{94186a6a-5176-4402-ae83-886557b53cca}</Project> … … 92 91 <Name>HeuristicLab.Protobuf</Name> 93 92 </ProjectReference> 93 </ItemGroup> 94 <ItemGroup> 95 <PackageReference Include="System.Runtime.CompilerServices.Unsafe"> 96 <Version>6.0.0</Version> 97 </PackageReference> 94 98 </ItemGroup> 95 99 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.Protobuf/3.6.1/HeuristicLab.Protobuf.csproj
r17930 r18239 61 61 <ItemGroup> 62 62 <PackageReference Include="Google.Protobuf"> 63 <Version>3.6.1</Version> 63 <Version>3.11.3</Version> 64 </PackageReference> 65 <PackageReference Include="System.Runtime.CompilerServices.Unsafe"> 66 <Version>6.0.0</Version> 64 67 </PackageReference> 65 68 </ItemGroup> -
branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.TensorFlowNet/HeuristicLab.TensorFlowNet.csproj
r17930 r18239 10 10 <RootNamespace>HeuristicLab.TensorFlowNet</RootNamespace> 11 11 <AssemblyName>HeuristicLab.TensorFlowNet</AssemblyName> 12 <TargetFrameworkVersion>v4.7 </TargetFrameworkVersion>12 <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> 13 13 <FileAlignment>512</FileAlignment> 14 14 <Deterministic>true</Deterministic> 15 <TargetFrameworkProfile /> 15 16 </PropertyGroup> 16 17 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 55 56 <Reference Include="System" /> 56 57 <Reference Include="System.Core" /> 57 <Reference Include="TensorFlow.NET.Merged, Version=0.15.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">58 <SpecificVersion>False</SpecificVersion>59 <HintPath>..\..\bin\TensorFlow.NET.Merged.dll</HintPath>60 </Reference>61 58 </ItemGroup> 62 59 <ItemGroup> … … 73 70 </ItemGroup> 74 71 <ItemGroup> 75 <Content Include="tensorflow.dll"> 76 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 77 </Content> 72 <PackageReference Include="SciSharp.TensorFlow.Redist"> 73 <Version>2.7.0</Version> 74 </PackageReference> 75 <PackageReference Include="TensorFlow.Keras"> 76 <Version>0.7.0</Version> 77 </PackageReference> 78 <PackageReference Include="TensorFlow.NET"> 79 <Version>0.70.0</Version> 80 </PackageReference> 78 81 </ItemGroup> 79 82 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.TensorFlowNet/Plugin.cs.frame
r17935 r18239 26 26 [Plugin("HeuristicLab.TensorFlowNet", "HeuristicLab wrapper for TensofFlow.NET nuget package.", "0.15.0.$WCREV$")] 27 27 [PluginFile("HeuristicLab.TensorFlowNet.dll", PluginFileType.Assembly)] 28 [PluginFile("TensorFlow.NET.Merged.dll", PluginFileType.Assembly)] 28 [PluginFile("TensorFlow.Binding.dll", PluginFileType.Assembly)] 29 [PluginFile("TensorFlow.Keras.dll", PluginFileType.Assembly)] 30 [PluginFile("Protobuf.Text.dll", PluginFileType.Assembly)] 31 [PluginFile("Serilog.dll", PluginFileType.Assembly)] 32 [PluginFile("Serilog.Sinks.Console.dll", PluginFileType.Assembly)] 29 33 [PluginFile("tensorflow.dll", PluginFileType.NativeDll)] 30 [PluginFile("System.Memory.dll", PluginFileType.Assembly)] 31 [PluginDependency("HeuristicLab.Protobuf", "3.6.1")] 34 //[PluginDependency("HeuristicLab.Protobuf", "3.6.1")] 32 35 public class Plugin : PluginBase { 33 36 public Plugin() { } -
branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/TensorFlowNet/TensorFlowNetILMerge.csproj
r17930 r18239 10 10 <RootNamespace>TensorFlowNetILMerge</RootNamespace> 11 11 <AssemblyName>TensorFlowNetILMerge_</AssemblyName> 12 <TargetFrameworkVersion>v4.7 </TargetFrameworkVersion>12 <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> 13 13 <FileAlignment>512</FileAlignment> 14 14 <Deterministic>true</Deterministic> 15 <TargetFrameworkProfile /> 15 16 </PropertyGroup> 16 17 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 57 58 </ItemGroup> 58 59 <ItemGroup> 59 <PackageReference Include="Google.Protobuf">60 <Version>3.6.1</Version>61 </PackageReference>62 60 <PackageReference Include="ilmerge"> 63 61 <Version>3.0.41</Version> 64 62 </PackageReference> 65 63 <PackageReference Include="TensorFlow.NET"> 66 <Version>0. 15.0</Version>64 <Version>0.70.1</Version> 67 65 </PackageReference> 68 66 </ItemGroup> 69 67 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 70 68 <PropertyGroup> 71 <PostBuildEvent>"$(ILMergeConsolePath)" /out:TensorFlow.NET.Merged.dll /keyfile:$(SolutionDir)HeuristicLab\3.3\HeuristicLab.snk TensorFlow.NET.dll NumSharp.Lite.dll Protobuf.Text.dll 72 73 rm NumSharp.Lite.pdb 74 rm NumSharp.Lite.dll 75 rm TensorFlow.NET.dll 76 rm Protobuf.Text.dll 77 </PostBuildEvent> 69 <PostBuildEvent> 70 </PostBuildEvent> 78 71 </PropertyGroup> 79 72 <PropertyGroup> -
branches/3040_VectorBasedGP/HeuristicLab.MathNet.Numerics/HeuristicLab.MathNet.Numerics.csproj
r17930 r18239 56 56 </PropertyGroup> 57 57 <ItemGroup> 58 <Reference Include="HEAL.Attic, Version=1. 5.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">58 <Reference Include="HEAL.Attic, Version=1.7.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 59 59 <SpecificVersion>False</SpecificVersion> 60 60 <HintPath>..\bin\HEAL.Attic.dll</HintPath> … … 73 73 </ItemGroup> 74 74 <ItemGroup> 75 <ProjectReference Include="..\HeuristicLab.ExtLibs\HeuristicLab.Attic\1.0.0\HeuristicLab.Attic.csproj">76 <Project>{44a10478-d290-43c2-9757-f08f5292fea5}</Project>77 <Name>HeuristicLab.Attic</Name>78 </ProjectReference>79 75 <ProjectReference Include="..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj"> 80 76 <Project>{94186a6a-5176-4402-ae83-886557b53cca}</Project> … … 83 79 </ItemGroup> 84 80 <ItemGroup> 81 <PackageReference Include="Google.Protobuf"> 82 <Version>3.11.3</Version> 83 </PackageReference> 85 84 <PackageReference Include="MathNet.Numerics.Signed"> 86 85 <Version>4.9.0</Version> 86 </PackageReference> 87 <PackageReference Include="System.Runtime.CompilerServices.Unsafe"> 88 <Version>6.0.0</Version> 87 89 </PackageReference> 88 90 </ItemGroup> -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs
r18238 r18239 47 47 nudLearningRate.Enabled = tree != null && TensorFlowConstantOptimizationEvaluator.CanOptimizeConstants(tree); 48 48 btnUnrollingVectorOptimizeConstants.Enabled = tree != null && VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.CanOptimizeConstants(tree); 49 #if INCLUDE_DIFFSHARP 49 50 btnDiffSharpOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresVectorConstantOptimizationEvaluator.CanOptimizeConstants(tree); 51 #endif 50 52 } 51 53 … … 95 97 return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, 96 98 regressionProblemData.TrainingIndices, 97 applyLinearScaling: true, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate, 99 //new int[]{ 0, 1 }, 100 applyLinearScaling: false, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate, 98 101 cancellationToken: cancellationToken, 99 102 progress: new SynchronousProgress<double>(cost => { … … 121 124 tree = VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree( 122 125 tree, interpreter, 123 regressionProblemData, regressionProblemData.TrainingIndices, 124 applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true, 126 regressionProblemData, 127 regressionProblemData.TrainingIndices, 128 //new int[] { 0, 1 }, 129 applyLinearScaling: false, maxIterations: constOptIterations, updateVariableWeights: true, 125 130 cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => { 126 131 double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported … … 150 155 int reps = 0; 151 156 157 #if INCLUDE_DIFFSHARP 152 158 do { 153 159 prevResult = result; … … 166 172 progress.ProgressState != ProgressState.StopRequested && 167 173 progress.ProgressState != ProgressState.CancelRequested); 174 #endif 168 175 return tree; 169 176 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r17930 r18239 47 47 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 48 48 <Prefer32Bit>false</Prefer32Bit> 49 <LangVersion> 7</LangVersion>49 <LangVersion>latest</LangVersion> 50 50 </PropertyGroup> 51 51 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 58 58 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 59 59 <Prefer32Bit>false</Prefer32Bit> 60 <LangVersion> 7</LangVersion>60 <LangVersion>latest</LangVersion> 61 61 </PropertyGroup> 62 62 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' "> … … 69 69 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 70 70 <Prefer32Bit>false</Prefer32Bit> 71 <LangVersion> 7</LangVersion>71 <LangVersion>latest</LangVersion> 72 72 </PropertyGroup> 73 73 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' "> … … 80 80 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 81 81 <Prefer32Bit>false</Prefer32Bit> 82 <LangVersion> 7</LangVersion>82 <LangVersion>latest</LangVersion> 83 83 </PropertyGroup> 84 84 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> … … 91 91 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 92 92 <Prefer32Bit>false</Prefer32Bit> 93 <LangVersion> 7</LangVersion>93 <LangVersion>latest</LangVersion> 94 94 </PropertyGroup> 95 95 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> … … 102 102 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 103 103 <Prefer32Bit>false</Prefer32Bit> 104 <LangVersion> 7</LangVersion>104 <LangVersion>latest</LangVersion> 105 105 </PropertyGroup> 106 106 <ItemGroup> … … 109 109 <HintPath>..\..\bin\ALGLIB-3.7.0.dll</HintPath> 110 110 <Private>False</Private> 111 </Reference>112 <Reference Include="DiffSharp.Merged, Version=0.8.4.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=AMD64">113 <SpecificVersion>False</SpecificVersion>114 <HintPath>..\..\bin\DiffSharp.Merged.dll</HintPath>115 111 </Reference> 116 112 <Reference Include="MathNet.Numerics"> … … 133 129 <Reference Include="System.Data" /> 134 130 <Reference Include="System.Xml" /> 135 <Reference Include="Tensor Flow.NET.Merged, Version=0.15.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">131 <Reference Include="Tensorflow.Binding, Version=0.70.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=AMD64"> 136 132 <SpecificVersion>False</SpecificVersion> 137 <HintPath>..\..\bin\TensorFlow.NET.Merged.dll</HintPath> 133 <HintPath>..\..\bin\Tensorflow.Binding.dll</HintPath> 134 </Reference> 135 <Reference Include="Tensorflow.Keras, Version=0.7.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=AMD64"> 136 <SpecificVersion>False</SpecificVersion> 137 <HintPath>..\..\bin\Tensorflow.Keras.dll</HintPath> 138 138 </Reference> 139 139 </ItemGroup> -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Plugin.cs.frame
r17786 r18239 43 43 [PluginDependency("HeuristicLab.MathNet.Numerics", "4.9.0")] 44 44 [PluginDependency("HeuristicLab.TensorFlowNet", "0.15.0")] 45 [PluginDependency("HeuristicLab.DiffSharp", "0.7.7")]45 //[PluginDependency("HeuristicLab.DiffSharp", "0.7.7")] 46 46 public class HeuristicLabProblemsDataAnalysisSymbolicRegressionPlugin : PluginBase { 47 47 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NonlinearLeastSquaresVectorConstantOptimizationEvaluator.cs
r17930 r18239 19 19 */ 20 20 #endregion 21 22 #if INCLUDE_DIFFSHARP 21 23 22 24 using System; … … 194 196 } 195 197 } 198 199 #endif -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/TensorFlowConstantOptimizationEvaluator.cs
r17721 r18239 25 25 26 26 using System; 27 using System.Collections;28 27 using System.Collections.Generic; 29 28 #if LOG_CONSOLE … … 42 41 using HeuristicLab.Parameters; 43 42 using HEAL.Attic; 44 using NumSharp;45 43 using Tensorflow; 44 using Tensorflow.NumPy; 46 45 using static Tensorflow.Binding; 46 using static Tensorflow.KerasApi; 47 47 using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>; 48 48 … … 54 54 private const string LearningRateName = "LearningRate"; 55 55 56 //private static readonly TF_DataType DataType = tf.float64; 56 57 private static readonly TF_DataType DataType = tf.float32; 57 58 … … 105 106 CancellationToken cancellationToken = default(CancellationToken), IProgress<double> progress = null) { 106 107 107 int numRows = rows.Count(); 108 var variableLengths = problemData.AllowedInputVariables.ToDictionary( 109 var => var, 110 var => { 111 if (problemData.Dataset.VariableHasType<double>(var)) return 1; 112 if (problemData.Dataset.VariableHasType<DoubleVector>(var)) return problemData.Dataset.GetDoubleVectorValue(var, 0).Count; 113 throw new NotSupportedException($"Type of variable {var} is not supported."); 114 }); 115 116 bool success = TreeToTensorConverter.TryConvert(tree, 117 numRows, variableLengths, 108 const bool eager = true; 109 110 bool prepared = TreeToTensorConverter.TryPrepareTree( 111 tree, 112 problemData, rows.ToList(), 118 113 updateVariableWeights, applyLinearScaling, 119 out Tensor prediction,120 out Dictionary< Tensor, string> parameters, out List<Tensor> variables/*, out double[] initialConstants*/);121 122 if (! success)114 eager, 115 out Dictionary<string, Tensor> inputFeatures, out Tensor target, 116 out Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]> variables); 117 if (!prepared) 123 118 return (ISymbolicExpressionTree)tree.Clone(); 124 119 125 var target = tf.placeholder(DataType, new TensorShape(numRows), name: problemData.TargetVariable); 126 // MSE 127 var cost = tf.reduce_mean(tf.square(target - prediction)); 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 138 139 // features as feed items 140 var variablesFeed = new Hashtable(); 141 foreach (var kvp in parameters) { 142 var variable = kvp.Key; 143 var variableName = kvp.Value; 144 if (problemData.Dataset.VariableHasType<double>(variableName)) { 145 var data = problemData.Dataset.GetDoubleValues(variableName, rows).Select(x => (float)x).ToArray(); 146 variablesFeed.Add(variable, np.array(data).reshape(numRows, 1)); 147 } else if (problemData.Dataset.VariableHasType<DoubleVector>(variableName)) { 148 var data = problemData.Dataset.GetDoubleVectorValues(variableName, rows).Select(x => x.Select(y => (float)y).ToArray()).ToArray(); 149 variablesFeed.Add(variable, np.array(data)); 150 } else 151 throw new NotSupportedException($"Type of the variable is not supported: {variableName}"); 120 var optimizer = keras.optimizers.Adam((float)learningRate); 121 122 for (int i = 0; i < maxIterations; i++) { 123 if (cancellationToken.IsCancellationRequested) break; 124 125 using var tape = tf.GradientTape(); 126 127 bool success = TreeToTensorConverter.TryEvaluate( 128 tree, 129 inputFeatures, variables, 130 updateVariableWeights, applyLinearScaling, 131 eager, 132 out Tensor prediction); 133 if (!success) 134 return (ISymbolicExpressionTree)tree.Clone(); 135 136 var loss = tf.reduce_mean(tf.square(target - prediction)); 137 138 progress?.Report(loss.ToArray<float>()[0]); 139 140 var variablesList = variables.Values.SelectMany(x => x).ToList(); 141 var gradients = tape.gradient(loss, variablesList); 142 143 optimizer.apply_gradients(zip(gradients, variablesList)); 152 144 } 153 var targetData = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).Select(x => (float)x).ToArray(); 154 variablesFeed.Add(target, np.array(targetData)); 155 156 157 List<NDArray> constants; 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 172 session.run(tf.global_variables_initializer()); 173 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)}"); 180 181 Trace.WriteLine("Weights:"); 182 foreach (var v in variables) { 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 202 203 for (int i = 0; i < maxIterations; i++) { 204 if (cancellationToken.IsCancellationRequested) 145 146 var cloner = new Cloner(); 147 var newTree = cloner.Clone(tree); 148 var newConstants = variables.ToDictionary( 149 kvp => (ISymbolicExpressionTreeNode)cloner.GetClone(kvp.Key), 150 kvp => kvp.Value.Select(x => (double)(x.numpy().ToArray<float>()[0])).ToArray() 151 ); 152 UpdateConstants(newTree, newConstants); 153 154 155 return newTree; 156 157 158 159 160 161 // //int numRows = rows.Count(); 162 163 164 165 166 167 168 // var variableLengths = problemData.AllowedInputVariables.ToDictionary( 169 // var => var, 170 // var => { 171 // if (problemData.Dataset.VariableHasType<double>(var)) return 1; 172 // if (problemData.Dataset.VariableHasType<DoubleVector>(var)) return problemData.Dataset.GetDoubleVectorValue(var, 0).Count; 173 // throw new NotSupportedException($"Type of variable {var} is not supported."); 174 // }); 175 176 // var variablesDict = problemData.AllowedInputVariables.ToDictionary( 177 // var => var, 178 // var => { 179 // if (problemData.Dataset.VariableHasType<double>(var)) { 180 // var data = problemData.Dataset.GetDoubleValues(var, rows).Select(x => (float)x).ToArray(); 181 // return tf.convert_to_tensor(np.array(data).reshape(new Shape(numRows, 1)), DataType); 182 // } else if (problemData.Dataset.VariableHasType<DoubleVector>(var)) { 183 // var data = problemData.Dataset.GetDoubleVectorValues(var, rows).SelectMany(x => x.Select(y => (float)y)).ToArray(); 184 // return tf.convert_to_tensor(np.array(data).reshape(new Shape(numRows, -1)), DataType); 185 // } else throw new NotSupportedException($"Type of the variable is not supported: {var}"); 186 // } 187 // ); 188 189 // using var tape = tf.GradientTape(persistent: true); 190 191 // bool success = TreeToTensorConverter.TryEvaluateEager(tree, 192 // numRows, variablesDict, 193 // updateVariableWeights, applyLinearScaling, 194 // out Tensor prediction, 195 // out Dictionary<Tensor, string> parameters, out List<ResourceVariable> variables); 196 197 // //bool success = TreeToTensorConverter.TryConvert(tree, 198 // // numRows, variableLengths, 199 // // updateVariableWeights, applyLinearScaling, 200 // // out Tensor prediction, 201 // // out Dictionary<Tensor, string> parameters, out List<Tensor> variables); 202 203 // if (!success) 204 // return (ISymbolicExpressionTree)tree.Clone(); 205 206 // //var target = tf.placeholder(DataType, new Shape(numRows), name: problemData.TargetVariable); 207 // var targetData = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).Select(x => (float)x).ToArray(); 208 // var target = tf.convert_to_tensor(np.array(targetData).reshape(new Shape(numRows)), DataType); 209 // // MSE 210 // var cost = tf.reduce_sum(tf.square(prediction - target)); 211 212 // tape.watch(cost); 213 214 // //var optimizer = tf.train.AdamOptimizer((float)learningRate); 215 // //var optimizer = tf.train.AdamOptimizer(tf.constant(learningRate, DataType)); 216 // //var optimizer = tf.train.GradientDescentOptimizer((float)learningRate); 217 // //var optimizer = tf.train.GradientDescentOptimizer(tf.constant(learningRate, DataType)); 218 // //var optimizer = tf.train.GradientDescentOptimizer((float)learningRate); 219 // //var optimizer = tf.train.AdamOptimizer((float)learningRate); 220 // //var optimizationOperation = optimizer.minimize(cost); 221 // var optimizer = keras.optimizers.Adam((float)learningRate); 222 223 // #if EXPORT_GRAPH 224 // //https://github.com/SciSharp/TensorFlow.NET/wiki/Debugging 225 // tf.train.export_meta_graph(@"C:\temp\TFboard\graph.meta", as_text: false, 226 // clear_devices: true, clear_extraneous_savers: false, strip_default_attrs: true); 227 //#endif 228 229 // //// features as feed items 230 // //var variablesFeed = new Hashtable(); 231 // //foreach (var kvp in parameters) { 232 // // var variable = kvp.Key; 233 // // var variableName = kvp.Value; 234 // // if (problemData.Dataset.VariableHasType<double>(variableName)) { 235 // // var data = problemData.Dataset.GetDoubleValues(variableName, rows).Select(x => (float)x).ToArray(); 236 // // variablesFeed.Add(variable, np.array(data).reshape(new Shape(numRows, 1))); 237 // // } else if (problemData.Dataset.VariableHasType<DoubleVector>(variableName)) { 238 // // var data = problemData.Dataset.GetDoubleVectorValues(variableName, rows).SelectMany(x => x.Select(y => (float)y)).ToArray(); 239 // // variablesFeed.Add(variable, np.array(data).reshape(new Shape(numRows, -1))); 240 // // } else 241 // // throw new NotSupportedException($"Type of the variable is not supported: {variableName}"); 242 // //} 243 // //var targetData = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).Select(x => (float)x).ToArray(); 244 // //variablesFeed.Add(target, np.array(targetData)); 245 246 247 // List<NDArray> constants; 248 // //using (var session = tf.Session()) { 249 250 //#if LOG_FILE 251 // var directoryName = $"C:\\temp\\TFboard\\logdir\\manual_{DateTime.Now.ToString("yyyyMMddHHmmss")}_{maxIterations}_{learningRate.ToString(CultureInfo.InvariantCulture)}"; 252 // Directory.CreateDirectory(directoryName); 253 // var costsWriter = new StreamWriter(File.Create(Path.Combine(directoryName, "Costs.csv"))); 254 // var weightsWriter = new StreamWriter(File.Create(Path.Combine(directoryName, "Weights.csv"))); 255 // var gradientsWriter = new StreamWriter(File.Create(Path.Combine(directoryName, "Gradients.csv"))); 256 //#endif 257 258 // //session.run(tf.global_variables_initializer()); 259 260 //#if LOG_CONSOLE || LOG_FILE 261 // var gradients = optimizer.compute_gradients(cost); 262 //#endif 263 264 // //var vars = variables.Select(v => session.run(v, variablesFeed)[0].ToArray<float>()[0]).ToList(); 265 // //var gradient = optimizer.compute_gradients(cost) 266 // // .Where(g => g.Item1 != null) 267 // // //.Select(g => session.run(g.Item1, variablesFeed)[0].GetValue<float>(0)). 268 // // .Select(g => session.run(g.Item1, variablesFeed)[0].ToArray<float>()[0]) 269 // // .ToList(); 270 271 // //var gradientPrediction = optimizer.compute_gradients(prediction) 272 // // .Where(g => g.Item1 != null) 273 // // .Select(g => session.run(g.Item1, variablesFeed)[0].ToArray<float>()[0]) 274 // // .ToList(); 275 276 277 // //progress?.Report(session.run(cost, variablesFeed)[0].ToArray<float>()[0]); 278 // progress?.Report(cost.ToArray<float>()[0]); 279 280 281 282 283 284 //#if LOG_CONSOLE 285 // Trace.WriteLine("Costs:"); 286 // Trace.WriteLine($"MSE: {session.run(cost, variablesFeed)[0].ToString(true)}"); 287 288 // Trace.WriteLine("Weights:"); 289 // foreach (var v in variables) { 290 // Trace.WriteLine($"{v.name}: {session.run(v).ToString(true)}"); 291 // } 292 293 // Trace.WriteLine("Gradients:"); 294 // foreach (var t in gradients) { 295 // Trace.WriteLine($"{t.Item2.name}: {session.run(t.Item1, variablesFeed)[0].ToString(true)}"); 296 // } 297 //#endif 298 299 //#if LOG_FILE 300 // costsWriter.WriteLine("MSE"); 301 // costsWriter.WriteLine(session.run(cost, variablesFeed)[0].ToArray<float>()[0].ToString(CultureInfo.InvariantCulture)); 302 303 // weightsWriter.WriteLine(string.Join(";", variables.Select(v => v.name))); 304 // weightsWriter.WriteLine(string.Join(";", variables.Select(v => session.run(v).ToArray<float>()[0].ToString(CultureInfo.InvariantCulture)))); 305 306 // gradientsWriter.WriteLine(string.Join(";", gradients.Select(t => t.Item2.Name))); 307 // gradientsWriter.WriteLine(string.Join(";", gradients.Select(t => session.run(t.Item1, variablesFeed)[0].ToArray<float>()[0].ToString(CultureInfo.InvariantCulture)))); 308 //#endif 309 310 // for (int i = 0; i < maxIterations; i++) { 311 // if (cancellationToken.IsCancellationRequested) 312 // break; 313 314 315 // var gradients = tape.gradient(cost, variables); 316 // //optimizer.apply_gradients(gradients.Zip(variables, Tuple.Create<Tensor, IVariableV1>).ToArray()); 317 // optimizer.apply_gradients(zip(gradients, variables)); 318 319 320 // //session.run(optimizationOperation, variablesFeed); 321 322 // progress?.Report(cost.ToArray<float>()[0]); 323 // //progress?.Report(session.run(cost, variablesFeed)[0].ToArray<float>()[0]); 324 325 //#if LOG_CONSOLE 326 // Trace.WriteLine("Costs:"); 327 // Trace.WriteLine($"MSE: {session.run(cost, variablesFeed)[0].ToString(true)}"); 328 329 // Trace.WriteLine("Weights:"); 330 // foreach (var v in variables) { 331 // Trace.WriteLine($"{v.name}: {session.run(v).ToString(true)}"); 332 // } 333 334 // Trace.WriteLine("Gradients:"); 335 // foreach (var t in gradients) { 336 // Trace.WriteLine($"{t.Item2.name}: {session.run(t.Item1, variablesFeed)[0].ToString(true)}"); 337 // } 338 //#endif 339 340 //#if LOG_FILE 341 // costsWriter.WriteLine(session.run(cost, variablesFeed)[0].ToArray<float>()[0].ToString(CultureInfo.InvariantCulture)); 342 // weightsWriter.WriteLine(string.Join(";", variables.Select(v => session.run(v).ToArray<float>()[0].ToString(CultureInfo.InvariantCulture)))); 343 // gradientsWriter.WriteLine(string.Join(";", gradients.Select(t => session.run(t.Item1, variablesFeed)[0].ToArray<float>()[0].ToString(CultureInfo.InvariantCulture)))); 344 //#endif 345 // } 346 347 //#if LOG_FILE 348 // costsWriter.Close(); 349 // weightsWriter.Close(); 350 // gradientsWriter.Close(); 351 //#endif 352 // //constants = variables.Select(v => session.run(v)).ToList(); 353 // constants = variables.Select(v => v.numpy()).ToList(); 354 // //} 355 356 // if (applyLinearScaling) 357 // constants = constants.Skip(2).ToList(); 358 // var newTree = (ISymbolicExpressionTree)tree.Clone(); 359 // UpdateConstants(newTree, constants, updateVariableWeights); 360 361 // return newTree; 362 } 363 364 private static void UpdateConstants(ISymbolicExpressionTree tree, Dictionary<ISymbolicExpressionTreeNode, double[]> constants) { 365 foreach (var kvp in constants) { 366 var node = kvp.Key; 367 var value = kvp.Value; 368 369 switch (node) { 370 case ConstantTreeNode constantTreeNode: 371 constantTreeNode.Value = value[0]; 205 372 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)}"); 214 215 Trace.WriteLine("Weights:"); 216 foreach (var v in variables) { 217 Trace.WriteLine($"{v.name}: {session.run(v).ToString(true)}"); 373 case VariableTreeNodeBase variableTreeNodeBase: 374 variableTreeNodeBase.Weight = value[0]; 375 break; 376 case FactorVariableTreeNode factorVarTreeNode: { 377 for (int i = 0; i < factorVarTreeNode.Weights.Length; i++) { 378 factorVarTreeNode.Weights[i] = value[i]; 379 } 380 break; 218 381 } 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 #endif225 226 #if LOG_FILE227 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 #endif231 }232 233 #if LOG_FILE234 costsWriter.Close();235 weightsWriter.Close();236 gradientsWriter.Close();237 #endif238 constants = variables.Select(v => session.run(v)).ToList();239 }240 241 if (applyLinearScaling)242 constants = constants.Skip(2).ToList();243 var newTree = (ISymbolicExpressionTree)tree.Clone();244 UpdateConstants(newTree, constants, updateVariableWeights);245 246 return newTree;247 }248 249 private static void UpdateConstants(ISymbolicExpressionTree tree, IList<NDArray> constants, bool updateVariableWeights) {250 int i = 0;251 foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {252 if (node is ConstantTreeNode constantTreeNode)253 constantTreeNode.Value = constants[i++].GetValue<float>(0, 0);254 else if (node is VariableTreeNodeBase variableTreeNodeBase && updateVariableWeights)255 variableTreeNodeBase.Weight = constants[i++].GetValue<float>(0, 0);256 else if (node is FactorVariableTreeNode factorVarTreeNode && updateVariableWeights) {257 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)258 factorVarTreeNode.Weights[j] = constants[i++].GetValue<float>(0, 0);259 382 } 260 383 } 261 384 } 262 385 386 //private static void UpdateConstants(ISymbolicExpressionTree tree, IList<NDArray> constants, bool updateVariableWeights) { 387 // int i = 0; 388 // foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) { 389 // if (node is ConstantTreeNode constantTreeNode) { 390 // constantTreeNode.Value = constants[i++].ToArray<float>()[0]; 391 // } else if (node is VariableTreeNodeBase variableTreeNodeBase && updateVariableWeights) { 392 // variableTreeNodeBase.Weight = constants[i++].ToArray<float>()[0]; 393 // } else if (node is FactorVariableTreeNode factorVarTreeNode && updateVariableWeights) { 394 // for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 395 // factorVarTreeNode.Weights[j] = constants[i++].ToArray<float>()[0]; 396 // } 397 // } 398 //} 399 263 400 public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) { 264 401 return TreeToTensorConverter.IsCompatible(tree); -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToDiffSharpConverter.cs
r17930 r18239 19 19 */ 20 20 #endregion 21 22 #if INCLUDE_DIFFSHARP 21 23 22 24 using System; … … 505 507 } 506 508 } 509 510 #endif -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToTensorConverter.cs
r18238 r18239 20 20 #endregion 21 21 22 #define EXPLICIT_SHAPE23 24 22 using System; 25 23 using System.Collections.Generic; 26 24 using System.Linq; 27 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 using NumSharp;29 26 using Tensorflow; 27 using Tensorflow.NumPy; 30 28 using static Tensorflow.Binding; 29 using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>; 31 30 32 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 32 public class TreeToTensorConverter { 34 33 34 //private static readonly TF_DataType DataType = tf.float64; 35 35 private static readonly TF_DataType DataType = tf.float32; 36 36 37 public static bool TryConvert(ISymbolicExpressionTree tree, int numRows, Dictionary<string, int> variableLengths, 38 bool makeVariableWeightsVariable, bool addLinearScalingTerms, 39 out Tensor graph, out Dictionary<Tensor, string> parameters, out List<Tensor> variables) { 37 public static bool TryPrepareTree( 38 ISymbolicExpressionTree tree, 39 IRegressionProblemData problemData, List<int> rows, 40 bool updateVariableWeights, bool applyLinearScaling, 41 bool eagerEvaluation, 42 out Dictionary<string, Tensor> inputFeatures, out Tensor target, 43 out Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]> variables) { 40 44 41 45 try { 42 var converter = new TreeToTensorConverter(numRows, variableLengths, makeVariableWeightsVariable, addLinearScalingTerms); 43 graph = converter.ConvertNode(tree.Root.GetSubtree(0)); 44 45 parameters = converter.parameters; 46 var converter = new TreeToTensorConverter( 47 problemData, rows, 48 updateVariableWeights, applyLinearScaling, 49 eagerEvaluation 50 ); 51 52 if (eagerEvaluation) 53 tf.enable_eager_execution(); 54 else 55 tf.compat.v1.disable_eager_execution(); 56 57 converter.PrepareNode(tree.Root.GetSubtree(0)); 58 59 inputFeatures = converter.inputFeatures; 60 target = InputFeatureToTensor(problemData.TargetVariable, problemData, rows); 46 61 variables = converter.variables; 62 47 63 return true; 48 64 } catch (NotSupportedException) { 49 graph= null;50 parameters= null;65 inputFeatures = null; 66 target= null; 51 67 variables = null; 52 68 return false; 53 69 } 54 } 55 56 private readonly int numRows; 57 private readonly Dictionary<string, int> variableLengths; 70 71 } 72 73 public static bool TryEvaluate( 74 ISymbolicExpressionTree tree, 75 Dictionary<string, Tensor> inputFeatures, Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]> variables, 76 bool makeVariableWeightsVariable, bool addLinearScalingTerms, 77 bool eagerEvaluation, 78 out Tensor prediction) { 79 80 try { 81 var converter = new TreeToTensorConverter( 82 inputFeatures, variables, 83 makeVariableWeightsVariable, addLinearScalingTerms, 84 eagerEvaluation 85 ); 86 87 if (eagerEvaluation) 88 tf.enable_eager_execution(); 89 else 90 tf.compat.v1.disable_eager_execution(); 91 92 prediction = converter.EvaluateNode(tree.Root.GetSubtree(0)); 93 94 return true; 95 } catch (NotSupportedException) { 96 prediction = null; 97 return false; 98 } 99 100 } 101 102 //public static bool TryConvert/*Lazy/Graph*/( 103 // ISymbolicExpressionTree tree, 104 // Dictionary<string, Tensor> inputFeatures, Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]> variables, 105 // bool makeVariableWeightsVariable, bool addLinearScalingTerms, 106 // out Tensor prediction) { 107 108 // try { 109 // var converter = new TreeToTensorConverter( 110 // inputFeatures, variables, 111 // makeVariableWeightsVariable, addLinearScalingTerms, 112 // eagerEvaluation: false 113 // ); 114 115 // tf.compat.v1.disable_eager_execution(); 116 // prediction = converter.EvaluateNode(tree.Root.GetSubtree(0)); 117 // return true; 118 // } catch (NotSupportedException) { 119 // prediction = null; 120 // return false; 121 // } 122 //} 123 124 125 private readonly IDataAnalysisProblemData problemData; 126 private readonly List<int> rows; 127 128 private readonly Dictionary<string, Tensor> inputFeatures = new Dictionary<string, Tensor>(); 129 private readonly Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]> variables = new Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]>(); 130 58 131 private readonly bool makeVariableWeightsVariable; 59 132 private readonly bool addLinearScalingTerms; 60 61 private readonly Dictionary<Tensor, string> parameters = new Dictionary<Tensor, string>(); 62 private readonly List<Tensor> variables = new List<Tensor>(); 63 64 private TreeToTensorConverter(int numRows, Dictionary<string, int> variableLengths, bool makeVariableWeightsVariable, bool addLinearScalingTerms) { 65 this.numRows = numRows; 66 this.variableLengths = variableLengths; 133 private readonly bool eagerEvaluation; 134 135 private TreeToTensorConverter( 136 IDataAnalysisProblemData problemData, List<int> rows, 137 bool makeVariableWeightsVariable, bool addLinearScalingTerms, 138 bool eagerEvaluation 139 ) { 140 this.problemData = problemData; 141 this.rows = rows; 142 67 143 this.makeVariableWeightsVariable = makeVariableWeightsVariable; 68 144 this.addLinearScalingTerms = addLinearScalingTerms; 69 } 70 71 72 private Tensor ConvertNode(ISymbolicExpressionTreeNode node) { 145 this.eagerEvaluation = eagerEvaluation; 146 } 147 148 private TreeToTensorConverter( 149 Dictionary<string, Tensor> inputFeatures, Dictionary<ISymbolicExpressionTreeNode, ResourceVariable[]> variables, 150 bool makeVariableWeightsVariable, bool addLinearScalingTerms, 151 bool eagerEvaluation 152 ) { 153 this.inputFeatures = inputFeatures; 154 this.variables = variables; 155 156 this.makeVariableWeightsVariable = makeVariableWeightsVariable; 157 this.addLinearScalingTerms = addLinearScalingTerms; 158 this.eagerEvaluation = eagerEvaluation; 159 } 160 161 private static Tensor InputFeatureToTensor(string var, IDataAnalysisProblemData problemData, List<int> rows) { 162 if (problemData.Dataset.VariableHasType<double>(var)) { 163 var data = problemData.Dataset.GetDoubleValues(var, rows).Select(x => (float)x).ToArray(); 164 return tf.convert_to_tensor(np.array(data).reshape(new Shape(rows.Count, 1)), DataType); 165 } else if (problemData.Dataset.VariableHasType<DoubleVector>(var)) { 166 var data = problemData.Dataset.GetDoubleVectorValues(var, rows).SelectMany(x => x.Select(y => (float)y)).ToArray(); 167 return tf.convert_to_tensor(np.array(data).reshape(new Shape(rows.Count, -1)), DataType); 168 } else throw new NotSupportedException($"Type of the variable is not supported: {var}"); 169 } 170 private static Tensor InputFeatureToPlaceholder(string var, IDataAnalysisProblemData problemData, List<int> rows) { 171 if (problemData.Dataset.VariableHasType<double>(var)) { 172 return tf.placeholder(DataType, new Shape(rows.Count, 1), name: var); 173 } else if (problemData.Dataset.VariableHasType<DoubleVector>(var)) { 174 //var vectorLength = problemData.Dataset.GetDoubleVectorValues(var, rows).Select(v => v.Count).Distinct().Single(); 175 var vectorLength = problemData.Dataset.GetDoubleVectorValue(var, rows[0]).Count; 176 return tf.placeholder(DataType, new Shape(rows.Count, vectorLength), name: var); 177 } else throw new NotSupportedException($"Type of the variable is not supported: {var}"); 178 } 179 180 private void PrepareNode(ISymbolicExpressionTreeNode node) { 181 if (node.Symbol is Constant ) { 182 var constantNode = (ConstantTreeNode)node; 183 var value = (float)constantNode.Value; 184 var value_arr = np.array(value).reshape(new Shape(1, 1)); 185 var c = tf.Variable(value_arr, name: $"c_{variables.Count}", dtype: DataType); 186 variables.Add(node, new[] { c }); 187 } else if (node.Symbol is Variable) { 188 var varNode = (VariableTreeNodeBase)node; 189 if (makeVariableWeightsVariable) { 190 var w_arr = np.array((float)varNode.Weight).reshape(new Shape(1, 1)); 191 var w = tf.Variable(w_arr, name: $"w_{varNode.VariableName}", dtype: DataType); 192 variables.Add(node, new[] { w }); 193 } 194 if (!inputFeatures.ContainsKey(varNode.VariableName)) { 195 inputFeatures.Add( 196 varNode.VariableName, 197 eagerEvaluation 198 ? InputFeatureToTensor(varNode.VariableName, problemData, rows) 199 : InputFeatureToPlaceholder(varNode.VariableName, problemData, rows)); 200 } 201 } else if (node.Symbol is StartSymbol) { 202 if (addLinearScalingTerms) { 203 var alpha_arr = np.array(1.0f).reshape(new Shape(1, 1)); 204 var alpha = tf.Variable(alpha_arr, name: "alpha", dtype: DataType); 205 var beta_arr = np.array(0.0f).reshape(new Shape(1, 1)); 206 var beta = tf.Variable(beta_arr, name: "beta", dtype: DataType); 207 variables.Add(node, new[] { beta, alpha }); 208 } 209 } 210 211 foreach (var subTree in node.Subtrees) { 212 PrepareNode(subTree); 213 } 214 } 215 216 217 private Tensor EvaluateNode(ISymbolicExpressionTreeNode node) { 73 218 if (node.Symbol is Constant) { 74 var value = (float)((ConstantTreeNode)node).Value; 75 var value_arr = np.array(value).reshape(1, 1); 76 var var = tf.Variable(value_arr, name: $"c_{variables.Count}", dtype: DataType); 77 variables.Add(var); 78 return var; 219 return variables[node][0]; 79 220 } 80 221 81 222 if (node.Symbol is Variable/* || node.Symbol is BinaryFactorVariable*/) { 82 223 var varNode = node as VariableTreeNodeBase; 83 //var factorVarNode = node as BinaryFactorVariableTreeNode; 84 // factor variable values are only 0 or 1 and set in x accordingly 85 //var varValue = factorVarNode != null ? factorVarNode.VariableValue : string.Empty; 86 //var par = FindOrCreateParameter(parameters, varNode.VariableName, varValue); 87 var par = tf.placeholder(DataType, new TensorShape(numRows, variableLengths[varNode.VariableName]), name: varNode.VariableName); 88 parameters.Add(par, varNode.VariableName); 89 224 225 var par = inputFeatures[varNode.VariableName]; // eager or placeholder 90 226 if (makeVariableWeightsVariable) { 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); 93 variables.Add(w); 227 var w = variables[node][0]; 94 228 return w * par; 95 229 } else { … … 119 253 120 254 if (node.Symbol is Addition) { 121 var terms = node.Subtrees.Select( ConvertNode).ToList();255 var terms = node.Subtrees.Select(EvaluateNode).ToList(); 122 256 if (terms.Count == 1) return terms[0]; 123 257 return terms.Aggregate((a, b) => a + b); … … 125 259 126 260 if (node.Symbol is Subtraction) { 127 var terms = node.Subtrees.Select( ConvertNode).ToList();261 var terms = node.Subtrees.Select(EvaluateNode).ToList(); 128 262 if (terms.Count == 1) return -terms[0]; 129 263 return terms.Aggregate((a, b) => a - b); … … 131 265 132 266 if (node.Symbol is Multiplication) { 133 var terms = node.Subtrees.Select( ConvertNode).ToList();267 var terms = node.Subtrees.Select(EvaluateNode).ToList(); 134 268 if (terms.Count == 1) return terms[0]; 135 269 return terms.Aggregate((a, b) => a * b); … … 137 271 138 272 if (node.Symbol is Division) { 139 var terms = node.Subtrees.Select(ConvertNode).ToList(); 140 if (terms.Count == 1) return 1.0f / terms[0]; 273 var terms = node.Subtrees.Select(EvaluateNode).ToList(); 274 //if (terms.Count == 1) return 1.0f / terms[0]; 275 if (terms.Count == 1) return 1.0 / terms[0]; 141 276 return terms.Aggregate((a, b) => a / b); 142 277 } 143 278 144 279 if (node.Symbol is Absolute) { 145 var x1 = ConvertNode(node.GetSubtree(0));280 var x1 = EvaluateNode(node.GetSubtree(0)); 146 281 return tf.abs(x1); 147 282 } 148 283 149 284 if (node.Symbol is AnalyticQuotient) { 150 var x1 = ConvertNode(node.GetSubtree(0));151 var x2 = ConvertNode(node.GetSubtree(1));285 var x1 = EvaluateNode(node.GetSubtree(0)); 286 var x2 = EvaluateNode(node.GetSubtree(1)); 152 287 return x1 / tf.pow(1.0f + x2 * x2, 0.5f); 288 //return x1 / tf.pow(1.0 + x2 * x2, 0.5); 153 289 } 154 290 155 291 if (node.Symbol is Logarithm) { 156 292 return tf.log( 157 ConvertNode(node.GetSubtree(0)));293 EvaluateNode(node.GetSubtree(0))); 158 294 } 159 295 … … 161 297 return tf.pow( 162 298 (float)Math.E, 163 ConvertNode(node.GetSubtree(0))); 299 //Math.E, 300 EvaluateNode(node.GetSubtree(0))); 164 301 } 165 302 166 303 if (node.Symbol is Square) { 167 304 return tf.square( 168 ConvertNode(node.GetSubtree(0)));305 EvaluateNode(node.GetSubtree(0))); 169 306 } 170 307 171 308 if (node.Symbol is SquareRoot) { 172 309 return tf.sqrt( 173 ConvertNode(node.GetSubtree(0)));310 EvaluateNode(node.GetSubtree(0))); 174 311 } 175 312 176 313 if (node.Symbol is Cube) { 177 314 return tf.pow( 178 ConvertNode(node.GetSubtree(0)), 3.0f); 315 EvaluateNode(node.GetSubtree(0)), 3.0f); 316 //ConvertNode(node.GetSubtree(0)), 3.0); 179 317 } 180 318 181 319 if (node.Symbol is CubeRoot) { 182 320 return tf.pow( 183 ConvertNode(node.GetSubtree(0)), 1.0f / 3.0f); 321 EvaluateNode(node.GetSubtree(0)), 1.0f / 3.0f); 322 //ConvertNode(node.GetSubtree(0)), 1.0 / 3.0); 184 323 // TODO 185 324 // f: x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3), … … 189 328 if (node.Symbol is Sine) { 190 329 return tf.sin( 191 ConvertNode(node.GetSubtree(0)));330 EvaluateNode(node.GetSubtree(0))); 192 331 } 193 332 194 333 if (node.Symbol is Cosine) { 195 334 return tf.cos( 196 ConvertNode(node.GetSubtree(0)));335 EvaluateNode(node.GetSubtree(0))); 197 336 } 198 337 199 338 if (node.Symbol is Tangent) { 200 339 return tf.tan( 201 ConvertNode(node.GetSubtree(0)));340 EvaluateNode(node.GetSubtree(0))); 202 341 } 203 342 204 343 if (node.Symbol is Mean) { 205 344 return tf.reduce_mean( 206 ConvertNode(node.GetSubtree(0)),345 EvaluateNode(node.GetSubtree(0)), 207 346 axis: new[] { 1 }, 208 347 keepdims: true); … … 210 349 211 350 if (node.Symbol is StandardDeviation) { 212 return reduce_std(213 ConvertNode(node.GetSubtree(0)),351 return tf.reduce_std( 352 EvaluateNode(node.GetSubtree(0)), 214 353 axis: new[] { 1 }, 215 354 keepdims: true 216 );355 ); 217 356 } 218 357 219 358 if (node.Symbol is Variance) { 220 return reduce_var(221 ConvertNode(node.GetSubtree(0)),359 return tf.reduce_variance( 360 EvaluateNode(node.GetSubtree(0)), 222 361 axis: new[] { 1 } , 223 362 keepdims: true … … 227 366 if (node.Symbol is Sum) { 228 367 return tf.reduce_sum( 229 ConvertNode(node.GetSubtree(0)),368 EvaluateNode(node.GetSubtree(0)), 230 369 axis: new[] { 1 }, 231 370 keepdims: true); … … 233 372 234 373 if (node.Symbol is SubVector) { 235 var tensor = ConvertNode(node.GetSubtree(0));236 int rows = tensor.shape[0], vectorLength =tensor.shape[1];374 var tensor = EvaluateNode(node.GetSubtree(0)); 375 int rows = (int)tensor.shape[0], vectorLength = (int)tensor.shape[1]; 237 376 var windowedNode = (IWindowedSymbolTreeNode)node; 238 377 int startIdx = SymbolicDataAnalysisExpressionTreeVectorInterpreter.ToVectorIdx(windowedNode.Offset, vectorLength); … … 244 383 segments.Add(tensor[new Slice(), new Slice(start, start + count)]); 245 384 } 246 return tf.concat(segments .ToArray(), axis: 1);385 return tf.concat(segments, axis: 1); 247 386 } 248 387 249 388 250 389 if (node.Symbol is StartSymbol) { 251 Tensor prediction; 390 Tensor prediction = EvaluateNode(node.GetSubtree(0)); 391 392 if (prediction.rank != 2 && prediction.shape[1] != 1) 393 throw new InvalidOperationException("Prediction must be a rank 1 (single value per row)."); 394 395 prediction = tf.reshape(prediction, new Shape(-1)); 396 252 397 if (addLinearScalingTerms) { 253 // scaling variables α, β are given at the beginning of the parameter vector 254 var alpha_arr = np.array(1.0f).reshape(1, 1); 255 var alpha = tf.Variable(alpha_arr, name: "alpha", dtype: DataType); 256 var beta_arr = np.array(0.0f).reshape(1, 1); 257 var beta = tf.Variable(beta_arr, name: "beta", dtype: DataType); 258 variables.Add(beta); 259 variables.Add(alpha); 260 var t = ConvertNode(node.GetSubtree(0)); 261 prediction = t * alpha + beta; 398 var vars = variables[node]; 399 Tensor alpha = vars[1], beta = vars[0]; 400 return prediction * alpha + beta; 262 401 } else { 263 prediction = ConvertNode(node.GetSubtree(0)); 264 } 265 266 return tf.reshape(prediction, shape: new[] { -1 }); 402 return prediction; 403 } 267 404 } 268 405 269 406 throw new NotSupportedException($"Node symbol {node.Symbol} is not supported."); 270 }271 272 private static Tensor reduce_var(Tensor input_tensor, int[] axis = null, bool keepdims = false) {273 var means = tf.reduce_mean(input_tensor, axis, true);274 var squared_deviation = tf.square(input_tensor - means);275 return tf.reduce_mean(squared_deviation, axis, keepdims);276 }277 private static Tensor reduce_std(Tensor input_tensor, int[] axis = null, bool keepdims = false) {278 return tf.sqrt(reduce_var(input_tensor, axis, keepdims));279 407 } 280 408 -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r18229 r18239 113 113 <Private>False</Private> 114 114 </Reference> 115 <Reference Include="DiffSharp.Merged, Version=0.8.4.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=AMD64">116 <SpecificVersion>False</SpecificVersion>117 <HintPath>..\..\bin\DiffSharp.Merged.dll</HintPath>118 </Reference>119 115 <Reference Include="HEAL.Attic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 120 116 <SpecificVersion>False</SpecificVersion> … … 144 140 <Reference Include="System.Data" /> 145 141 <Reference Include="System.Xml" /> 146 <Reference Include="Tensor Flow.NET.Merged, Version=0.15.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">142 <Reference Include="Tensorflow.Binding, Version=0.70.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=AMD64"> 147 143 <SpecificVersion>False</SpecificVersion> 148 <HintPath>..\..\bin\Tensor Flow.NET.Merged.dll</HintPath>144 <HintPath>..\..\bin\Tensorflow.Binding.dll</HintPath> 149 145 </Reference> 150 146 </ItemGroup> -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Plugin.cs.frame
r18229 r18239 56 56 [PluginDependency("HeuristicLab.MathNet.Numerics", "4.9.0")] 57 57 [PluginDependency("HeuristicLab.TensorFlowNet", "0.15.0")] 58 [PluginDependency("HeuristicLab.DiffSharp", "0.7.7")]58 //[PluginDependency("HeuristicLab.DiffSharp", "0.7.7")] 59 59 public class HeuristicLabProblemsDataAnalysisSymbolicPlugin : PluginBase { 60 60 }
Note: See TracChangeset
for help on using the changeset viewer.