Package ai.djl.nn

Class SequentialBlock

  • All Implemented Interfaces:
    Block
    Direct Known Subclasses:
    PointwiseFeedForwardBlock

    public class SequentialBlock
    extends AbstractBlock
    SequentialBlock is a Block whose children form a chain of blocks with each child block feeding its output to the next. The output of the last child is returned as the output of the SequentialBlock.

    SequentialBlock has no direct parameters.

    • Constructor Detail

      • SequentialBlock

        public SequentialBlock()
        Creates an empty sequential block. Use add and addAll to add blocks to be executed in sequence.
    • Method Detail

      • addAll

        public SequentialBlock addAll​(Block... blocks)
        Adds an array of blocks to be executed in sequence, in order.
        Parameters:
        blocks - the array of blocks
        Returns:
        this block
      • addAll

        public SequentialBlock addAll​(java.util.Collection<Block> blocks)
        Adds a Collection of blocks to be executed in sequence, in order.
        Parameters:
        blocks - the Collection of blocks
        Returns:
        this block
      • add

        public SequentialBlock add​(Block block)
        Adds the given Block to the block to be executed in order.
        Parameters:
        block - the block to be added to the sequence of blocks
        Returns:
        this block
      • add

        public SequentialBlock add​(java.util.function.Function<NDList,​NDList> f,
                                   java.lang.String name)
        Adds a LambdaBlock that applies the given function to the sequence of blocks.
        Parameters:
        f - the function forms the LambdaBlock
        name - the function name
        Returns:
        this block
      • removeLastBlock

        public void removeLastBlock()
        Removes the Block added last from the sequence of blocks.
      • replaceLastBlock

        public void replaceLastBlock​(Block block)
        Replaces the Block last added from the sequence of blocks, and adds the given block.
        Parameters:
        block - the block to replace the last block with
      • isReturnIntermediate

        public boolean isReturnIntermediate()
        Returns whether the block returns all intermediate block results or only the end of the sequential chain.
        Returns:
        whether the block returns all intermediate block results or only the end of the sequential chain
      • setReturnIntermediate

        public SequentialBlock setReturnIntermediate​(boolean returnIntermediate)
        Sets whether the block returns all intermediate sequence results.
        Parameters:
        returnIntermediate - true for intermediates, false for only chain result (default and typical behavior is false)
        Returns:
        this SequentialBlock
      • initializeChildBlocks

        public void initializeChildBlocks​(NDManager manager,
                                          DataType dataType,
                                          Shape... inputShapes)
        Initializes the Child blocks of this block. You need to override this method if your subclass has child blocks. Used to determine the correct input shapes for child blocks based on the requested input shape for this block.
        Overrides:
        initializeChildBlocks in class AbstractBaseBlock
        Parameters:
        manager - the manager to use for initialization
        dataType - the requested data type
        inputShapes - the expected input shapes for this block
      • 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
      • 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 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 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