Click or drag to resize
Accord.NET (logo)

DynamicTimeWarping Structure

Dynamic Time Warping Sequence Kernel.

Namespace:  Accord.Statistics.Kernels
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.5.0
Syntax
[SerializableAttribute]
public struct DynamicTimeWarping : IKernel, 
	IKernel<double[]>, IKernel<double[][]>, ICloneable, 
	IDistance, IDistance<double[]>, IDistance<double[], double[]>, 
	IDistance<double[][]>, IDistance<double[][], double[][]>
Request Example View Source

The DynamicTimeWarping type exposes the following members.

Constructors
  NameDescription
Public methodDynamicTimeWarping(Int32)
Constructs a new Dynamic Time Warping kernel.
Public methodDynamicTimeWarping(Double, Int32)
Constructs a new Dynamic Time Warping kernel.
Public methodDynamicTimeWarping(Int32, Double)
Constructs a new Dynamic Time Warping kernel.
Public methodDynamicTimeWarping(Int32, Double, Int32)
Constructs a new Dynamic Time Warping kernel.
Top
Properties
  NameDescription
Public propertyAlpha
Gets or sets the hypersphere ratio. Default is 1.
Public propertyDegree
Gets or sets the polynomial degree for this kernel. Default is 1.
Public propertyLength
Gets or sets the length for the feature vectors contained in each sequence used by the kernel.
Top
Methods
  NameDescription
Public methodClone
Creates a new object that is a copy of the current instance.
Public methodDistance(Double, Double)
Computes the squared distance in feature space between two points given in input space.
Public methodDistance(Double, Double)
Computes the squared distance in feature space between two points given in input space.
Public methodEquals
Indicates whether this instance and a specified object are equal.
(Inherited from ValueType.)
Public methodFunction(Double, Double)
Dynamic Time Warping kernel function.
Public methodFunction(Double, Double)
Dynamic Time Warping kernel function.
Public methodGetHashCode
Returns the hash code for this instance.
(Inherited from ValueType.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns the fully qualified type name of this instance.
(Inherited from ValueType.)
Top
Extension Methods
  NameDescription
Public Extension MethodDistance
Computes the kernel distance for a kernel function even if it doesn't implement the IDistance interface. Can be used to check the proper implementation of the distance function.
(Defined by Tools.)
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by Matrix.)
Top
Remarks

The Dynamic Time Warping Sequence Kernel is a sequence kernel, accepting vector sequences of variable size as input. Despite the sequences being variable in size, the vectors contained in such sequences should have its size fixed and should be informed at the construction of this kernel.

The conversion of the DTW global distance to a dot product uses a combination of a technique known as spherical normalization and the polynomial kernel. The degree of the polynomial kernel and the alpha for the spherical normalization should be given at the construction of the kernel. For more information, please see the referenced papers shown below.

The use of a cache is highly advisable when using this kernel.

  • V. Wan, J. Carmichael; Polynomial Dynamic Time Warping Kernel Support Vector Machines for Dysarthric Speech Recognition with Sparse Training Data. Interspeech'2005 - Eurospeech - 9th European Conference on Speech Communication and Technology. Lisboa, 2005.

Examples

The following example demonstrates how to create and learn a Support Vector Machine (SVM) to recognize sequences of univariate observations using the Dynamic Time Warping kernel.

// Suppose you have sequences of univariate observations, 
// and that those sequences could be of arbitrary length.
// In this example, we have sequences binary numbers:

double[][] inputs =
{
    // Class -1
    new double[] { 0,1,1,0 },
    new double[] { 0,0,1,0 },  
    new double[] { 0,1,1,1,0 }, 
    new double[] { 0,1,0 },    

    // Class +1
    new double[] { 1,0,0,1 },   
    new double[] { 1,1,0,1 }, 
    new double[] { 1,0,0,0,1 },
    new double[] { 1,0,1 },   
    new double[] { 1,0,0,0,1,1 } 
};

int[] outputs =
{
        0, 0, 0, 0,  // First four sequences are of class 0
        1, 1, 1, 1, 1 // Last five sequences are of class 1
};

// Create the Sequential Minimal Optimization learning algorithm
var smo = new SequentialMinimalOptimization<DynamicTimeWarping>()
{
    Complexity = 1.5,

    // Set the parameters of the kernel
    Kernel = new DynamicTimeWarping(alpha: 1, degree: 1)
};

// And use it to learn a machine!
var svm = smo.Learn(inputs, outputs);

// Now we can compute predicted values
bool[] predicted = svm.Decide(inputs);

// And check how far we are from the expected values
double error = new ZeroOneLoss(outputs).Loss(predicted); // error will be 0.0

Now, instead of having univariate observations, the following example demonstrates how to create and learn a sequences of multivariate (or n-dimensional) observations.

// Suppose you have sequences of multivariate observations, and that
// those sequences could be of arbitrary length. On the other hand, 
// each observation have a fixed, delimited number of dimensions.

// In this example, we have sequences of 3-dimensional observations. 
// Each sequence can have an arbitrary length, but each observation
// will always have length 3:

double[][][] sequences =
{
    new double[][] // first sequence
    {
        new double[] { 1, 1, 1 }, // first observation of the first sequence
        new double[] { 1, 2, 1 }, // second observation of the first sequence
        new double[] { 1, 4, 2 }, // third observation of the first sequence
        new double[] { 2, 2, 2 }, // fourth observation of the first sequence
    },

    new double[][] // second sequence (note that this sequence has a different length)
    {
        new double[] { 1, 1, 1 }, // first observation of the second sequence
        new double[] { 1, 5, 6 }, // second observation of the second sequence
        new double[] { 2, 7, 1 }, // third observation of the second sequence
    },

    new double[][] // third sequence 
    {
        new double[] { 8, 2, 1 }, // first observation of the third sequence
    },

    new double[][] // fourth sequence 
    {
        new double[] { 8, 2, 5 }, // first observation of the fourth sequence
        new double[] { 1, 5, 4 }, // second observation of the fourth sequence
    }
};

// Now, we will also have different class labels associated which each 
// sequence. We will assign -1 to sequences whose observations start 
// with { 1, 1, 1 } and +1 to those that do not:

int[] outputs =
{
        0, 0,  // First two sequences are of class 0 (those start with {1,1,1})
        1, 1,  // Last two sequences are of class  1 (don't start with {1,1,1})
};

// Now we can create the Sequential Minimal Optimization learning algorithm
var smo = new SequentialMinimalOptimization<DynamicTimeWarping, double[][]>()
{
    Complexity = 1.5,

    // Set the parameters of the kernel
    Kernel = new DynamicTimeWarping(alpha: 1, degree: 1)
};

// And use it to learn a machine!
var svm = smo.Learn(sequences, outputs); 

// Now we can compute predicted values
bool[] predicted = svm.Decide(sequences);

// And check how far we are from the expected values
double error = new ZeroOneLoss(outputs).Loss(predicted); // error will be 0.0


// At this point, we should have obtained an useful machine. Let's
// see if it can understand a few examples it hasn't seem before:

double[][] a = 
{ 
    new double[] { 1, 1, 1 },
    new double[] { 7, 2, 5 },
    new double[] { 2, 5, 1 },
};

double[][] b =
{
    new double[] { 7, 5, 2 },
    new double[] { 4, 2, 5 },
    new double[] { 3, 1, 2 },
};

// Following the aforementioned logic, sequence (a) should be
// classified as -1, and sequence (b) should be classified as +1.

bool resultA = svm.Decide(a); // false
bool resultB = svm.Decide(b); // true
See Also