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

Commit

Permalink
GH-398: PrincipalComponentAnalysis serialization error in v3.4.0
Browse files Browse the repository at this point in the history
Fixing serialization of PCA, KPCA, LDA, KDA and other classes that contain a CancellationToken.
  • Loading branch information
cesarsouza committed Jan 27, 2017
1 parent 1e4b4aa commit 1f34143
Show file tree
Hide file tree
Showing 38 changed files with 453 additions and 52 deletions.
1 change: 1 addition & 0 deletions Sources/Accord.Core/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static class ExtensionMethods
///
/// <returns>A copy of the collection where each element has also been copied.</returns>
///
[Obsolete("Please use Matrix.Copy() instead.")]
public static T DeepClone<T>(this T list)
where T : IList<ICloneable>, ICloneable
{
Expand Down
13 changes: 13 additions & 0 deletions Sources/Accord.Core/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ public static T Load<T>(Stream stream, BinaryFormatter formatter)
}
}

/// <summary>
/// Performs a deep copy of an object by serializing and deserializing it.
/// </summary>
///
/// <typeparam name="T">The type of the model to be copied.</typeparam>
/// <param name="obj">The object.</param>
///
/// <returns>A deep copy of the given object.</returns>
///
public static T DeepClone<T>(this T obj)
{
return Load<T>(Save<T>(obj));
}


private static SerializationBinder GetBinder(Type type)
Expand Down
9 changes: 8 additions & 1 deletion Sources/Accord.MachineLearning/Rules/Apriori.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public class Apriori<T> :
IUnsupervisedLearning<AssociationRuleMatcher<T>, SortedSet<T>, SortedSet<T>[]>,
IUnsupervisedLearning<AssociationRuleMatcher<T>, T[], T[][]>
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private int supportMin;
private double confidence;
private Dictionary<SortedSet<T>, int> frequent;
Expand All @@ -87,7 +90,11 @@ public Dictionary<SortedSet<T>, int> Frequent
/// stop the learning algorithm while it is running.
/// </summary>
/// <value>The token.</value>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Initializes a new instance of the <see cref="Apriori{T}"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ public abstract class BaseSupportVectorCalibration<TModel, TKernel, TInput> :
where TModel : SupportVectorMachine<TKernel, TInput>
where TInput : ICloneable
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

// Support Vector Machine parameters
private TModel machine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ public abstract class BaseSupportVectorRegression<TModel, TKernel, TInput> :
where TModel : SupportVectorMachine<TKernel, TInput>, ISupportVectorMachine<TInput>
where TInput : ICloneable
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

// Training data
private TInput[] inputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public class OneclassSupportVectorLearning<TModel, TKernel, TInput>
where TModel : SupportVectorMachine<TKernel, TInput>
where TInput : ICloneable
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private double[] alpha;
private TInput[] inputs;
private double nu = 0.5;
Expand Down Expand Up @@ -122,7 +125,11 @@ public OneclassSupportVectorLearning()
/// stop the learning algorithm while it is running.
/// </summary>
///
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Convergence tolerance. Default value is 1e-2.
Expand Down
1 change: 1 addition & 0 deletions Sources/Accord.Math/Accord.Math.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="Decompositions\SingularValueDecompositionF.cs">
<DependentUpon>SingularValueDecomposition.tt</DependentUpon>
</Compile>
<Compile Include="Distances\Angular.cs" />
<Compile Include="Matrix\Jagged.Product.Generated.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ namespace Accord.Math.Optimization
///
public abstract class BaseGradientOptimizationMethod : BaseOptimizationMethod
{
[NonSerialized]
private CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
///
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Gets or sets a function returning the gradient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace Accord.Math.Optimization
///
public class FanChenLinQuadraticOptimization : IOptimizationMethod
{
[NonSerialized]
CancellationToken token = new CancellationToken();

const double TAU = 1e-12;

Expand Down Expand Up @@ -146,7 +148,11 @@ public double[] Solution
/// stop the learning algorithm while it is running.
/// </summary>
///
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Gets the output of the function at the current <see cref="Solution" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Accord.Math.Optimization
///
public class GaussNewton : ILeastSquaresMethod
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private int numberOfParameters;

Expand Down Expand Up @@ -82,7 +84,11 @@ public class GaussNewton : ILeastSquaresMethod
/// stop the learning algorithm while it is running.
/// </summary>
///
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Gets the number of variables (free parameters) in the optimization problem.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ namespace Accord.MachineLearning
public class MinimumMeanDistanceClassifier : MulticlassScoreClassifierBase<double[]>,
ISupervisedLearning<MinimumMeanDistanceClassifier, double[], int>
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private double[][] means;
private IDistance<double[]> distance = new SquareEuclidean();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Gets or sets the class means to which samples will be compared against.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ namespace Accord.Statistics.Analysis
public abstract class BaseDiscriminantAnalysis : TransformBase<double[], double[]>
#pragma warning restore 612, 618
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private int numSamples;
private int numClasses;
private double[] totalMeans;
Expand Down Expand Up @@ -141,7 +144,11 @@ protected void Init(double[][] inputs, int[] outputs)
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}


/// <summary>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,18 @@ public class LogisticRegressionAnalysis : TransformBase<double[], double>, // TO
ISupervisedLearning<LogisticRegression, double[], int>,
ISupervisedLearning<LogisticRegression, double[], double>
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

private LogisticRegression regression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@ public class MultinomialLogisticRegressionAnalysis : TransformBase<double[], int
ISupervisedLearning<MultinomialLogisticRegression, double[], double[]>,
ISupervisedLearning<MultinomialLogisticRegression, double[], int>
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
///
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

int inputCount;
int outputCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ public class MultipleLinearRegressionAnalysis : TransformBase<double[], double>,
IRegressionAnalysis, IAnova,
ISupervisedLearning<MultipleLinearRegression, double[], double>
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}


internal MultipleLinearRegression regression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,18 @@ public class PartialLeastSquaresAnalysis : MultipleTransformBase<double[], doubl
ISupervisedLearning<MultivariateLinearRegression, double[], double[]>
#pragma warning restore 612, 618
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

[Obsolete]
internal double[,] sourceX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ namespace Accord.Statistics.Analysis
public class ProportionalHazardsAnalysis : IRegressionAnalysis,
ISupervisedLearning<ProportionalHazards, Tuple<double[], double>, int>
{
[NonSerialized]
CancellationToken token = new CancellationToken();

/// <summary>
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

private ProportionalHazards regression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class HiddenConjugateGradientLearning<T> :
IHiddenConditionalRandomFieldLearning<T>,
IConvergenceLearning, IDisposable
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private ForwardBackwardGradient<T> calculator;
private ConjugateGradient optimizer;
Expand All @@ -56,7 +58,11 @@ public class HiddenConjugateGradientLearning<T> :
/// stop the learning algorithm while it is running.
/// </summary>
///
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}

/// <summary>
/// Gets or sets the model being trained.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class HiddenGradientDescentLearning<T> :
IHiddenConditionalRandomFieldLearning<T>,
IConvergenceLearning, IDisposable
{
[NonSerialized]
CancellationToken token = new CancellationToken();

private double learningRate = 100;
private ISingleValueConvergence convergence;

Expand All @@ -71,7 +74,11 @@ public class HiddenGradientDescentLearning<T> :
/// Gets or sets a cancellation token that can be used to
/// stop the learning algorithm while it is running.
/// </summary>
public CancellationToken Token { get; set; }
public CancellationToken Token
{
get { return token; }
set { token = value; }
}


/// <summary>
Expand Down
Loading

0 comments on commit 1f34143

Please sign in to comment.