Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/01/13 18:32:26 (12 years ago)
Author:
abeham
Message:

#1913: Changed NCA to use LM-BFGS optimization algorithm, added model/solution creators, added operator for gradient calculation

Location:
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/INcaInitializer.cs

    r8471 r9270  
    2424
    2525namespace HeuristicLab.Algorithms.DataAnalysis {
    26   public interface INCAInitializer : IItem {
     26  public interface INcaInitializer : IOperator {
    2727    /// <summary>
    2828    /// Calculates an initial projection for the NCA to start from.
     
    3030    /// <param name="data">The problem data that contains the AllowedInputVariables and TrainingIndices.</param>
    3131    /// <param name="dimensions">The amount of columns in the matrix</param>
    32     /// <returns>A flat representation of a matrix that is read row-wise and contains AllowedInputVariables * TrainingIndices numbers.</returns>
    33     double[] Initialize(IClassificationProblemData data, int dimensions);
     32    /// <returns>The matrix that projects the input variables into a lower dimensional space.</returns>
     33    double[,] Initialize(IClassificationProblemData data, Scaling scaling, int dimensions);
    3434  }
    3535}
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/LdaInitializer.cs

    r8471 r9270  
    2020#endregion
    2121
    22 using System.Collections.Generic;
    2322using System.Linq;
    2423using HeuristicLab.Common;
     
    3029  [Item("LDA", "Initializes the matrix by performing a linear discriminant analysis.")]
    3130  [StorableClass]
    32   public class LDAInitializer : Item, INCAInitializer {
     31  public class LdaInitializer : NcaInitializer {
    3332
    3433    [StorableConstructor]
    35     protected LDAInitializer(bool deserializing) : base(deserializing) { }
    36     protected LDAInitializer(LDAInitializer original, Cloner cloner) : base(original, cloner) { }
    37     public LDAInitializer() : base() { }
     34    protected LdaInitializer(bool deserializing) : base(deserializing) { }
     35    protected LdaInitializer(LdaInitializer original, Cloner cloner) : base(original, cloner) { }
     36    public LdaInitializer() : base() { }
    3837
    3938    public override IDeepCloneable Clone(Cloner cloner) {
    40       return new LDAInitializer(this, cloner);
     39      return new LdaInitializer(this, cloner);
    4140    }
    4241
    43     public double[] Initialize(IClassificationProblemData data, int dimensions) {
     42    public override double[,] Initialize(IClassificationProblemData data, Scaling scaling, int dimensions) {
    4443      var instances = data.TrainingIndices.Count();
    4544      var attributes = data.AllowedInputVariables.Count();
    4645
    4746      var ldaDs = new double[instances, attributes + 1];
    48       int row, col = 0;
    49       foreach (var variable in data.AllowedInputVariables) {
    50         row = 0;
    51         foreach (var value in data.Dataset.GetDoubleValues(variable, data.TrainingIndices)) {
    52           ldaDs[row, col] = value;
    53           row++;
     47      int j = 0;
     48      foreach (var a in data.AllowedInputVariables) {
     49        int i = 0;
     50        var sv = scaling.GetScaledValues(data.Dataset, a, data.TrainingIndices);
     51        foreach (var v in sv) {
     52          ldaDs[i++, j] = v;
    5453        }
    55         col++;
     54        j++;
    5655      }
    57       row = 0;
    58       var uniqueClasses = new Dictionary<double, int>();
    59       foreach (var label in data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices)) {
    60         if (!uniqueClasses.ContainsKey(label))
    61           uniqueClasses[label] = uniqueClasses.Count;
    62         ldaDs[row++, attributes] = label;
    63       }
    64       for (row = 0; row < instances; row++)
    65         ldaDs[row, attributes] = uniqueClasses[ldaDs[row, attributes]];
     56      j = 0;
     57      foreach (var tv in data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices))
     58        ldaDs[j++, attributes] = tv;
     59
     60      var uniqueClasses = data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices).Distinct().Count();
    6661
    6762      int info;
    6863      double[,] matrix;
    69       alglib.fisherldan(ldaDs, instances, attributes, uniqueClasses.Count, out info, out matrix);
     64      alglib.fisherldan(ldaDs, instances, attributes, uniqueClasses, out info, out matrix);
    7065
    71       var result = new double[attributes * dimensions];
    72       for (int i = 0; i < attributes; i++)
    73         for (int j = 0; j < dimensions; j++)
    74           result[i * dimensions + j] = matrix[i, j];
    75 
    76       return result;
     66      return matrix;
    7767    }
    7868
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/PcaInitializer.cs

    r8471 r9270  
    2929  [Item("PCA", "Initializes the matrix by performing a principal components analysis.")]
    3030  [StorableClass]
    31   public sealed class PCAInitializer : Item, INCAInitializer {
     31  public sealed class PcaInitializer : NcaInitializer {
    3232
    3333    [StorableConstructor]
    34     private PCAInitializer(bool deserializing) : base(deserializing) { }
    35     private PCAInitializer(PCAInitializer original, Cloner cloner) : base(original, cloner) { }
    36     public PCAInitializer() : base() { }
     34    private PcaInitializer(bool deserializing) : base(deserializing) { }
     35    private PcaInitializer(PcaInitializer original, Cloner cloner) : base(original, cloner) { }
     36    public PcaInitializer() : base() { }
    3737
    3838    public override IDeepCloneable Clone(Cloner cloner) {
    39       return new PCAInitializer(this, cloner);
     39      return new PcaInitializer(this, cloner);
    4040    }
    4141
    42     public double[] Initialize(IClassificationProblemData data, int dimensions) {
     42    public override double[,] Initialize(IClassificationProblemData data, Scaling scaling, int dimensions) {
    4343      var instances = data.TrainingIndices.Count();
    4444      var attributes = data.AllowedInputVariables.Count();
    4545
    46       var pcaDs = new double[instances, attributes];
    47       int col = 0;
    48       foreach (var variable in data.AllowedInputVariables) {
    49         int row = 0;
    50         foreach (var value in data.Dataset.GetDoubleValues(variable, data.TrainingIndices)) {
    51           pcaDs[row, col] = value;
    52           row++;
    53         }
    54         col++;
    55       }
     46      var pcaDs = AlglibUtil.PrepareAndScaleInputMatrix(data.Dataset, data.AllowedInputVariables, data.TrainingIndices, scaling);
    5647
    5748      int info;
     
    6051      alglib.pcabuildbasis(pcaDs, instances, attributes, out info, out varianceValues, out matrix);
    6152
    62       var result = new double[attributes * dimensions];
    63       for (int i = 0; i < attributes; i++)
    64         for (int j = 0; j < dimensions; j++)
    65           result[i * dimensions + j] = matrix[i, j];
    66 
    67       return result;
     53      return matrix;
    6854    }
    6955
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/RandomInitializer.cs

    r8471 r9270  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
    25 using HeuristicLab.Data;
     25using HeuristicLab.Optimization;
    2626using HeuristicLab.Parameters;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828using HeuristicLab.Problems.DataAnalysis;
    29 using HeuristicLab.Random;
    3029
    3130namespace HeuristicLab.Algorithms.DataAnalysis {
    3231  [Item("Random", "Initializes the matrix randomly.")]
    3332  [StorableClass]
    34   public class RandomInitializer : ParameterizedNamedItem, INCAInitializer {
    35     private IValueParameter<IntValue> RandomParameter {
    36       get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
    37     }
    38     private IValueParameter<BoolValue> SetSeedRandomlyParameter {
    39       get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
    40     }
    41 
    42     public int Seed {
    43       get { return RandomParameter.Value.Value; }
    44       set { RandomParameter.Value.Value = value; }
    45     }
    46 
    47     public bool SetSeedRandomly {
    48       get { return SetSeedRandomlyParameter.Value.Value; }
    49       set { SetSeedRandomlyParameter.Value.Value = value; }
     33  public sealed class RandomInitializer : NcaInitializer, IStochasticOperator {
     34    public ILookupParameter<IRandom> RandomParameter {
     35      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    5036    }
    5137
    5238    [StorableConstructor]
    53     protected RandomInitializer(bool deserializing) : base(deserializing) { }
    54     protected RandomInitializer(RandomInitializer original, Cloner cloner) : base(original, cloner) { }
     39    private RandomInitializer(bool deserializing) : base(deserializing) { }
     40    private RandomInitializer(RandomInitializer original, Cloner cloner) : base(original, cloner) { }
    5541    public RandomInitializer()
    5642      : base() {
    57       Parameters.Add(new ValueParameter<IntValue>("Seed", "The seed for the random number generator.", new IntValue(0)));
    58       Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "Whether the seed should be randomized for each call.", new BoolValue(true)));
     43      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    5944    }
    6045
     
    6348    }
    6449
    65     public double[] Initialize(IClassificationProblemData data, int dimensions) {
    66       var instances = data.TrainingIndices.Count();
     50    public override double[,] Initialize(IClassificationProblemData data, Scaling scaling, int dimensions) {
    6751      var attributes = data.AllowedInputVariables.Count();
    6852
    69       var random = new MersenneTwister();
    70       if (SetSeedRandomly) Seed = random.Next();
    71       random.Reset(Seed);
    72 
    73       var range = data.AllowedInputVariables.Select(x => data.Dataset.GetDoubleValues(x).Max() - data.Dataset.GetDoubleValues(x).Min()).ToArray();
    74       var matrix = new double[attributes * dimensions];
    75       for (int i = 0; i < matrix.Length; i++)
    76         matrix[i] = random.NextDouble() / range[i / dimensions];
     53      var random = RandomParameter.ActualValue;
     54      var matrix = new double[attributes, dimensions];
     55      for (int i = 0; i < attributes; i++)
     56        for (int j = 0; j < dimensions; j++)
     57          matrix[i, j] = random.NextDouble();
    7758
    7859      return matrix;
Note: See TracChangeset for help on using the changeset viewer.