Class Multiplication

All Implemented Interfaces:
Block

public class Multiplication extends AbstractBlock
A Multiplication block performs an element-wise multiplication of inputs and weights as opposed to a Linear block which additionally sums up each element-wise multiplication.

Similar to a LinearCollection, multiple split dimensions are supported but they remain optional (i.e. \(t\) can be zero). Other differences to a Linear block are that the weight has an additional dimension of size 1 interspersed (to broadcast the weight to every input of the batch when applying the internally used algebraic operation NDArray.mul(NDArray) ) and that biases are not supported.

Caution: the output-channel is the left-most dimension as opposed to traditionally being the right-most dimension. As the output is one dimension larger than that of a Linear block, it is more efficient and therefore recommended to apply an aggregating function (like the sum) first and only then shift the first axis of the aggregated and thus smaller NDArray instance into last position.

It has the following shapes:

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

The Multiplication block should be constructed using Multiplication.Builder.

  • Method Details

    • forwardInternal

      protected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String,Object> params)
      Specified by:
      forwardInternal in class AbstractBaseBlock
      Parameters:
      parameterStore - the parameter store
      inputs - the input NDList
      training - true for a training forward pass
      params - optional parameters
      Returns:
      the output of the forward pass
    • 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<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
    • prepare

      public void prepare(Shape[] inputShapes)
      Sets the shape of Parameters.
      Overrides:
      prepare in class AbstractBaseBlock
      Parameters:
      inputShapes - the shapes of inputs
    • saveMetadata

      protected void saveMetadata(DataOutputStream os) throws 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:
      IOException - saving failed
    • loadMetadata

      public void loadMetadata(byte loadVersion, DataInputStream is) throws 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:
      IOException - loading failed
      MalformedModelException - data can be loaded but has wrong format
    • multiply

      public NDList multiply(NDArray input, NDArray weight)
      Applies an element-wise multiplication to the incoming data.
      Parameters:
      input - The incoming data
      weight - The weight of this block
      Returns:
      element-wise multiplication of input and weight using broadcasting rules
    • builder

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