Class LearnShapeletsLearningAlgorithm
- java.lang.Object
-
- ai.libs.jaicore.basic.algorithm.AAlgorithm<ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2,C>
-
- ai.libs.jaicore.ml.classification.singlelabel.timeseries.learner.ASimplifiedTSCLearningAlgorithm<java.lang.Integer,LearnShapeletsClassifier>
-
- ai.libs.jaicore.ml.weka.classification.singlelabel.timeseries.learner.shapelets.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 forLearnShapeletsClassifierpublished 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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceLearnShapeletsLearningAlgorithm.ILearnShapeletsLearningAlgorithmConfig
-
Field Summary
Fields Modifier and Type Field Description static doubleALPHAPredefined alpha parameter used within the calculations.static booleanUSE_BIAS_CORRECTIONIndicator whether Bessel's correction should be used when normalizing arrays.
-
Constructor Summary
Constructors Constructor Description LearnShapeletsLearningAlgorithm(LearnShapeletsLearningAlgorithm.ILearnShapeletsLearningAlgorithmConfig config, LearnShapeletsClassifier classifier, ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2 dataset)Constructor of the algorithm to train aLearnShapeletsClassifier.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static doublecalculateD(double[][][] s, int minShapeLength, int r, double[] instance, int k, int j)Function to calculate the distance between thej-th segment of the given time seriesinstanceand thek-th shapelet stored in the shapelet tensorS.static doublecalculateMHat(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.LearnShapeletsClassifiercall()Main function to train aLearnShapeletsClassifier.intgetC()LearnShapeletsLearningAlgorithm.ILearnShapeletsLearningAlgorithmConfiggetConfig()static intgetNumberOfSegments(int Q, int minShapeLength, int r)Returns the number of segments which are available for a instance withQattributes for a given scalerand a minimum shape lengthminShapeLength.double[][][]initializeS(double[][] trainingMatrix)Initializes the tensorSstoring the shapelets for each scale.voidinitializeWeights(double[][][] w, double[] w0)Randomly initializes the weights around zero.booleanisUseInstanceReordering()org.api4.java.algorithm.events.IAlgorithmEventnextWithException()voidperformSGD(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.voidsetC(int c)voidsetUseInstanceReordering(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
-
-
-
-
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
-
-
Constructor Detail
-
LearnShapeletsLearningAlgorithm
public LearnShapeletsLearningAlgorithm(LearnShapeletsLearningAlgorithm.ILearnShapeletsLearningAlgorithmConfig config, LearnShapeletsClassifier classifier, ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2 dataset)
Constructor of the algorithm to train aLearnShapeletsClassifier.
-
-
Method Detail
-
initializeS
public double[][][] initializeS(double[][] trainingMatrix) throws org.api4.java.ai.ml.core.exception.TrainingExceptionInitializes the tensorSstoring 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 ofS.- 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 aLearnShapeletsClassifier.- 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 matrixw0- 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 matrixwHist- The weight's history matrix used for smoothing learningw0- The bias vectorw0Hist- The bias' history vector used for smoothing learnings- The shapelet matrixsHist- The shapelet's history matrix used for smoothing learningdataMatrix- The data values matrixy- The binarized target matrixbeginTime- 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 datasettargets- The targets of each instancerandom- Random object used for randomized shuffling- Returns:
- Returns the list of the shuffled indices of the alternating class
scheme. Each index of
instanceIndicesis 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 scalesminShapeLength- The minimum shape lengthr- The number of scale to look atinstance- The instance time series vectork- The index of the shapelet to look atQ- 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
randk.
-
calculateD
public static double calculateD(double[][][] s, int minShapeLength, int r, double[] instance, int k, int j)Function to calculate the distance between thej-th segment of the given time seriesinstanceand thek-th shapelet stored in the shapelet tensorS.- Parameters:
s- The tensor storing the shapelets for different scalesminShapeLength- The minimum shape lengthr- The number of scale to look atinstance- The instance time series vectork- The index of the shapelet to look atj- 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 parametersr,kandj.
-
nextWithException
public org.api4.java.algorithm.events.IAlgorithmEvent nextWithException()
- Specified by:
nextWithExceptionin interfaceorg.api4.java.algorithm.IAlgorithm<ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2,LearnShapeletsClassifier>- Overrides:
nextWithExceptionin classai.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 withQattributes for a given scalerand a minimum shape lengthminShapeLength.- Parameters:
Q- Number of attributes of an instanceminShapeLength- Minimum shapelet lengthr- Scale to be looked at- Returns:
- Returns the number of segments which can be looked at for an instance
with
Qtime series attributes
-
getConfig
public LearnShapeletsLearningAlgorithm.ILearnShapeletsLearningAlgorithmConfig getConfig()
- Specified by:
getConfigin interfaceorg.api4.java.algorithm.IAlgorithm<ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2,LearnShapeletsClassifier>- Overrides:
getConfigin classai.libs.jaicore.basic.algorithm.AAlgorithm<ai.libs.jaicore.ml.classification.singlelabel.timeseries.dataset.TimeSeriesDataset2,LearnShapeletsClassifier>
-
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
-
-