Class Linear

  • All Implemented Interfaces:
    Block

    public class Linear
    extends AbstractBlock
    A Linear block applies a linear transformation \(Y = XW^T + b\).

    It has the following shapes:

    • input X: [x1, x2, …, xn, input_dim]
    • weight W: [units, input_dim]
    • Bias b: [units]
    • output Y: [x1, x2, …, xn, units]

    The Linear block should be constructed using Linear.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 AbstractBlock
        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 AbstractBlock
        Parameters:
        inputShapes - the expected shapes of the input
      • prepare

        public void prepare​(Shape[] inputShapes)
        Sets the shape of Parameters.
        Overrides:
        prepare in class AbstractBlock
        Parameters:
        inputShapes - the shapes of inputs
      • 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 AbstractBlock
        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 AbstractBlock.saveMetadata(DataOutputStream) or need to provide backward compatibility to older binary formats, you prabably 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 AbstractBlock
        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 static NDList linear​(NDArray input,
                                    NDArray weight)
        Applies a linear transformation to the incoming data.
        Parameters:
        input - input X: [x1, x2, …, xn, input_dim]
        weight - weight W: [units, input_dim]
        Returns:
        output Y: [x1, x2, …, xn, units]
      • linear

        public static NDList linear​(NDArray input,
                                    NDArray weight,
                                    NDArray bias)
        Applies a linear transformation to the incoming data.
        Parameters:
        input - input X: [x1, x2, …, xn, input_dim]
        weight - weight W: [units, input_dim]
        bias - bias b: [units]
        Returns:
        output Y: [x1, x2, …, xn, units]
      • builder

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