Class LinearParameters

java.lang.Object
org.tribuo.math.LinearParameters
All Implemented Interfaces:
Serializable, FeedForwardParameters, Parameters, ProtoSerializable<org.tribuo.math.protos.ParametersProto>

public class LinearParameters extends Object implements FeedForwardParameters
A Parameters for producing linear models.
See Also:
  • Field Details

    • CURRENT_VERSION

      public static final int CURRENT_VERSION
      Protobuf serialization version.
      See Also:
  • Constructor Details

    • LinearParameters

      public LinearParameters(int numFeatures, int numLabels)
      Constructor. The number of features and the number of outputs must be fixed and known in advance.
      Parameters:
      numFeatures - The number of features in the training dataset (excluding the bias).
      numLabels - The number of outputs in the training dataset.
    • LinearParameters

      public LinearParameters(DenseMatrix weightMatrix)
      Constructs a LinearParameters wrapped around a weight matrix.

      Used for serialization compatibility with Tribuo 4.0.

      Parameters:
      weightMatrix - The weight matrix to wrap.
  • Method Details

    • deserializeFromProto

      public static LinearParameters deserializeFromProto(int version, String className, com.google.protobuf.Any message) throws com.google.protobuf.InvalidProtocolBufferException
      Deserialization factory.
      Parameters:
      version - The serialized object version.
      className - The class name.
      message - The serialized data.
      Returns:
      The deserialized object.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - If the protobuf could not be parsed from the message.
    • serialize

      public org.tribuo.math.protos.ParametersProto serialize()
      Description copied from interface: ProtoSerializable
      Serializes this object to a protobuf.
      Specified by:
      serialize in interface ProtoSerializable<org.tribuo.math.protos.ParametersProto>
      Returns:
      The protobuf.
    • predict

      public DenseVector predict(SGDVector example)
      Generates an unnormalised prediction by leftMultiply'ing the weights with the incoming features.
      Specified by:
      predict in interface FeedForwardParameters
      Parameters:
      example - A feature vector
      Returns:
      A DenseVector containing a score for each label.
    • gradients

      public Tensor[] gradients(com.oracle.labs.mlrg.olcut.util.Pair<Double,SGDVector> score, SGDVector features)
      Generate the gradients for a particular feature vector given the loss and the per output gradients. This parameters returns a single element Tensor array.
      Specified by:
      gradients in interface FeedForwardParameters
      Parameters:
      score - The Pair returned by the objective.
      features - The feature vector.
      Returns:
      A Tensor array with a single Matrix containing all gradients.
    • getEmptyCopy

      public Tensor[] getEmptyCopy()
      This returns a DenseMatrix the same size as the Parameters.
      Specified by:
      getEmptyCopy in interface Parameters
      Returns:
      A Tensor array containing a single DenseMatrix.
    • get

      public Tensor[] get()
      Description copied from interface: Parameters
      Get a reference to the underlying Tensor array.
      Specified by:
      get in interface Parameters
      Returns:
      The parameters.
    • getWeightMatrix

      public DenseMatrix getWeightMatrix()
      Returns the weight matrix.
      Returns:
      The weight matrix.
    • set

      public void set(Tensor[] newWeights)
      Description copied from interface: Parameters
      Set the underlying Tensor array to newWeights.
      Specified by:
      set in interface Parameters
      Parameters:
      newWeights - New parameters to store in this object.
    • update

      public void update(Tensor[] gradients)
      Description copied from interface: Parameters
      Apply gradients to the parameters. Assumes that gradients is the same length as the parameters, and each Tensor is the same size as the corresponding one from the parameters.

      The gradients are added to the parameters.

      Specified by:
      update in interface Parameters
      Parameters:
      gradients - A Tensor array of updates, with the length equal to Parameters.get().length.
    • merge

      public Tensor[] merge(Tensor[][] gradients, int size)
      Description copied from interface: Parameters
      Merge together an array of gradient arrays. Assumes the first dimension is the number of gradient arrays and the second dimension is the number of parameter Tensors.

      For performance reasons this call may mutate the input gradient array, and may return a subset of those elements as the merge output.

      Specified by:
      merge in interface Parameters
      Parameters:
      gradients - An array of gradient update arrays.
      size - The number of elements of gradients to merge. Allows gradients to have unused elements.
      Returns:
      A single Tensor array of the summed gradients.
    • copy

      public LinearParameters copy()
      Description copied from interface: FeedForwardParameters
      Returns a copy of the parameters.
      Specified by:
      copy in interface FeedForwardParameters
      Returns:
      A copy of the model parameters.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object