Class LinearCollection

  • All Implemented Interfaces:
    Block

    public class LinearCollection
    extends AbstractBlock
    A LinearCollection block applies \(m\) linear transformations \(Y_i = X_i W_i + b_i\) for each \(i \in [1, \ldots, m]\) and \(m = \prod_{j=1}^t s_j\). \(t\) is typically 1, so compared to a Linear block, the involved shapes have typically one split dimension added which separates the different linear transformations from each other. Another difference to a Linear block is that the weight is not transposed (to align with the internally used algebraic operation NDArray.matMul(NDArray) ).

    It has the following shapes:

    • input X: [x_1, s_1, s_2, …, s_t, input_dim]
    • weight W: [s_1, s_2, …, s_t, input_dim, units]
    • Bias b: [s_1, s_2, …, s_t, units]
    • output Y: [x_1, s_1, s_2, …, s_t, units]

    The LinearCollection block should be constructed using LinearCollection.Builder.

    • Method Detail

      • getOutputShapes

        public Shape[] getOutputShapes​(Shape[] inputs)
        Returns the expected output shapes of the block for the specified input shapes.
        Parameters:
        inputs - the shapes of the inputs
        Returns:
        the expected output shapes of the block
      • describeInput

        public ai.djl.util.PairList<java.lang.String,​Shape> describeInput()
        Returns a PairList of input names, and shapes.
        Specified by:
        describeInput in interface Block
        Overrides:
        describeInput in class AbstractBaseBlock
        Returns:
        the PairList of input names, and shapes
      • beforeInitialize

        protected void beforeInitialize​(Shape... inputShapes)
        Performs any action necessary before initialization. For example, keep the input information or verify the layout.
        Overrides:
        beforeInitialize in class AbstractBaseBlock
        Parameters:
        inputShapes - the expected shapes of the input
      • saveMetadata

        protected void saveMetadata​(java.io.DataOutputStream os)
                             throws java.io.IOException
        Override this method to save additional data apart from parameter values.

        This default implementation saves the currently set input shapes.

        Overrides:
        saveMetadata in class AbstractBaseBlock
        Parameters:
        os - the non-null output stream the parameter values and metadata are written to
        Throws:
        java.io.IOException - saving failed
      • loadMetadata

        public void loadMetadata​(byte loadVersion,
                                 java.io.DataInputStream is)
                          throws java.io.IOException,
                                 MalformedModelException
        Overwrite this to load additional metadata with the parameter values.

        If you overwrite AbstractBaseBlock.saveMetadata(DataOutputStream) or need to provide backward compatibility to older binary formats, you probably need to overwrite this. This default implementation checks if the version number fits, if not it throws an MalformedModelException. After that it restores the input shapes.

        Overrides:
        loadMetadata in class AbstractBaseBlock
        Parameters:
        loadVersion - the version used for loading this metadata.
        is - the input stream we are loading from
        Throws:
        java.io.IOException - loading failed
        MalformedModelException - data can be loaded but has wrong format
      • linear

        public NDList linear​(NDArray input,
                             NDArray weight,
                             NDArray bias)
        Applies linear transformations to the incoming data.
        Parameters:
        input - input X: [x_1, s_1, s_2, …, s_t, input_dim]
        weight - weight W: [s_1, s_2, …, s_t, input_dim, units]
        bias - bias b: [s_1, s_2, …, s_t, units]
        Returns:
        output Y: [x_1, s_1, s_2, …, s_t, units]
      • builder

        public static LinearCollection.Builder builder()
        Creates a builder to build a Linear.
        Returns:
        a new builder