Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18239


Ignore:
Timestamp:
03/22/22 13:28:56 (3 years ago)
Author:
pfleck
Message:

#3040 Updated to newer TensorFlow.NET version.

  • Removed IL Merge from TensorFlow.NET.
  • Temporarily removed DiffSharp.
  • Changed to a locally built Attic with a specific Protobuf version that is compatible with TensorFlow.NET. (Also adapted other versions of nuget dependencies.)
Location:
branches/3040_VectorBasedGP
Files:
1 deleted
16 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.ExtLibs.sln

    r17930 r18239  
    8585Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.MathNet.Numerics", "HeuristicLab.MathNet.Numerics\HeuristicLab.MathNet.Numerics.csproj", "{71AE8AE6-F1A5-445C-AD31-AFFC83CE2E28}"
    8686EndProject
    87 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlowNetILMerge", "HeuristicLab.ExtLibs\TensorFlowNet\TensorFlowNetILMerge.csproj", "{99BF44AE-436E-4B30-9435-E2BA98EE6FB3}"
    88 EndProject
    8987Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.TensorFlowNet", "HeuristicLab.ExtLibs\HeuristicLab.TensorFlowNet\HeuristicLab.TensorFlowNet.csproj", "{C2E26343-88C7-4288-938B-EEE7C2ABBDB6}"
    90   ProjectSection(ProjectDependencies) = postProject
    91     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3} = {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}
    92   EndProjectSection
    93 EndProject
    94 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiffSharpILMerge", "HeuristicLab.ExtLibs\DiffSharp\DiffSharpILMerge.csproj", "{0C9D4608-1B14-404E-9932-F60E67B625BA}"
    95 EndProject
    96 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.DiffSharp", "HeuristicLab.ExtLibs\HeuristicLab.DiffSharp\HeuristicLab.DiffSharp.csproj", "{99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}"
    97   ProjectSection(ProjectDependencies) = postProject
    98     {0C9D4608-1B14-404E-9932-F60E67B625BA} = {0C9D4608-1B14-404E-9932-F60E67B625BA}
    99   EndProjectSection
    10088EndProject
    10189Global
     
    561549    {71AE8AE6-F1A5-445C-AD31-AFFC83CE2E28}.Release|x86.ActiveCfg = Release|Any CPU
    562550    {71AE8AE6-F1A5-445C-AD31-AFFC83CE2E28}.Release|x86.Build.0 = Release|Any CPU
    563     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    564     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
    565     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x64.ActiveCfg = Debug|x64
    566     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x64.Build.0 = Debug|x64
    567     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x86.ActiveCfg = Debug|Any CPU
    568     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Debug|x86.Build.0 = Debug|Any CPU
    569     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
    570     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|Any CPU.Build.0 = Release|Any CPU
    571     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x64.ActiveCfg = Release|x64
    572     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x64.Build.0 = Release|x64
    573     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x86.ActiveCfg = Release|Any CPU
    574     {99BF44AE-436E-4B30-9435-E2BA98EE6FB3}.Release|x86.Build.0 = Release|Any CPU
    575551    {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    576552    {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
     
    585561    {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Release|x86.ActiveCfg = Release|Any CPU
    586562    {C2E26343-88C7-4288-938B-EEE7C2ABBDB6}.Release|x86.Build.0 = Release|Any CPU
    587     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|Any CPU.ActiveCfg = Debug|x64
    588     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|x64.ActiveCfg = Debug|x64
    589     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|x64.Build.0 = Debug|x64
    590     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Debug|x86.ActiveCfg = Debug|x64
    591     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|Any CPU.ActiveCfg = Release|x64
    592     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|x64.ActiveCfg = Release|x64
    593     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|x64.Build.0 = Release|x64
    594     {0C9D4608-1B14-404E-9932-F60E67B625BA}.Release|x86.ActiveCfg = Release|x64
    595     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|Any CPU.ActiveCfg = Debug|x64
    596     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|x64.ActiveCfg = Debug|x64
    597     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|x64.Build.0 = Debug|x64
    598     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Debug|x86.ActiveCfg = Debug|x64
    599     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|Any CPU.ActiveCfg = Release|x64
    600     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|x64.ActiveCfg = Release|x64
    601     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|x64.Build.0 = Release|x64
    602     {99935EB6-C98F-4524-A8E2-94C8F6A0C8D9}.Release|x86.ActiveCfg = Release|x64
    603563  EndGlobalSection
    604564  GlobalSection(SolutionProperties) = preSolution
  • branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.Attic/1.0.0/HeuristicLab.Attic.csproj

    r17935 r18239  
    5454  </PropertyGroup>
    5555  <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>
    5660    <Reference Include="System" />
    5761    <Reference Include="System.Core" />
     
    7882  </ItemGroup>
    7983  <ItemGroup>
    80     <PackageReference Include="HEAL.Attic">
    81       <Version>1.5.0</Version>
    82     </PackageReference>
    83   </ItemGroup>
    84   <ItemGroup>
    8584    <ProjectReference Include="..\..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    8685      <Project>{94186a6a-5176-4402-ae83-886557b53cca}</Project>
     
    9291      <Name>HeuristicLab.Protobuf</Name>
    9392    </ProjectReference>
     93  </ItemGroup>
     94  <ItemGroup>
     95    <PackageReference Include="System.Runtime.CompilerServices.Unsafe">
     96      <Version>6.0.0</Version>
     97    </PackageReference>
    9498  </ItemGroup>
    9599  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.Protobuf/3.6.1/HeuristicLab.Protobuf.csproj

    r17930 r18239  
    6161  <ItemGroup>
    6262    <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>
    6467    </PackageReference>
    6568  </ItemGroup>
  • branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.TensorFlowNet/HeuristicLab.TensorFlowNet.csproj

    r17930 r18239  
    1010    <RootNamespace>HeuristicLab.TensorFlowNet</RootNamespace>
    1111    <AssemblyName>HeuristicLab.TensorFlowNet</AssemblyName>
    12     <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
     12    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    1313    <FileAlignment>512</FileAlignment>
    1414    <Deterministic>true</Deterministic>
     15    <TargetFrameworkProfile />
    1516  </PropertyGroup>
    1617  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    5556    <Reference Include="System" />
    5657    <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>
    6158  </ItemGroup>
    6259  <ItemGroup>
     
    7370  </ItemGroup>
    7471  <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>
    7881  </ItemGroup>
    7982  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/HeuristicLab.TensorFlowNet/Plugin.cs.frame

    r17935 r18239  
    2626  [Plugin("HeuristicLab.TensorFlowNet", "HeuristicLab wrapper for TensofFlow.NET nuget package.", "0.15.0.$WCREV$")]
    2727  [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)]
    2933  [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")]
    3235  public class Plugin : PluginBase {
    3336    public Plugin() { }
  • branches/3040_VectorBasedGP/HeuristicLab.ExtLibs/TensorFlowNet/TensorFlowNetILMerge.csproj

    r17930 r18239  
    1010    <RootNamespace>TensorFlowNetILMerge</RootNamespace>
    1111    <AssemblyName>TensorFlowNetILMerge_</AssemblyName>
    12     <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
     12    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    1313    <FileAlignment>512</FileAlignment>
    1414    <Deterministic>true</Deterministic>
     15    <TargetFrameworkProfile />
    1516  </PropertyGroup>
    1617  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    5758  </ItemGroup>
    5859  <ItemGroup>
    59     <PackageReference Include="Google.Protobuf">
    60       <Version>3.6.1</Version>
    61     </PackageReference>
    6260    <PackageReference Include="ilmerge">
    6361      <Version>3.0.41</Version>
    6462    </PackageReference>
    6563    <PackageReference Include="TensorFlow.NET">
    66       <Version>0.15.0</Version>
     64      <Version>0.70.1</Version>
    6765    </PackageReference>
    6866  </ItemGroup>
    6967  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    7068  <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>
    7871  </PropertyGroup>
    7972  <PropertyGroup>
  • branches/3040_VectorBasedGP/HeuristicLab.MathNet.Numerics/HeuristicLab.MathNet.Numerics.csproj

    r17930 r18239  
    5656  </PropertyGroup>
    5757  <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">
    5959      <SpecificVersion>False</SpecificVersion>
    6060      <HintPath>..\bin\HEAL.Attic.dll</HintPath>
     
    7373  </ItemGroup>
    7474  <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>
    7975    <ProjectReference Include="..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    8076      <Project>{94186a6a-5176-4402-ae83-886557b53cca}</Project>
     
    8379  </ItemGroup>
    8480  <ItemGroup>
     81    <PackageReference Include="Google.Protobuf">
     82      <Version>3.11.3</Version>
     83    </PackageReference>
    8584    <PackageReference Include="MathNet.Numerics.Signed">
    8685      <Version>4.9.0</Version>
     86    </PackageReference>
     87    <PackageReference Include="System.Runtime.CompilerServices.Unsafe">
     88      <Version>6.0.0</Version>
    8789    </PackageReference>
    8890  </ItemGroup>
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r18238 r18239  
    4747      nudLearningRate.Enabled = tree != null && TensorFlowConstantOptimizationEvaluator.CanOptimizeConstants(tree);
    4848      btnUnrollingVectorOptimizeConstants.Enabled = tree != null && VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.CanOptimizeConstants(tree);
     49#if INCLUDE_DIFFSHARP
    4950      btnDiffSharpOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresVectorConstantOptimizationEvaluator.CanOptimizeConstants(tree);
     51#endif
    5052    }
    5153
     
    9597      return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData,
    9698        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,
    98101        cancellationToken: cancellationToken,
    99102        progress: new SynchronousProgress<double>(cost => {
     
    121124        tree = VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(
    122125          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,
    125130          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
    126131            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
     
    150155      int reps = 0;
    151156
     157#if INCLUDE_DIFFSHARP
    152158      do {
    153159        prevResult = result;
     
    166172               progress.ProgressState != ProgressState.StopRequested &&
    167173               progress.ProgressState != ProgressState.CancelRequested);
     174#endif
    168175      return tree;
    169176    }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r17930 r18239  
    4747    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    4848    <Prefer32Bit>false</Prefer32Bit>
    49     <LangVersion>7</LangVersion>
     49    <LangVersion>latest</LangVersion>
    5050  </PropertyGroup>
    5151  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     
    5858    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    5959    <Prefer32Bit>false</Prefer32Bit>
    60     <LangVersion>7</LangVersion>
     60    <LangVersion>latest</LangVersion>
    6161  </PropertyGroup>
    6262  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
     
    6969    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    7070    <Prefer32Bit>false</Prefer32Bit>
    71     <LangVersion>7</LangVersion>
     71    <LangVersion>latest</LangVersion>
    7272  </PropertyGroup>
    7373  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
     
    8080    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    8181    <Prefer32Bit>false</Prefer32Bit>
    82     <LangVersion>7</LangVersion>
     82    <LangVersion>latest</LangVersion>
    8383  </PropertyGroup>
    8484  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
     
    9191    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    9292    <Prefer32Bit>false</Prefer32Bit>
    93     <LangVersion>7</LangVersion>
     93    <LangVersion>latest</LangVersion>
    9494  </PropertyGroup>
    9595  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
     
    102102    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    103103    <Prefer32Bit>false</Prefer32Bit>
    104     <LangVersion>7</LangVersion>
     104    <LangVersion>latest</LangVersion>
    105105  </PropertyGroup>
    106106  <ItemGroup>
     
    109109      <HintPath>..\..\bin\ALGLIB-3.7.0.dll</HintPath>
    110110      <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>
    115111    </Reference>
    116112    <Reference Include="MathNet.Numerics">
     
    133129    <Reference Include="System.Data" />
    134130    <Reference Include="System.Xml" />
    135     <Reference Include="TensorFlow.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">
    136132      <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>
    138138    </Reference>
    139139  </ItemGroup>
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Plugin.cs.frame

    r17786 r18239  
    4343  [PluginDependency("HeuristicLab.MathNet.Numerics", "4.9.0")]
    4444  [PluginDependency("HeuristicLab.TensorFlowNet", "0.15.0")]
    45   [PluginDependency("HeuristicLab.DiffSharp", "0.7.7")]
     45  //[PluginDependency("HeuristicLab.DiffSharp", "0.7.7")]
    4646  public class HeuristicLabProblemsDataAnalysisSymbolicRegressionPlugin : PluginBase {
    4747  }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NonlinearLeastSquaresVectorConstantOptimizationEvaluator.cs

    r17930 r18239  
    1919 */
    2020#endregion
     21
     22#if INCLUDE_DIFFSHARP
    2123
    2224using System;
     
    194196  }
    195197}
     198
     199#endif
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/TensorFlowConstantOptimizationEvaluator.cs

    r17721 r18239  
    2525
    2626using System;
    27 using System.Collections;
    2827using System.Collections.Generic;
    2928#if LOG_CONSOLE
     
    4241using HeuristicLab.Parameters;
    4342using HEAL.Attic;
    44 using NumSharp;
    4543using Tensorflow;
     44using Tensorflow.NumPy;
    4645using static Tensorflow.Binding;
     46using static Tensorflow.KerasApi;
    4747using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
    4848
     
    5454    private const string LearningRateName = "LearningRate";
    5555
     56    //private static readonly TF_DataType DataType = tf.float64;
    5657    private static readonly TF_DataType DataType = tf.float32;
    5758
     
    105106      CancellationToken cancellationToken = default(CancellationToken), IProgress<double> progress = null) {
    106107
    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(),
    118113        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)
    123118        return (ISymbolicExpressionTree)tree.Clone();
    124119
    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));
    152144      }
    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];
    205372            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;
    218381          }
    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
    238         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);
    259382        }
    260383      }
    261384    }
    262385
     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
    263400    public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
    264401      return TreeToTensorConverter.IsCompatible(tree);
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToDiffSharpConverter.cs

    r17930 r18239  
    1919 */
    2020#endregion
     21
     22#if INCLUDE_DIFFSHARP
    2123
    2224using System;
     
    505507  }
    506508}
     509
     510#endif
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToTensorConverter.cs

    r18238 r18239  
    2020#endregion
    2121
    22 #define EXPLICIT_SHAPE
    23 
    2422using System;
    2523using System.Collections.Generic;
    2624using System.Linq;
    2725using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    28 using NumSharp;
    2926using Tensorflow;
     27using Tensorflow.NumPy;
    3028using static Tensorflow.Binding;
     29using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
    3130
    3231namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3332  public class TreeToTensorConverter {
    3433
     34    //private static readonly TF_DataType DataType = tf.float64;
    3535    private static readonly TF_DataType DataType = tf.float32;
    3636
    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) {
    4044
    4145      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);
    4661        variables = converter.variables;
     62
    4763        return true;
    4864      } catch (NotSupportedException) {
    49         graph = null;
    50         parameters = null;
     65        inputFeatures = null;
     66        target= null;
    5167        variables = null;
    5268        return false;
    5369      }
    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
    58131    private readonly bool makeVariableWeightsVariable;
    59132    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
    67143      this.makeVariableWeightsVariable = makeVariableWeightsVariable;
    68144      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) {
    73218      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];
    79220      }
    80221
    81222      if (node.Symbol is Variable/* || node.Symbol is BinaryFactorVariable*/) {
    82223        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
    90226        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];
    94228          return w * par;
    95229        } else {
     
    119253
    120254      if (node.Symbol is Addition) {
    121         var terms = node.Subtrees.Select(ConvertNode).ToList();
     255        var terms = node.Subtrees.Select(EvaluateNode).ToList();
    122256        if (terms.Count == 1) return terms[0];
    123257        return terms.Aggregate((a, b) => a + b);
     
    125259
    126260      if (node.Symbol is Subtraction) {
    127         var terms = node.Subtrees.Select(ConvertNode).ToList();
     261        var terms = node.Subtrees.Select(EvaluateNode).ToList();
    128262        if (terms.Count == 1) return -terms[0];
    129263        return terms.Aggregate((a, b) => a - b);
     
    131265
    132266      if (node.Symbol is Multiplication) {
    133         var terms = node.Subtrees.Select(ConvertNode).ToList();
     267        var terms = node.Subtrees.Select(EvaluateNode).ToList();
    134268        if (terms.Count == 1) return terms[0];
    135269        return terms.Aggregate((a, b) => a * b);
     
    137271
    138272      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];
    141276        return terms.Aggregate((a, b) => a / b);
    142277      }
    143278
    144279      if (node.Symbol is Absolute) {
    145         var x1 = ConvertNode(node.GetSubtree(0));
     280        var x1 = EvaluateNode(node.GetSubtree(0));
    146281        return tf.abs(x1);
    147282      }
    148283
    149284      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));
    152287        return x1 / tf.pow(1.0f + x2 * x2, 0.5f);
     288        //return x1 / tf.pow(1.0 + x2 * x2, 0.5);
    153289      }
    154290
    155291      if (node.Symbol is Logarithm) {
    156292        return tf.log(
    157           ConvertNode(node.GetSubtree(0)));
     293          EvaluateNode(node.GetSubtree(0)));
    158294      }
    159295
     
    161297        return tf.pow(
    162298          (float)Math.E,
    163           ConvertNode(node.GetSubtree(0)));
     299          //Math.E,
     300          EvaluateNode(node.GetSubtree(0)));
    164301      }
    165302
    166303      if (node.Symbol is Square) {
    167304        return tf.square(
    168           ConvertNode(node.GetSubtree(0)));
     305          EvaluateNode(node.GetSubtree(0)));
    169306      }
    170307
    171308      if (node.Symbol is SquareRoot) {
    172309        return tf.sqrt(
    173           ConvertNode(node.GetSubtree(0)));
     310          EvaluateNode(node.GetSubtree(0)));
    174311      }
    175312
    176313      if (node.Symbol is Cube) {
    177314        return tf.pow(
    178           ConvertNode(node.GetSubtree(0)), 3.0f);
     315          EvaluateNode(node.GetSubtree(0)), 3.0f);
     316        //ConvertNode(node.GetSubtree(0)), 3.0);
    179317      }
    180318
    181319      if (node.Symbol is CubeRoot) {
    182320        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);
    184323        // TODO
    185324        // f: x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3),
     
    189328      if (node.Symbol is Sine) {
    190329        return tf.sin(
    191           ConvertNode(node.GetSubtree(0)));
     330          EvaluateNode(node.GetSubtree(0)));
    192331      }
    193332
    194333      if (node.Symbol is Cosine) {
    195334        return tf.cos(
    196           ConvertNode(node.GetSubtree(0)));
     335          EvaluateNode(node.GetSubtree(0)));
    197336      }
    198337
    199338      if (node.Symbol is Tangent) {
    200339        return tf.tan(
    201           ConvertNode(node.GetSubtree(0)));
     340          EvaluateNode(node.GetSubtree(0)));
    202341      }
    203342
    204343      if (node.Symbol is Mean) {
    205344        return tf.reduce_mean(
    206           ConvertNode(node.GetSubtree(0)),
     345          EvaluateNode(node.GetSubtree(0)),
    207346          axis: new[] { 1 },
    208347          keepdims: true);
     
    210349
    211350      if (node.Symbol is StandardDeviation) {
    212         return reduce_std(
    213           ConvertNode(node.GetSubtree(0)),
     351        return tf.reduce_std(
     352          EvaluateNode(node.GetSubtree(0)),
    214353          axis: new[] { 1 },
    215354          keepdims: true
    216        );
     355        );
    217356      }
    218357
    219358      if (node.Symbol is Variance) {
    220         return reduce_var(
    221           ConvertNode(node.GetSubtree(0)),
     359        return tf.reduce_variance(
     360          EvaluateNode(node.GetSubtree(0)),
    222361          axis: new[] { 1 } ,
    223362          keepdims: true
     
    227366      if (node.Symbol is Sum) {
    228367        return tf.reduce_sum(
    229           ConvertNode(node.GetSubtree(0)),
     368          EvaluateNode(node.GetSubtree(0)),
    230369          axis: new[] { 1 },
    231370          keepdims: true);
     
    233372
    234373      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];
    237376        var windowedNode = (IWindowedSymbolTreeNode)node;
    238377        int startIdx = SymbolicDataAnalysisExpressionTreeVectorInterpreter.ToVectorIdx(windowedNode.Offset, vectorLength);
     
    244383          segments.Add(tensor[new Slice(), new Slice(start, start + count)]);
    245384        }
    246         return tf.concat(segments.ToArray(), axis: 1);
     385        return tf.concat(segments, axis: 1);
    247386      }
    248387
    249388
    250389      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
    252397        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;
    262401        } else {
    263           prediction = ConvertNode(node.GetSubtree(0));
    264         }
    265 
    266         return tf.reshape(prediction, shape: new[] { -1 });
     402          return prediction;
     403        }
    267404      }
    268405
    269406      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));
    279407    }
    280408
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r18229 r18239  
    113113      <Private>False</Private>
    114114    </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>
    119115    <Reference Include="HEAL.Attic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    120116      <SpecificVersion>False</SpecificVersion>
     
    144140    <Reference Include="System.Data" />
    145141    <Reference Include="System.Xml" />
    146     <Reference Include="TensorFlow.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">
    147143      <SpecificVersion>False</SpecificVersion>
    148       <HintPath>..\..\bin\TensorFlow.NET.Merged.dll</HintPath>
     144      <HintPath>..\..\bin\Tensorflow.Binding.dll</HintPath>
    149145    </Reference>
    150146  </ItemGroup>
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Plugin.cs.frame

    r18229 r18239  
    5656  [PluginDependency("HeuristicLab.MathNet.Numerics", "4.9.0")]
    5757  [PluginDependency("HeuristicLab.TensorFlowNet", "0.15.0")]
    58   [PluginDependency("HeuristicLab.DiffSharp", "0.7.7")]
     58  //[PluginDependency("HeuristicLab.DiffSharp", "0.7.7")]
    5959  public class HeuristicLabProblemsDataAnalysisSymbolicPlugin : PluginBase {
    6060  }
Note: See TracChangeset for help on using the changeset viewer.