Class KNNTrainer<T extends Output<T>>

java.lang.Object
org.tribuo.common.nearest.KNNTrainer<T>
All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.config.Configurable, com.oracle.labs.mlrg.olcut.provenance.Provenancable<TrainerProvenance>, Trainer<T>

public class KNNTrainer<T extends Output<T>> extends Object implements Trainer<T>
A Trainer for k-nearest neighbour models.
  • Constructor Details

    • KNNTrainer

      public KNNTrainer(int k, Distance dist, int numThreads, EnsembleCombiner<T> combiner, KNNModel.Backend backend, NeighboursQueryFactoryType nqFactoryType)
      Creates a K-NN trainer using the supplied parameters.
      Parameters:
      k - The number of nearest neighbours to consider.
      dist - The distance function.
      numThreads - The number of threads to use.
      combiner - The combination function to aggregate the k predictions.
      backend - The computational backend.
      nqFactoryType - The nearest neighbour implementation factory to use.
    • KNNTrainer

      @Deprecated public KNNTrainer(int k, KNNTrainer.Distance distance, int numThreads, EnsembleCombiner<T> combiner, KNNModel.Backend backend)
      Deprecated.
      This Constructor is deprecated in version 4.3.
      Creates a K-NN trainer using the supplied parameters. neighboursQueryFactory defaults to NeighboursBruteForceFactory.
      Parameters:
      k - The number of nearest neighbours to consider.
      distance - The distance function.
      numThreads - The number of threads to use.
      combiner - The combination function to aggregate the k predictions.
      backend - The computational backend.
    • KNNTrainer

      public KNNTrainer(int k, int numThreads, EnsembleCombiner<T> combiner, KNNModel.Backend backend, NeighboursQueryFactory neighboursQueryFactory)
      Creates a K-NN trainer using the supplied parameters.
      Parameters:
      k - The number of nearest neighbours to consider.
      numThreads - The number of threads to use.
      combiner - The combination function to aggregate the k predictions.
      backend - The computational backend.
      neighboursQueryFactory - The nearest neighbour implementation factory to use.
  • Method Details

    • postConfig

      public void postConfig()
      Used by the OLCUT configuration system, and should not be called by external code.
      Specified by:
      postConfig in interface com.oracle.labs.mlrg.olcut.config.Configurable
    • train

      public Model<T> train(Dataset<T> examples, Map<String,com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance)
      Description copied from interface: Trainer
      Trains a predictive model using the examples in the given data set.
      Specified by:
      train in interface Trainer<T extends Output<T>>
      Parameters:
      examples - the data set containing the examples.
      runProvenance - Training run specific provenance (e.g., fold number).
      Returns:
      a predictive model that can be used to generate predictions for new examples.
    • train

      public Model<T> train(Dataset<T> examples, Map<String,com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance, int invocationCount)
      Description copied from interface: Trainer
      Trains a predictive model using the examples in the given data set.
      Specified by:
      train in interface Trainer<T extends Output<T>>
      Parameters:
      examples - the data set containing the examples.
      runProvenance - Training run specific provenance (e.g., fold number).
      invocationCount - The invocation counter that the trainer should be set to before training, which in most cases alters the state of the RNG inside this trainer. If the value is set to Trainer.INCREMENT_INVOCATION_COUNT then the invocation count is not changed.
      Returns:
      a predictive model that can be used to generate predictions for new examples.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getInvocationCount

      public int getInvocationCount()
      Description copied from interface: Trainer
      The number of times this trainer instance has had it's train method invoked.

      This is used to determine how many times the trainer's RNG has been accessed to ensure replicability in the random number stream.

      Specified by:
      getInvocationCount in interface Trainer<T extends Output<T>>
      Returns:
      The number of train invocations.
    • setInvocationCount

      public void setInvocationCount(int invocationCount)
      Description copied from interface: Trainer
      Set the internal state of the trainer to the provided number of invocations of the train method.

      This is used when reproducing a Tribuo-trained model by setting the state of the RNG to what it was at when Tribuo trained the original model by simulating invocations of the train method. This method should ALWAYS be overridden, and the default method is purely for compatibility.

      In a future major release this default implementation will be removed.

      Specified by:
      setInvocationCount in interface Trainer<T extends Output<T>>
      Parameters:
      invocationCount - the number of invocations of the train method to simulate
    • getProvenance

      public TrainerProvenance getProvenance()
      Specified by:
      getProvenance in interface com.oracle.labs.mlrg.olcut.provenance.Provenancable<T extends Output<T>>