Package ai.djl.nn

Class LambdaBlock

  • All Implemented Interfaces:
    Block

    public class LambdaBlock
    extends AbstractBlock
    LambdaBlock is a Block with no parameters or children.

    LambdaBlock allows converting any function that takes an NDList as input and returns an NDList into a parameter-less and child-less Block. This can be useful in converting activation functions, identity blocks, and more. For example, identity block can be created as new LambdaBlock(x -> x).

    • Constructor Detail

      • LambdaBlock

        public LambdaBlock​(java.util.function.Function<NDList,​NDList> lambda)
        Creates a LambdaBlock that can apply the specified function.
        Parameters:
        lambda - the function to apply
    • Method Detail

      • singleton

        public static LambdaBlock singleton​(java.util.function.Function<NDArray,​NDArray> lambda)
        Creates a LambdaBlock for a singleton function.
        Parameters:
        lambda - a function accepting a singleton NDList and returning another sinleton NDList
        Returns:
        a new LambdaBlock for the function
      • getOutputShapes

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

        public void loadParameters​(NDManager manager,
                                   java.io.DataInputStream is)
                            throws java.io.IOException,
                                   MalformedModelException
        Loads the parameters from the given input stream.
        Specified by:
        loadParameters in interface Block
        Overrides:
        loadParameters in class AbstractBlock
        Parameters:
        manager - an NDManager to create the parameter arrays
        is - the inputstream that stream the parameter values
        Throws:
        java.io.IOException - if an I/O error occurs
        MalformedModelException - if the model file is corrupted or unsupported