Class LearnShapeletsLearningAlgorithm

  • All Implemented Interfaces:
    java.lang.Iterable<org.api4.java.algorithm.events.IAlgorithmEvent>, java.util.concurrent.Callable<LearnShapeletsClassifier>, java.util.Iterator<org.api4.java.algorithm.events.IAlgorithmEvent>, org.api4.java.algorithm.IAlgorithm<ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2,​LearnShapeletsClassifier>, org.api4.java.common.control.ICancelable, org.api4.java.common.control.ILoggingCustomizable, org.api4.java.common.event.IEventEmitter<java.lang.Object>, org.api4.java.common.event.IRelaxedEventEmitter

    public class LearnShapeletsLearningAlgorithm
    extends ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSCLearningAlgorithm<java.lang.Integer,​LearnShapeletsClassifier>
    Generalized Shapelets Learning implementation for LearnShapeletsClassifier published in "J. Grabocka, N. Schilling, M. Wistuba, L. Schmidt-Thieme: Learning Time-Series Shapelets" (https://www.ismll.uni-hildesheim.de/pub/pdfs/grabocka2014e-kdd.pdf)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double ALPHA
      Predefined alpha parameter used within the calculations.
      static boolean USE_BIAS_CORRECTION
      Indicator whether Bessel's correction should be used when normalizing arrays.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static double calculateD​(double[][][] s, int minShapeLength, int r, double[] instance, int k, int j)
      Function to calculate the distance between the j-th segment of the given time series instance and the k-th shapelet stored in the shapelet tensor S.
      static double calculateMHat​(double[][][] s, int minShapeLength, int r, double[] instance, int k, int Q, double alpha)
      Function to calculate the soft-minimum function which is a differentiable approximation of the minimum distance matrix given in the paper in section 3.1.4.
      LearnShapeletsClassifier call()
      Main function to train a LearnShapeletsClassifier.
      int getC()  
      LearnShapeletsLearningAlgorithm.ILearnShapeletsLearningAlgorithmConfig getConfig()
      static int getNumberOfSegments​(int Q, int minShapeLength, int r)
      Returns the number of segments which are available for a instance with Q attributes for a given scale r and a minimum shape length minShapeLength.
      double[][][] initializeS​(double[][] trainingMatrix)
      Initializes the tensor S storing the shapelets for each scale.
      void initializeWeights​(double[][][] w, double[] w0)
      Randomly initializes the weights around zero.
      boolean isUseInstanceReordering()  
      org.api4.java.algorithm.events.IAlgorithmEvent nextWithException()
      void performSGD​(double[][][] w, double[][][] wHist, double[] w0, double[] w0Hist, double[][][] s, double[][][] sHist, double[][] dataMatrix, int[][] y, long beginTime, int[] targets)
      Method performing the stochastic gradient descent to learn the weights and shapelets.
      void setC​(int c)  
      void setUseInstanceReordering​(boolean useInstanceReordering)  
      java.util.List<java.lang.Integer> shuffleAccordingToAlternatingClassScheme​(java.util.List<java.lang.Integer> instanceIndices, int[] targets, java.util.Random random)
      Shuffles the data in a class alternating scheme.
      • Methods inherited from class ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSCLearningAlgorithm

        cancel, getClassifier, hasNext, iterator, next, registerListener
      • Methods inherited from class ai.libs.jaicore.basic.algorithm.AAlgorithm

        activate, announceTimeoutDetected, avoidReinterruptionOnShutdownOnCurrentThread, checkAndConductTermination, checkTermination, computeTimeoutAware, getActivationTime, getDeadline, getId, getInput, getListeners, getLoggerName, getNumCPUs, getRemainingTimeToDeadline, getState, getTimeout, getTimeoutPrecautionOffset, hasThreadBeenInterruptedDuringShutdown, interruptThreadAsPartOfShutdown, isCanceled, isShutdownInitialized, isStopCriterionSatisfied, isTimeoutDefined, isTimeouted, post, registerActiveThread, resolveShutdownInterruptOnCurrentThread, setConfig, setDeadline, setLoggerName, setMaxNumThreads, setNumCPUs, setState, setTimeout, setTimeout, setTimeoutPrecautionOffset, shutdown, terminate, unregisterActiveThread, unregisterThreadAndShutdown
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface java.util.Iterator

        forEachRemaining, remove
    • Field Detail

      • USE_BIAS_CORRECTION

        public static final boolean USE_BIAS_CORRECTION
        Indicator whether Bessel's correction should be used when normalizing arrays.
        See Also:
        Constant Field Values
      • ALPHA

        public static final double ALPHA
        Predefined alpha parameter used within the calculations.
        See Also:
        Constant Field Values
    • Method Detail

      • initializeS

        public double[][][] initializeS​(double[][] trainingMatrix)
                                 throws org.api4.java.ai.ml.core.exception.TrainingException
        Initializes the tensor S storing the shapelets for each scale. The initialization is done by deriving inital shapelets from all normalized segments.
        Parameters:
        trainingMatrix - The training matrix used for the initialization of S.
        Returns:
        Return the initialized tensor storing an initial guess for the shapelets based on the clustering
        Throws:
        org.api4.java.ai.ml.core.exception.TrainingException
      • call

        public LearnShapeletsClassifier call()
                                      throws org.api4.java.algorithm.exceptions.AlgorithmException
        Main function to train a LearnShapeletsClassifier.
        Throws:
        org.api4.java.algorithm.exceptions.AlgorithmException
      • initializeWeights

        public void initializeWeights​(double[][][] w,
                                      double[] w0)
        Randomly initializes the weights around zero. As opposed to the paper, the approach has been changed to a different standard deviation as used in the reference implementation for performance reasons.
        Parameters:
        w - The weight matrix
        w0 - The bias vector
      • performSGD

        public void performSGD​(double[][][] w,
                               double[][][] wHist,
                               double[] w0,
                               double[] w0Hist,
                               double[][][] s,
                               double[][][] sHist,
                               double[][] dataMatrix,
                               int[][] y,
                               long beginTime,
                               int[] targets)
        Method performing the stochastic gradient descent to learn the weights and shapelets.
        Parameters:
        w - The weight matrix
        wHist - The weight's history matrix used for smoothing learning
        w0 - The bias vector
        w0Hist - The bias' history vector used for smoothing learning
        s - The shapelet matrix
        sHist - The shapelet's history matrix used for smoothing learning
        dataMatrix - The data values matrix
        y - The binarized target matrix
        beginTime - The begin time used to check for the timeout
      • shuffleAccordingToAlternatingClassScheme

        public java.util.List<java.lang.Integer> shuffleAccordingToAlternatingClassScheme​(java.util.List<java.lang.Integer> instanceIndices,
                                                                                          int[] targets,
                                                                                          java.util.Random random)
        Shuffles the data in a class alternating scheme. That means that at first, all indices per class are shuffled. Then, the randomized indices are selected in a round robin fashion among the classes.
        Parameters:
        instanceIndices - The instance indices the original dataset
        targets - The targets of each instance
        random - Random object used for randomized shuffling
        Returns:
        Returns the list of the shuffled indices of the alternating class scheme. Each index of instanceIndices is only used once (without replacement)
      • calculateMHat

        public static double calculateMHat​(double[][][] s,
                                           int minShapeLength,
                                           int r,
                                           double[] instance,
                                           int k,
                                           int Q,
                                           double alpha)
        Function to calculate the soft-minimum function which is a differentiable approximation of the minimum distance matrix given in the paper in section 3.1.4.
        Parameters:
        s - The tensor storing the shapelets for different scales
        minShapeLength - The minimum shape length
        r - The number of scale to look at
        instance - The instance time series vector
        k - The index of the shapelet to look at
        Q - The number of attributes (time series length)
        alpha - Parameter to control the desired precision of the M_hat approximation
        Returns:
        Returns the approximation of the minimum distance of the instance and the shapelet given by the parameters r and k.
      • calculateD

        public static double calculateD​(double[][][] s,
                                        int minShapeLength,
                                        int r,
                                        double[] instance,
                                        int k,
                                        int j)
        Function to calculate the distance between the j-th segment of the given time series instance and the k-th shapelet stored in the shapelet tensor S.
        Parameters:
        s - The tensor storing the shapelets for different scales
        minShapeLength - The minimum shape length
        r - The number of scale to look at
        instance - The instance time series vector
        k - The index of the shapelet to look at
        j - The segment of the instance time series to look at
        Returns:
        Returns the minimum distance of the j-th segment of the instance and the shapelet given by the parameters r, k and j.
      • nextWithException

        public org.api4.java.algorithm.events.IAlgorithmEvent nextWithException()
        Specified by:
        nextWithException in interface org.api4.java.algorithm.IAlgorithm<ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2,​LearnShapeletsClassifier>
        Overrides:
        nextWithException in class ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSCLearningAlgorithm<java.lang.Integer,​LearnShapeletsClassifier>
      • getNumberOfSegments

        public static int getNumberOfSegments​(int Q,
                                              int minShapeLength,
                                              int r)
        Returns the number of segments which are available for a instance with Q attributes for a given scale r and a minimum shape length minShapeLength.
        Parameters:
        Q - Number of attributes of an instance
        minShapeLength - Minimum shapelet length
        r - Scale to be looked at
        Returns:
        Returns the number of segments which can be looked at for an instance with Q time series attributes
      • isUseInstanceReordering

        public boolean isUseInstanceReordering()
        Returns:
        the useInstanceReordering
      • setUseInstanceReordering

        public void setUseInstanceReordering​(boolean useInstanceReordering)
        Parameters:
        useInstanceReordering - the useInstanceReordering to set
      • getC

        public int getC()
        Returns:
        the c
      • setC

        public void setC​(int c)
        Parameters:
        c - the c to set