Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8425


Ignore:
Timestamp:
08/07/12 15:14:34 (12 years ago)
Author:
abeham
Message:

#1913: Added several initialization methods (LDA, PCA, and Random)

Location:
branches/NCA
Files:
6 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/NCA

    • Property svn:ignore
      •  

        old new  
        22*.user
        33TestResults
         4_ReSharper.NCA
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/HeuristicLab.Algorithms.NCA-3.3.csproj

    r8420 r8425  
    8989      <Private>False</Private>
    9090    </Reference>
     91    <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    9192    <Reference Include="System" />
    9293    <Reference Include="System.Core" />
     
    9899  </ItemGroup>
    99100  <ItemGroup>
     101    <Compile Include="Initialization\INCAInitializer.cs" />
    100102    <Compile Include="INCAModel.cs" />
     103    <Compile Include="Initialization\LDAInitializer.cs" />
     104    <Compile Include="Initialization\PCAInitializer.cs" />
     105    <Compile Include="Initialization\RandomInitializer.cs" />
    101106    <Compile Include="Matrix.cs" />
    102107    <Compile Include="NCA.cs" />
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCA.cs

    r8412 r8425  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Algorithms.DataAnalysis;
    2324using HeuristicLab.Common;
     
    2728using HeuristicLab.Parameters;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.PluginInfrastructure;
    2931using HeuristicLab.Problems.DataAnalysis;
    3032
     
    3436  /// </summary>
    3537  [Item("NCA", "Neihborhood Components Analysis is described in J. Goldberger, S. Roweis, G. Hinton, R. Salakhutdinov. 2005. Neighbourhood Component Analysis. Advances in Neural Information Processing Systems, 17. pp. 513-520.")]
    36   [Creatable("Algorithms")]
     38  [Creatable("Data Analysis")]
    3739  [StorableClass]
    3840  public sealed class NCA : FixedDataAnalysisAlgorithm<IClassificationProblem> {
     
    4345    public IValueLookupParameter<IntValue> ReduceDimensionsParameter {
    4446      get { return (IValueLookupParameter<IntValue>)Parameters["ReduceDimensions"]; }
     47    }
     48    private IConstrainedValueParameter<INCAInitializer> InitializationParameter {
     49      get { return (IConstrainedValueParameter<INCAInitializer>)Parameters["Initialization"]; }
    4550    }
    4651    #endregion
     
    6267      Parameters.Add(new ValueLookupParameter<IntValue>("k", "The k for the nearest neighbor.", new IntValue(1)));
    6368      Parameters.Add(new ValueLookupParameter<IntValue>("ReduceDimensions", "The number of dimensions that NCA should reduce the data to.", new IntValue(2)));
     69      Parameters.Add(new ConstrainedValueParameter<INCAInitializer>("Initialization", "Which method should be used to initialize the matrix. Typically LDA (linear discriminant analysis) should provide a good estimate."));
     70
     71      INCAInitializer defaultInitializer = null;
     72      foreach (var initializer in ApplicationManager.Manager.GetInstances<INCAInitializer>().OrderBy(x => x.ItemName)) {
     73        if (initializer is LDAInitializer) defaultInitializer = initializer;
     74        InitializationParameter.ValidValues.Add(initializer);
     75      }
     76      if (defaultInitializer != null) InitializationParameter.Value = defaultInitializer;
    6477
    6578      Problem = new ClassificationProblem();
     
    7588
    7689    protected override void Run() {
    77       var classification = NeighborhoodComponentsAnalysis.CreateNCASolution(Problem.ProblemData, K.Value, ReduceDimensions.Value);
     90      var classification = NeighborhoodComponentsAnalysis.CreateNCASolution(Problem.ProblemData, K.Value, ReduceDimensions.Value, InitializationParameter.Value);
    7891      Results.Add(new Result("ClassificationSolution", "The classification solution.", classification));
    7992    }
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NeighborhoodComponentsAnalysis.cs

    r8422 r8425  
    2929  public class NeighborhoodComponentsAnalysis {
    3030
    31     public static INCAModel Train(IClassificationProblemData data, int k, int reduceDimensions) {
     31    public static INCAModel Train(IClassificationProblemData data, int k, int reduceDimensions, INCAInitializer initializer) {
    3232      var instances = data.TrainingIndices.Count();
    3333      var attributes = data.AllowedInputVariables.Count();
    3434
    35       /*alglib.pcabuildbasis(
    36     double[,] x,
    37     int npoints,
    38     int nvars,
    39     out int info,
    40     out double[] s2,
    41     out double[,] v)*/
    42 
    43       var matrix = new double[attributes * reduceDimensions];
    44 
    45       // TODO: make some more clever initialization of matrix
    46       var rand = new Random();
    47       var counter = 0;
    48       for (var i = 0; i < attributes; i++)
    49         for (var j = 0; j < reduceDimensions; j++)
    50           matrix[counter++] = rand.NextDouble();
     35      double[] matrix = initializer.Initialize(data, reduceDimensions);
    5136
    5237      alglib.mincgstate state;
     
    6045
    6146      var transformationMatrix = new double[attributes, reduceDimensions];
    62       counter = 0;
     47      var counter = 0;
    6348      for (var i = 0; i < attributes; i++)
    6449        for (var j = 0; j < reduceDimensions; j++)
     
    143128    }
    144129
    145     public static NCAClassificationSolution CreateNCASolution(IClassificationProblemData problemData, int k, int reduceDimensions) {
    146       return new NCAClassificationSolution(problemData, Train(problemData, k, reduceDimensions));
     130    public static NCAClassificationSolution CreateNCASolution(IClassificationProblemData problemData, int k, int reduceDimensions, INCAInitializer initializer) {
     131      return new NCAClassificationSolution(problemData, Train(problemData, k, reduceDimensions, initializer));
    147132    }
    148133
Note: See TracChangeset for help on using the changeset viewer.