Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Updating GridSearch to use the new .Learn() interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Jun 29, 2017
1 parent 0fa2965 commit bce541a
Show file tree
Hide file tree
Showing 20 changed files with 2,686 additions and 510 deletions.
12 changes: 11 additions & 1 deletion Sources/Accord.MachineLearning/Accord.MachineLearning.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
<Compile Include="Clustering\KMeans\BalancedKMeans.cs" />
<Compile Include="DecisionTrees\DecisionTreeHelper.cs" />
<Compile Include="BaseKNearestNeighbors`2.cs" />
<Compile Include="Performance\GridSearch\New\GridSearchRange.cs" />
<Compile Include="Performance\GridSearch\New\GridSearch`2.cs" />
<Compile Include="Performance\GridSearch\New\GridSearch`3.cs" />
<Compile Include="Performance\GridSearch\New\GridSearch`5.cs" />
<Compile Include="Performance\GridSearch\New\GridSearch.cs" />
<Compile Include="Performance\GridSearch\New\GridSearch`4.cs" />
<Compile Include="Performance\GridSearch\New\BaseGridSearch`5.cs" />
<Compile Include="Performance\Base\BaseSplitSetValidation`4.cs" />
<Compile Include="Performance\Bootstrap\New\BootstrapResult`3.cs" />
<Compile Include="Performance\Bootstrap\New\Bootstrap`2.cs" />
<Compile Include="Performance\Bootstrap\New\Bootstrap`3.cs" />
Expand All @@ -67,6 +75,8 @@
<Compile Include="Performance\Data Splits\Base\IDataSplit.cs" />
<Compile Include="Performance\Data Splits\TrainValTestDataSplit.cs" />
<Compile Include="Performance\Data Splits\TrainTestDataSplit.cs" />
<Compile Include="Performance\GridSearch\New\GridSearchResult`4.cs" />
<Compile Include="Performance\GridSearch\New\IGridSearchRange.cs" />
<Compile Include="Performance\SetResult.cs" />
<Compile Include="Performance\SplitResult.cs" />
<Compile Include="Performance\CrossValidation\New\CrossValidation`2.cs" />
Expand Down Expand Up @@ -170,7 +180,7 @@
<Compile Include="Performance\SplitSet\SplitSetStatistics`1.cs" />
<Compile Include="Performance\SplitSet\SplitSetResult`1.cs" />
<Compile Include="Performance\SplitSet\SplitSetValidation`1.cs" />
<Compile Include="GridSearch.cs" />
<Compile Include="GridSearch`1.cs" />
<Compile Include="Clustering\KMeans\KMeans.cs" />
<Compile Include="QLearning.cs" />
<Compile Include="Ransac.cs" />
Expand Down
24 changes: 22 additions & 2 deletions Sources/Accord.MachineLearning/GridSearchParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Accord.MachineLearning
{
using Accord.MachineLearning.Performance;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -30,6 +31,8 @@ namespace Accord.MachineLearning
/// Contains the name and value of a parameter that should be used during fitting.
/// </summary>
///
/// <seealso cref="GridSearch"/>
///
[Serializable]
public struct GridSearchParameter
{
Expand Down Expand Up @@ -106,21 +109,38 @@ public override int GetHashCode()
}

/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
///
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
///
public override string ToString()
{
return String.Format("{0}: {1}", name, value);
}

/// <summary>
/// Performs an implicit conversion from <see cref="GridSearchParameter"/> to <see cref="System.Double"/>.
/// </summary>
///
/// <param name="param">The parameter to be converted.</param>
///
/// <returns>The value of the parameter's <see cref="GridSearchParameter.Value"/>.</returns>
///
public static implicit operator double(GridSearchParameter param)
{
return param.Value;
}
}

/// <summary>
/// Grid search parameter collection.
/// </summary>
///
/// <seealso cref="GridSearch"/>
///
[Serializable]
public class GridSearchParameterCollection : KeyedCollection<string, GridSearchParameter>
{
Expand Down
259 changes: 132 additions & 127 deletions Sources/Accord.MachineLearning/GridSearchRange.cs
Original file line number Diff line number Diff line change
@@ -1,127 +1,132 @@
// Accord Machine Learning Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.MachineLearning
{
using System;
using System.Collections.ObjectModel;
using Accord.Math;

/// <summary>
/// Range of parameters to be tested in a grid search.
/// </summary>
///
[Serializable]
public class GridSearchRange
{

/// <summary>
/// Gets or sets the name of the parameter from which the range belongs to.
/// </summary>
///
public string Name { get; set; }

/// <summary>
/// Gets or sets the range of values that should be tested for this parameter.
/// </summary>
///
public double[] Values { get; set; }

/// <summary>
/// Constructs a new GridsearchRange object.
/// </summary>
///
/// <param name="name">The name for this parameter.</param>
/// <param name="start">The starting value for this range.</param>
/// <param name="end">The end value for this range.</param>
/// <param name="step">The step size for this range.</param>
///
public GridSearchRange(string name, double start, double end, double step)
{
this.Name = name;
this.Values = Vector.Interval(start, end, step);
}

/// <summary>
/// Constructs a new GridSearchRange object.
/// </summary>
///
/// <param name="name">The name for this parameter.</param>
/// <param name="values">The array of values to try.</param>
///
public GridSearchRange(string name, double[] values)
{
this.Name = name;
this.Values = values;
}

/// <summary>
/// Gets the array of GridSearchParameters to try.
/// </summary>
///
public GridSearchParameter[] GetParameters()
{
GridSearchParameter[] parameters = new GridSearchParameter[Values.Length];
for (int i = 0; i < Values.Length; i++)
parameters[i] = new GridSearchParameter(Name, Values[i]);
return parameters;
}

}

/// <summary>
/// GridSearchRange collection.
/// </summary>
///
[Serializable]
public class GridSearchRangeCollection : KeyedCollection<string, GridSearchRange>
{
/// <summary>
/// Constructs a new collection of GridsearchRange objects.
/// </summary>
///
public GridSearchRangeCollection(params GridSearchRange[] ranges)
{
foreach (var range in ranges)
this.Add(range);
}

/// <summary>
/// Returns the identifying value for an item on this collection.
/// </summary>
///
protected override string GetKeyForItem(GridSearchRange item)
{
return item.Name;
}

/// <summary>
/// Adds a parameter range to the end of the GridsearchRangeCollection.
/// </summary>
///
public void Add(string name, params double[] values)
{
this.Add(new GridSearchRange(name, values));
}
}

}
// Accord Machine Learning Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.MachineLearning
{
using System;
using System.Collections.ObjectModel;
using Accord.Math;
using Accord.MachineLearning.Performance;

/// <summary>
/// Range of parameters to be tested in a grid search.
/// </summary>
///
/// <seealso cref="GridSearch"/>
///
[Serializable]
public class GridSearchRange
{

/// <summary>
/// Gets or sets the name of the parameter from which the range belongs to.
/// </summary>
///
public string Name { get; set; }

/// <summary>
/// Gets or sets the range of values that should be tested for this parameter.
/// </summary>
///
public double[] Values { get; set; }

/// <summary>
/// Constructs a new GridsearchRange object.
/// </summary>
///
/// <param name="name">The name for this parameter.</param>
/// <param name="start">The starting value for this range.</param>
/// <param name="end">The end value for this range.</param>
/// <param name="step">The step size for this range.</param>
///
public GridSearchRange(string name, double start, double end, double step)
{
this.Name = name;
this.Values = Vector.Interval(start, end, step);
}

/// <summary>
/// Constructs a new GridSearchRange object.
/// </summary>
///
/// <param name="name">The name for this parameter.</param>
/// <param name="values">The array of values to try.</param>
///
public GridSearchRange(string name, double[] values)
{
this.Name = name;
this.Values = values;
}

/// <summary>
/// Gets the array of GridSearchParameters to try.
/// </summary>
///
public GridSearchParameter[] GetParameters()
{
GridSearchParameter[] parameters = new GridSearchParameter[Values.Length];
for (int i = 0; i < Values.Length; i++)
parameters[i] = new GridSearchParameter(Name, Values[i]);
return parameters;
}

}

/// <summary>
/// GridSearchRange collection.
/// </summary>
///
/// <seealso cref="GridSearch"/>
///
[Serializable]
public class GridSearchRangeCollection : KeyedCollection<string, GridSearchRange>
{
/// <summary>
/// Constructs a new collection of GridsearchRange objects.
/// </summary>
///
public GridSearchRangeCollection(params GridSearchRange[] ranges)
{
foreach (var range in ranges)
this.Add(range);
}

/// <summary>
/// Returns the identifying value for an item on this collection.
/// </summary>
///
protected override string GetKeyForItem(GridSearchRange item)
{
return item.Name;
}

/// <summary>
/// Adds a parameter range to the end of the GridsearchRangeCollection.
/// </summary>
///
public void Add(string name, params double[] values)
{
this.Add(new GridSearchRange(name, values));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace Accord.MachineLearning
using System.Threading.Tasks;
using Accord.Math;
using System.Threading;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Statistics.Kernels;

/// <summary>
/// Delegate for grid search fitting functions.
Expand Down Expand Up @@ -115,6 +117,7 @@ namespace Accord.MachineLearning
/// </code>
/// </example>
///
[Obsolete("Please use GridSearch<TModel, TInput, TOutput> instead.")]
[Serializable]
public class GridSearch<TModel> : IParallel
// TODO: Inherit from ISupervisedLearning<TModel>
Expand Down Expand Up @@ -241,6 +244,10 @@ public GridSearchResult<TModel> Compute()
parameters, models, errors, best);
}

internal object Create(object ranges, Func<object, SequentialMinimalOptimization<Polynomial>> learner, Func<object, object, object, double> loss)
{
throw new NotImplementedException();
}
}

/// <summary>
Expand Down
Loading

0 comments on commit bce541a

Please sign in to comment.